/// <summary>
        /// Insert a new file content inside datafile in _files collection
        /// </summary>
        public LiteFileInfo Upload(LiteFileInfo file, Stream stream)
        {
            if (file == null)
            {
                throw new ArgumentNullException("id");
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            file.UploadDate = DateTime.Now;

            // insert file in _files collections with 0 file length
            _engine.Insert(FILES, new BsonDocument[] { file.AsDocument });

            // for each chunk, insert as a chunk document
            foreach (var chunk in file.CreateChunks(stream))
            {
                _engine.Insert(CHUNKS, new BsonDocument[] { chunk });
            }

            // update fileLength/chunks to confirm full file length stored in disk
            _engine.Update(FILES, new BsonDocument[] { file.AsDocument });

            return(file);
        }
        /// <summary>
        /// Delete a file inside datafile and all metadata related
        /// </summary>
        public bool Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentNullException("id");
            }

            // remove file reference in _files
            var d = _engine.Delete(FILES, Query.EQ("_id", id));

            // if not found, just return false
            if (d == 0)
            {
                return(false);
            }

            var index = 0;

            while (true)
            {
                var del = _engine.Delete(CHUNKS, Query.EQ("_id", LiteFileInfo.GetChunckId(id, index++)));

                if (del == 0)
                {
                    break;
                }
            }

            return(true);
        }
        /// <summary>
        /// Load data inside storage and returns as Stream
        /// </summary>
        internal LiteFileStream OpenRead(LiteFileInfo entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException("entry");
            }

            return(new LiteFileStream(_engine, entry));
        }
Esempio n. 4
0
        private byte[] GetChunkData(int index)
        {
            // check if there is no more chunks in this file
            var chunk = _engine
                        .Find(LiteFileStorage.CHUNKS, Query.EQ("_id", LiteFileInfo.GetChunckId(_file.Id, index)))
                        .FirstOrDefault();

            // if chunk is null there is no more chunks
            return(chunk == null ? null : chunk["data"].AsBinary);
        }
Esempio n. 5
0
        internal LiteFileStream(DbEngine engine, LiteFileInfo file)
        {
            _engine = engine;
            _file   = file;

            if (file.Length == 0)
            {
                throw LiteException.FileCorrupted(file);
            }

            _positionInChunk   = 0;
            _currentChunkIndex = 0;
            _currentChunkData  = this.GetChunkData(_currentChunkIndex);
        }
 public static LiteException FileCorrupted(LiteFileInfo file)
 {
     return(new LiteException(103, "File '{0}' has no content or is corrupted", file.Id));
 }