Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I identify a file type from a blob/filestream?

We bought an "off the shelf" application a lonnng time ago that is capable of storing files as a blob within SQL Server. We've noticed that the database has more than doubled in size within the past six months due to more frequent usage of this blob field by one department. As a result, the application has become painfully slow.

I've been tasked with removing the blob field from the database and saving the file onto the actual file system. Unfortunately, the application does not store what the file type is within the database. Although I can read the file as it exists in the database, I don't know what extension to save the file as. The application's support desk no longer supports this version of the software and will not talk to us about extracting the data. Unfortunately, we do not have access to their source code.

Any suggestions would be greatly appreciated! Thanks in advance!

like image 360
proudgeekdad Avatar asked Jun 16 '09 22:06

proudgeekdad


People also ask

How do I know my blob type?

Normally, if you're designing a table that stores multiple types of binary data, you'd have a blob column and then a separate varchar2 column that identifies the MIME type of the data. That would be something like application/pdf for PDF documents, image/png for PNG image files, etc.

What type of file is blob?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

How do I read a blob file?

If you cannot open your BLOB file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a BLOB file directly in the browser: Just drag the file onto this browser window and drop it.

Does blob contain filename?

BLOBs don't have filenames. They're just a string of bytes. They represent the file contents, not the file itself.


4 Answers

you can look at the first few bytes and figure it out for the most common file types

http://www.garykessler.net/library/file_sigs.html

like image 64
BlackTigerX Avatar answered Sep 22 '22 14:09

BlackTigerX


Don't save it as any type. Save it as a file with no extension. If you don't know what it is, don't fake it. If the app that saved it requests it, return it from the filesystem the same way it would be returned from the database; as binary data. The database doesn't care what type of data the Binary Object is; neither should you.

like image 24
Paul Sonier Avatar answered Sep 19 '22 14:09

Paul Sonier


You might try using TriD http://mark0.net/soft-trid-e.html

It will scan the files and try to determine the extension.

like image 39
Rob Boek Avatar answered Sep 23 '22 14:09

Rob Boek


You could use the FindMimeFromData() function in UrlMon.dll (using pinvoke).

See this page for an example and this MSDN page for the documentation of the function.

like image 23
M4N Avatar answered Sep 20 '22 14:09

M4N