What is the proper way to preview a file (image or pdf) using active storage? - rails-activestorage

I am trying to preview a PDF file and an image file I have uploaded:
class Document < ApplicationRecord
belongs_to :resourceable, :polymorphic => true
has_one_attached :file
end
I tried to use .preview and .representation methods to show the preview of uploaded image or pdf as thumbnail.
using preview(), results in the following error:
#document.file.preview(resize_to_limit: [100, 100])
ActiveStorage::UnpreviewableError: ActiveStorage::UnpreviewableError
using representation(), image_tag is unable to resolve the image url from the ActiveStorage::Variant object resulting in the following error:
>>> image_tag(#document.file.representation(resize: '500x500'))
ArgumentError: Can't resolve image into URL: undefined method `to_model' for #<ActiveStorage::Variant:0x00007fe39e809768>
document.file.representable? and document.file.previewable? in some documentation. Wonder what does these methods do? I could get representable as true sometimes but previewable false.

Since Rails 5.2 the image_tag helper required you to pass a url. Before it would guess as to what was passed.
To pass the url from your active storage variant do something like this:
image_tag(main_app.rails_representation_url(
#document.file.variant(resize: '500x500'))
) if #document.file.attached?
Images are not previewable according to the docs, PDF's are.
To check if the document has a preview available use the previewable? method:
image_tag(main_app.rails_representation_url(
#document.try(#document.previewable? ? :preview : :variant, resize: '500x500')
) if #document.file.attached?

Related

Why can't Azure Search import JSON blobs?

When importing data using the configuration found below, Azure Cognitive Search returns the following error:
Error detecting index schema from data source: ""
Is this configured incorrectly? The files are stored in the container "example1" and in the blob folder "json". When creating the same index with the same data in the past there were no errors, so I am not sure why it is different now.
Import data:
Data Source: Azure Blob Storage
Name: test-example
Data to extract: Content and metadata
Parsing mode: JSON
Connection string:
DefaultEndpointsProtocol=https;AccountName=EXAMPLESTORAGEACCOUNT;AccountKey=EXAMPLEACCOUNTKEY;
Container name: example1
Blob folder: json
.json file structure.
{
"string1": "vaule1",
"string2": "vaule2",
"string3": "vaule3",
"string4": "vaule4",
"string5": "vaule5",
"string6": "vaule6",
"string7": "vaule7",
"string8": "vaule8",
"list1": [
{
"nested1": "value1",
"nested2": "value2",
"nested3": "value3",
"nested4": "value4"
}
],
"FileLocation": null
}
Here is an image of the screen with the error when clicking "Next: Add cognitive skills (Optional)" button:
To clarify there are two problems:
1) There is a bug in the portal where the actual error message is not showing up for errors, hence we are observing the unhelpful empty string "" as an error message. A fix is on the way and should be rolled out early next week.
2) There is an error when the portal attempts to detect index schema from your data source. It's hard to say what the problem is when the error message is just "". I've tried your sample data and it works fine with importing.
I'll update the post once the fix for displaying the error message is out. In the meantime (again we're flying blind here without the specific error string) here are a few things to check:
1) Make sure your firewall rules allow the portal to read from your blob storage
2) Make sure there are no extra characters inside your JSON files. Check the whitespace charcters are whitespace (you should be able to open the file in VSCode and check).
Update: The portal fix for the missing error messages has been deployed. You should be able to see a more specific error message should an error occur during import.
Seems to me that is a problem related to the list1 data type. Make sure you're selecting: "Collection(Edm.String)" for it during the index creation.
more info, please check step 5 of the following link: https://learn.microsoft.com/en-us/azure/search/search-howto-index-json-blobs
I have been in contact with Microsoft, and this is a bug in the Azure Portal. The issue is the connection string wizard does not append the Endpoint suffix correctly. They have recommeded to manually pasting the connection string, but this still does not work for me. So this is a suggested answer by Microsoft, but I don't believe is completely correct because the portal outputs the same error message:
Error detecting index schema from data source: ""

undefined method `signed_id' for "sdsd":String - Active Storage

I'm having an issue using rails-admin and Active Storage at the same time. I saw that you can modify the value of labels with the label method. For example, for the "user #1" thing:
def name
email
end
The problem is that I have an Asset model to upload multiple files, but I see this:
Ugly attachment id
If I try to do the same trick (just to see if every file is now called "sdsd"):
def files
'sdsd'
end
Then this shows up:
undefined method `signed_id' for "sdsd":String
I tried making a reset of the database but it didn't work at all. Any ideas? I just want to show every filename instead of that ugly id's.

Getting PDF_VALIDATION_FAILED exception when trying to create envelopne in DocuSign sample recipe code

I am trying to run the code available on GitHub.
Issue is when I am trying to create an envelope I am getting an exception saying "PDF_VALIDATION_FAILED".
Can anyone help me out with this issue?
Really you should be creating a new issue to log the new error you are getting, or modify your original post. In any case the issue is most likely due to the file extension, the default is pdf so if you want to send a different format document you can do the following:
// Add a document to the envelope
Document doc = new Document();
doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
doc.Name = Path.GetFileName("/PATH/TO/DOC/TEST.DOCX");
doc.DocumentId = "1";
doc.FileExtension = "docx";
I managed to get a fix for this one. Apparently the file I was uploading was a corrupt file. However, not I am getting a different error 'UNABLE_TO_LOAD_DOCUMENT' when I try to upload a file in any format other than pdf.
Can anyone help me with this query? Also, what all file formats does DocuSign support?
Also, one of the previous libraries 'DocuSign.Integrations.Client' seem to work fine with word document uploads. Should they be used instead of 'DocuSign.eSign.Api', 'DocuSign.eSign.Client' and 'DocuSign.eSign.Model'?
This is in response to the second error you mentioned:
I think you are missing assigning the FileExtension to the required format
something like : doc.FileExtension = "docx"
Once you do that, you will get rid of UNABLE_TO_LOAD_DOCUMENT error and the document can be sent successfully.

Remove collection from model RacerJS?

This is my client Side code:
model.remove('agent',{'text':'online'});
I cant able to remove collection from model,Its shows following error in console
Error: remove must be performed under a collection and document id. Invalid path: agent
From the documentation, it looks like 'path' should be the collectionname.id. In this case, maybe I suppose it should be 'agent.id'.
This blog refers that the path should be in the format collection.documentId.document.
http://blog.derbyjs.com/2012/04/13/derby-v0-dot-3-0/

Kohana image upload validation

I'm having problems with image validation, documentation as always doesn't help, so there is my simple validation to check if image was uploaded
$validate = Validation::factory($_FILES)
->rule('file', array('Upload::not_empty'));
But this gives me error
ErrorException [ Warning ]: call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members
What could be causing this error?
The 2nd argument for rule is a PHP callback. So this would suffice because the not_empty method in the upload class is static:
rule('file', 'Upload::not_empty');
You only really need to do the array syntax when the method is not static:
rule('file', array($class, 'method'));

Resources