How to restore an unknown type BLOB field from Firebird - blob

I am trying to restore a BLOB field stored in a Firebird database, and the only information I have is that the content of the BLOB field is a document.
I've tried using IBManager to right-click on the cell and click "Save BLOB to file", but the saved file is unreadable (as if it was encrypted). I tried to open it with Microsoft Word, notepad, adobe etc, with no success. I also tried opening it with WinRAR (I thought that it might have been compressed before being stored to the database) but still nothing.
Is there a way to find out whether and how the BLOB file was compressed, and how to restore it?
Thanks in advance!
Update:
I have converted the firebird database to SQL and I use the following code to extract the Unencoded BLOB documents:
conn.Open();
dr = comm.ExecuteReader();
while (dr.Read())
{
byte[] document_byte = null;
if (dr[1] != System.DBNull.Value)
{
document_byte = (byte[])dr[1];
}
string subPath = "C:\\Documents\\" + dr[0] + "\\";
System.IO.Directory.CreateDirectory(subPath);
if (document_byte != null)
{
System.IO.File.WriteAllBytes(subPath + "Document", document_byte);
}
}
How can I adjust my code to decode the BLOB file from Base64 since I know is Base64 encoded?

Unless the field uses BLOB filter the data is stored into database as is, ie Firebird doesn't alter it in any way. Check the field's definition, if it does have SUB_TYPE 0 (or binary) then it is "ordinary" binary data, ie Firebird doesn't apply any filter to it. And even in case the field uses some filter, unless there is a bug in the filter code you should get the original data back when reading the content of the BLOB.
So it comes down to the program which stored the document into DB, it is quite possible that it compressed or encrypted the file, but there is no way Firebird can help you to figure out what algorithm was used... One option would be to save the content of the BLOB into file and then try the *nix file command, perhaps it is able to detect the file format used.
I would also check the DB for corruptions, just for case (Firebird's gfix command line tool).

Related

Converting image files to VARBINARY(max) in flutter

I need to store images that are captured using "image picker" in a SQL server database
final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 25);
now, I know that the images should be stored in a "VARBINARY" datatype. I tried to convert the XFile "image" that I got from the "ImagePicker()" to VARBINARY and these are the steps I made.
final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 25);
var imageTemp = File(image.path);
and then I store the image in the database using the following.
Attachment(
attachmentData: base64Encode((imageTemp .readAsBytesSync())),
attachmentName: imageTemp .path.split('/').last,
attachmentSize: imageTemp .readAsBytesSync().lengthInBytes,
attachmentType: imageTemp .path.split(".").last,
)
"attachmentData: base64Encode((imageTemp .readAsBytesSync()))"
"attachmentData is a String"
So from XFile to VARBINARY, I had done the following
final image = await ImagePicker().pickImage(source: ImageSource.camera, imageQuality: 25);
var imageTemp = File(image.path);
base64Encode((imageTemp .readAsBytesSync())) //the sent data
when I print the data after the readAsByteSynce() it starts with [255,216,.255,..] which is FF D8 FF
it looks like this
screen shot of print(imageTemp.readAsBytesSync())
and when I print the image after using base64Encode it starts with /9j/4Q
it looks like this
screen shot of print(base64Encode((imageTemp.readAsBytesSync())))
now when it's stored in the SQL server database it starts with 0x2F39
it looks like this screen shot of the attachmebtData field in the SQL server database
The image extension is .jpg. Shouldn't jpg images start with 0xFF?
I need to take these images and view them in ArcGIS and when I open the image it says that the images are not in a supported format.
can someone please tell me what am I doing wrong and why are my jpg images starts with 0x2F in the database and not 0xFFD8FF and how to fix this?
I want the image to be stored in the database
I also Tried converting Uint8List to hex using dart package hex 0.2.0
which converted it to this the output of print(Hex.encode(await image.readAsBytes))
as you can see in the above picture it starts with ffd8ff which is the correct format for jpg pictures, but when I send this to the database it looks like it converts these letters to VARBINARY and the output starts with 0x666666 and looks like this image from SQL server AttachmentData field when Hex.encode data was sent to it
The problem was that I'm not decoding it in the backend before inserting it into the database.
I'm Using Python "FAST API" and I needed to write the following line to attachmentdata before inserting it into the database.
attachment.attachmentData = base64.decodebytes(attachment.attachmentData)

How to access files stored in SQL Server's FileTable?

As I know SQL Server since version 2012 has a new feature, FileTable. It allows us to store files in the file system and to use them from T-SQL.
I am trying to use this feature and I have no idea how to do it properly.
Generally, I don't know how to access files stored in the file table. Let's suppose I have asp.net MVC app and there are a lot of images which I show on web pages in img tags. I would like to store these images in Filetable and access them as files from the filesystem. But I don't know where these files are stored and how to use them as files. Now my images are stored in web application directory in folder images and I write something like this:
<img src='/images/mypicture.png' />
And if I move my images to file table what I should write in src?
<img src='path-toimage-in-filetable' />
I don't think you still need this, anyways I'll post my answer for anyone else interested.
First, a filetable still being a table, so, if you want to access to data from it you need to use a Select SQL statement. So you'd need something like:
select name, file_stream from filetable_name
where
name = 'file_name',
file_type = 'file_extension'
just execute an statement like this in your asp.net app, then fetch the results and use the file_stream column to get the binary data of the stored file. If you want to retrieve the file from HTML, first you need to create an action in your controller, which will return the retrieved file:
public ActionResult GetFile(){
..
return File(file.file_stream,file.file_type);
}
After this, put in you HTML tag something like:
<img src="/controller/GetFile" />
hope this could help!
If you want to know the schema of a filetable see
here
I assume by FileTable you actually mean FileStream. A couple notes about that:
This feature is best used if your files are actually files
The files should be, on average, greater than 1mb - although there can be exceptions to this rule, if they're smaller than 1mb on average, you may be better off using a VARBINARY(MAX) or XML data type as appropriate. If your images are very small on average (only a few KB), consider using a VARBINARY(MAX) column.
Accessing these files will require an open transaction and that the database is properly configured for FILESTREAM
You can get some significant advantages bypassing the normal SQL engine/database file method of data access by telling SQL Server that you want to access the file directly, however it's not meant for directly accessing the file on the file system and attempting to do so can break SQL's management of these files (transactional consistency, tracking, locking, etc.).
It's pretty likely that your use case here would be better served by using a CDN and storing image URLs in the table if you really need SQL for this. You can use FILESTREAM to do this (see code sample below for one implementation), but you'll be hammering your SQL server for every request unless you store the images somewhere else anyway that the browser can properly cache (my example doesn't do that) - and if you store them somewhere else for rendering int he browser you might as well store them there to begin with (you won't have transactional consistency for those images once they're copied to some other drive/disk/location anyway).
With all that said, here's an example of how you'd access the FILESTREAM data using ADO.NET:
public static string connectionString = ...; // get your connection string from encrypted config
// assumes your FILESTREAM data column is called Img in a table called ImageTable
const string sql = #"
SELECT
Img.PathName(),
GET_FILESTREAM_TRANSACTION_CONTEXT()
FROM ImageTagble
WHERE ImageId = #id";
public string RetreiveImage(int id)
{
string serverPath;
byte[] txnToken;
string base64ImageData = null;
using (var ts = new TransactionScope())
{
using (var conn = new SqlConnection(connectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
cmd.Parameters.Add("#id", SqlDbType.Int).Value = id;
using (SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
serverPath = rdr.GetSqlString(0).Value;
txnToken = rdr.GetSqlBinary(1).Value;
}
}
using (var sfs = new SqlFileStream(serverPath, txnToken, FileAccess.Read))
{
// sfs will now work basically like a FileStream. You can either copy it locally or return it as a base64 encoded string
using (var ms = new MemoryStream())
{
sfs.CopyTo(ms);
base64ImageData = Convert.ToBase64String(ms.ToArray());
}
}
}
ts.Complete();
// assume this is PNG image data, replace PNG with JPG etc. as appropraite. Might store in table if it will vary...
return "data:img/png;base64," + base64ImageData;
}
}
Obviously, if you have lots of images to handle like this this is not an ideal method - don't try to make an instance of SQL server into what you should be using a CDN for.... However, if you have other really good reasons, you should try to grab as many images as possible in a single request/transaction (e.g. if you know you're displaying 50 images on a page, get all 50 with a single transaction scope, don't use 50 transaction scopes - this code won't handle that).

PhpBB attachment files

In the phpbb_attachments table of the database, we can find link between file on server and real name.
Example :
Physical filename = 2_8f375562c92996d3272ea0b43947b0e7
Real_filename = blah.jpg
Then if I try to open the file 2_8f375562c92996d3272ea0b43947b0e7 which is stored on server (by renaming with .jpg), its content is not the real image : the image is like "encrypted" !
Is there an attachment content encryption when storing attachment on server ?
If so, how to manually see the real content of an attachment?
There is a script in the phpBB knowledge base that you can use to back up all attachments with their original file name - https://www.phpbb.com/kb/article/backing-up-attachments-with-their-original-filenames/

cannot view document in ravendb studio

When I try to view my document I get this error:
Client side exception:
System.InvalidOperationException: Document's property: "DocumentData" is too long to view in the studio (property length: 699.608, max allowed length: 500.000)
at Raven.Studio.Models.EditableDocumentModel.AssertNoPropertyBeyondSize(RavenJToken token, Int32 maxSize, String path)
at Raven.Studio.Models.EditableDocumentModel.AssertNoPropertyBeyondSize(RavenJToken token, Int32 maxSize, String path)
at Raven.Studio.Models.EditableDocumentModel.<LoadModelParameters>b__2a(DocumentAndNavigationInfo result)
at Raven.Studio.Infrastructure.InvocationExtensions.<>c__DisplayClass17`1.<>c__DisplayClass19.<ContinueOnSuccessInTheUIThread>b__16()
at AsyncCompatLibExtensions.<>c__DisplayClass55.<InvokeAsync>b__54()
I am saving a pdf in that field.
I want to be able to edit the other fields.
Is it possible for it to ignore the field that's too big?
Thanks!
Don't save large binary (or base64 encoded) data into the json document. That's a poor use of the database. Instead, you should consider one of these two options:
Option 1
Write the binary data to disk (or cloud storage) yourself.
Save a file path (or url) to it in your document.
Option 2
Use Raven's attachments feature. This is a separate area in the database meant specifically for storing binary files.
The advantage is that your binary documents are included in database backups, and if you like you can take advantage of features like my Indexed Attachments Bundle, or write your own custom bundles that use attachment triggers.
The disadvantage is that your database can grow very large. For this reason, many prefer option 1.

How to open the .db paradox file

i want to view the test.db file, i search for it's editor but didn't get any one
So please help to see the it in editor as like sql server.
i found some sqlite editor but it's not an sqlite file on most forum it say that it is an paradox .db file.
So how do i open it
Thanks
To access Paradox tables in .NET you can use ODBC. Here's a small example (in C#):
private static void RunMinimumParadoxTest()
{
const string ConnectionStringFormat =
"Driver={{Microsoft Paradox Driver (*.db )}};Uid={0};UserCommitSync=Yes;Threads=3;SafeTransactions=0;" +
"ParadoxUserName={0};ParadoxNetStyle=4.x;ParadoxNetPath={1};PageTimeout=5;MaxScanRows=8;" +
"MaxBufferSize=65535;DriverID=538;Fil=Paradox 7.X;DefaultDir={2};Dbq={2};CollatingSequence={3}";
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.Odbc");
using (DbConnection connection = factory.CreateConnection())
{
string userName = "Tor";
string paradoxNetPath = #"C:\BdeNet";
string databasePath = #"C:\LangloMainSrv\LData\Ordre\LordWin\Database2011";
string collatingSequence = "Norwegian-Danish";
connection.ConnectionString =
String.Format(ConnectionStringFormat, userName, paradoxNetPath, databasePath, collatingSequence);
connection.Open();
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = "select Count(*) from [OrdreDet] where [Ordrenr] = 81699002";
object itemCount = command.ExecuteScalar();
Console.WriteLine("Order items: {0}", itemCount);
Console.ReadKey();
}
}
}
Also see the following link for more details: http://msdn.microsoft.com/en-us/library/ms710922(VS.85).aspx.
A Paradox db file contains just one flat table. The actual structure of the DB file changed over time and different versions. But you can usually open the DB file with MS Excel - of course that changed over different versions too.
As noted above, other database applications, also including Paradox for Dos and Paradox for Windows, will open the file and other features as well. The key, for example is in the PX file with the same table name.
All of this assumes the table is not password protected, which an application database could be - or that you know the password. Beware if you get an error to that effect.
You can open and view Paradox database files using Database Desktop that is shipped with Borland C++Builder. A free alternative is BB's Database Desktop. The software may require administrator privileges to run correctly.
You can use gnumeric spreadsheet, paradox-db-reader or BB database desktop to read db paradox file.
BB database dekstop able to read XG0 file too.
BB's Database Desktop now called JEDI Database Desktop, but project is closed and it couldn't edit my table. I have had to use some hack: open *.db file in MS Excel 2007, edit it, export to *.csv, close file then Open *.db file in Paradox Data Editor 3.2.0, clear all table data and import previosly saved csv-file. And it works (don't know why but this app can't insert row in my file itself)!