Detecting file type - ruby

What is the best way to find the file mime type of remote file in ruby on rails application (eg. I have a file located in s3 and want to check its file type, I don't think checking extension of file is a good idea).
To be specific, I want to find whether the given media is video or audio.

There's a library called ruby-filemagic that can check the content of the file and return the mime type. However, it required to access and read the file and it can be an issue if you need to fetch the content of the file from a remote source.
Please note that in the specific Amazon S3 case, you can also store the mime type of the file to Amazon S£ as object metadata when you upload the file itself. I strongly recommend you to do this, so that you can easily retrieve the metadata and search for the given attribute, instead of guessing it from the file.

Related

How to return an image file (Byte[]) as a compressed file with Spring API?

I'm the company's file server
Get the file as byte[] through the image path and authentication key.
(This server is not accessible to me.)
What I want to do is, when the user downloads the selected files, I want to compress these files and provide them as a compressed file.
Since the company's file server does not have a download API for multiple files, I think I need to request as many APIs as the number of file lists with a for statement in my service API.
In other words, it seems that we need to take a List<Byte[]> and compress this list.
Is there something wrong with my method?
And can I pass the result as json after compression? (I confirmed that the image file is passed as json.)

Can I serve files stored in Google Cloud Storage via a http.FileServer in golang?

I have developed a small web application that runs a web server in golang.
Each user can login, view the list of their docs (previously uploaded) and click on an item to view an html page that shows some fields of the document plus an tag with a src attribute
The src attribute includes an url like "mydocuments/download/123-456-789.pdf"
On the server side I handle the URL ("mydocuments/download/*") via an http Handler
mymux.HandleFunc(pat.Get("/mydocuments/download/:docname"), DocDownloadHandler)
where:
I check that the user has the rights to view the document in the url
Then I create a fileserver that obviously re-maps the url to the real path of the folder where the files are stored on the filesystem of the server
fileServer := http.StripPrefix("/mydocs/download/",http.FileServer(http.Dir("/the-real-path-to-documents-folder/user-specific-folder/)))
and of course I serve the files
fileServer.ServeHTTP(w, r)
IMPORTANT: the directory where the documents are stored is not the static-files directory I sue for the website but a directory where all files end after being uploaded by users.
My QUESTION
As I am trying to convert the code for it to work also on Google Cloud, I am trying to change the code so that files are stored in a bucket (or, better in "sub-directories" -as they do not properly exist- of a bucket).
How can I modify the code so to map the real document url as available via the cloud storage bucket?
Can I still use the http.FileServer technique above (if so what should I use instead of http.Dir to map the bucket "sub-folder" path where the documents are stored)?
I hope I was enough clear to explain my issue...
I apologise in advance for any unclear point...
Some options are:
Give the user direct access to the resource using a signed URL.
Write code to proxy the request to GCS.
Use http.FS with an fs.FS backed by GCS.
It's possible that a fs.FS for GCS already exists, but you may need to write one.
You can use http.FileSystem since it is an interface and can be implemented however you like.

How to download file using ProtoBuf

I'm trying to implement file download directly via Browser. Our company uses Protocol Buffer as data communication format. So how can I download the file once I open the web page?
I tried to user bytes and stream of Protocol Buffer. But the result is
{"result":{"data":"Cw4ODg4ODgsMCw4ODg4ODgsMTUwMCwwLDE1MDAsNDAwMDAsMTAwMDAsMzAwMDAKMDMvMTEvMjAxNSxVbmtub3duIEl0ZW0sUHJlIFJvbGwgVmlkZW8gKG1heCAwOjMwKSw2MDAwMCwzMTAwMCwyOTAwMCw1MDAwMCwyNDAwMCwyNjAwMCwyMC4wMCUsODQ0NCwwLDQwMDAsNDQ0NCw4OTAzODgsMCwwLDAsODg4ODg4LDAsODg4ODg4LDE1MDAsMCwxNTAwLDQwMDAwLDIxMDAwLDE5MDAwCg=="}}
Protobuf is good for structured communication but http provides the perfect protocol for downloading files. The right headers need to be set and the browser will download the file.
If you really have to use protobuf to transfer files, then you need to add some javascript that is parsing the protobuf first and then turns it into a file that can be downloaded. See How to create a dynamic file + link for download in Javascript? for reference.
So, send the message as bytes, add the javascript that parses the protobuf message to extract the bytes, and then create the file download like on the linked answer.

How to add/edit file metadata in Golang?

I am writing a Bittorrent client in Go and I would like to save the progress of the download by writing a bitfield to the file headers/metadata. This allows me to cancel the download and restart from where I left off the next time I start downloading.
However, I could not find any standard or third party libraries that allow me to write to file metadata. The closest I have gotten was printing the FileInfo struct returned by os.Stat. I am not sure how to add/edit the FileInfo struct.
I realized there are no library functions in Golang like that because metadata is a filesystem specific implementation. So, I have decided to hide my metadata in a hidden file like .filename.meta

Use different Mime-Type in FTP

When uploading a file to an FTP server it always defines the mime type as the file extension that the name is. Is there any way to change the mime type that the file is being uploaded as?
EX: If I am uploading Hi.php to the URL /Hi.bz2 it doesn't run the .php script at all and just reads it as plain text
FTP has no idea of a MIME-Type.
But the HTTP server, which serves the file later needs some way to determine the type of the file and the usual way for static files is to look at the files extension. It it the same way most system determine the type of the file when they need to know the responsable application.

Resources