FileStorage.Upload from stream does not seem to be working
See original GitHub issueHey David,
I have an issue with uploading images to LiteDB using the FileStorage.Upload method. Below is the code snipped that is failing:
using( var image = Image.FromFile("C:\\Temp\\testfile.jpg").GetThumbnailImage(200, 200, null, IntPtr.Zero))
using (var stream = new MemoryStream())
{
image.Save(stream, ImageFormat.Jpeg);
Database.FileStorage.Upload(Guid.NewGuid().ToString(), "testfile.jpg", stream);
}
When I debug I see that the stream contains data, but it does not seem to get written to the database. I see the document in the database with the filename, Id, etc. But the chunk-size remains 0.
Using OpenWrite and saving the image to the LiteFileStream directly does work, however, here I get an error when trying to read from the collection while OpenWrite is still writing to the database:
An exception of type ‘System.IO.IOException’ occurred in LiteDB.dll but was not handled in user code
Additional information: The requested operation cannot be performed on a file with a user-mapped section open.
Stack trace:
at System.IO.FileStream.SetLengthCore(Int64 value)
at System.IO.FileStream.SetLength(Int64 value)
at LiteDB.FileDiskService.SetLength(Int64 fileSize) in FileDiskService.cs:line 129
at LiteDB.FileDiskService.ClearJournal(UInt32 lastPageID) in FileDiskService.cs:line 191
at LiteDB.TransactionService.PersistDirtyPages() in TransactionService.cs:line 79
at LiteDB.LiteEngine.Transaction[T](String collection, Boolean addIfNotExists, Func`2 action) in LiteEngine.cs:line 1192
at LiteDB.LiteEngine.Insert(String collection, IEnumerable`1 docs, BsonType autoId) in LiteEngine.cs:line 886
at LiteDB.LiteEngine.Insert(String collection, BsonDocument doc, BsonType autoId) in LiteEngine.cs:line 873
at LiteDB.LiteStorage.OpenWrite(String id, String filename, BsonDocument metadata) in LiteStorage.cs:line 37
Ok, so actually I have 2 problems it seems. So either I am doing something very wrong or there really is a problem with the Upload method. Any ideas?
Sincerely, Glenn
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (3 by maintainers)
Top GitHub Comments
This is not intuitive, I spent 3 hours debugging the same issue. I’ve never had to manually set my stream.Position = 0 in any other libraries.
Maybe you could check if stream.Position == stream.Length or if the number of bytes copies is 0 and throw a useful exception to the user?
I’ve made a small set of extension methods to handle a few un-intuitive things like this.
` public static class LiteDatabaseExtensions {
You always need be sure that you stream are at beginning position. LiteDB will read you stream from current position to end. If your stream already at end, there is nothing to read.
In you case, the problem is
image.Save
that read first stream (your image) to create a thumbnail. This method Save run over your memory stream (to save the bytes) and do not get back to first position (to be saved on litedb).Its not about how LiteDB upload but most about how
Stream
works. If you write direct your image (with no thumbnail) where is not need set position = 0 because already in begin.