Пример #1
0
 private void EnsureParentExists(FileSystemPath target)
 {
     if(!DirectoryExists(target.Parent))
     {
         CreateDirectoryInternal(target.Parent);
     }
 }
Пример #2
0
        private FileSystemItem GetSpecificItem(FileSystemPath path)
        {
            var c = Session.CreateCriteria<FileSystemItem>()
                .Add(Restrictions.Eq("Path", path))
                .SetCacheable(true);

            return c.UniqueResult<FileSystemItem>();
        }
Пример #3
0
        private void AssertParentExists(FileSystemPath target)
        {
            EnsureParentExists(target);

            if (!DirectoryExists(target.Parent))
            {
                throw new DirectoryNotFoundException("Destination directory not found: " + target.Parent);
            }
        }
Пример #4
0
 private IEnumerable<FileSystemItem> FindChildren(FileSystemPath path, ICriterion criterion)
 {
     return Session.CreateCriteria<FileSystemItem>()
         .Add(Restrictions.Eq("Path.Parent", path.ToString()))
         .Add(criterion)
         .AddOrder(Order.Asc("Path.Name"))
         .SetCacheable(true)
         .List<FileSystemItem>();
 }
Пример #5
0
        public void Rebase(FileSystemPath source, FileSystemPath target)
        {
            if (!source.IsDirectory || !target.IsDirectory)
            {
                throw new ApplicationException("Rebase parameters \"source\" and \"target\" should both be directory paths.");
            }

            Parent = new FileSystemPath(target + ToString().Substring(source.ToString().Length)).Parent;
        }
Пример #6
0
 private IEnumerable<FileSystemChunk> GetChunks(FileSystemPath filePath)
 {
     return Session.CreateQuery("from FileSystemChunk fsc where fsc.BelongsTo.Path.Parent = :parentPath and fsc.BelongsTo.Path.Name = :name order by fsc.Offset")
         .SetParameter("parentPath", filePath.Parent)
         .SetParameter("name", filePath.Name)
         .Enumerable<FileSystemChunk>();
 }
Пример #7
0
 private IEnumerable<FileSystemItem> FindChildren(FileSystemPath path, ICriterion criterion)
 {
     return Session.CreateCriteria<FileSystemItem>()
         .Add(Restrictions.Eq("Path.Parent", path.ToString()))
         .Add(criterion)
         .List<FileSystemItem>();
 }
Пример #8
0
        private void DeleteDescendants(FileSystemPath path)
        {
            int deletedChunks = Session.CreateQuery("delete from " + typeof(FileSystemChunk).Name + " fsc where fsc.BelongsTo.ID in (select fsi.ID from " + typeof(FileSystemItem).Name + " fsi where fsi.Path.Parent like :parent)")
                .SetParameter("parent", path + "%")
                .ExecuteUpdate();

            int deletedItems = Session.CreateQuery("delete from " + typeof(FileSystemItem).Name + " fsi where fsi.Path.Parent like :parent")
                .SetParameter("parent", path + "%")
                .ExecuteUpdate();

            logger.Debug("Deleted " + deletedItems + " items and " + deletedChunks + " chunks below " + path);
        }
Пример #9
0
        private void CreateFile(FileSystemPath virtualPath, Stream inputStream)
        {
            EnsureParentExists(virtualPath);

            CheckStreamSize(inputStream, virtualPath);
            long fileSize = inputStream.CanSeek ? inputStream.Length : 0;

            using (var trx = Session.BeginTransaction())
            {
                var item = new FileSystemItem
                {
                    Path = virtualPath,
                    Created = Utility.CurrentTime(),
                    Updated = Utility.CurrentTime(),
                    Length = fileSize
                };
                Session.Save(item);

                var temp = new byte[chunkSize];
                int offset = 0;
                int size = 0;
                while(true)
                {
                    size = inputStream.Read(temp, 0, temp.Length);
                    if (size <= 0)
                        break;

                    var buffer = Copy(temp, size);

                    var chunk = CreateChunk(item, offset, buffer);
                    Session.Save(chunk);

                    if (size < temp.Length)
                        break;

                    offset += size;
                }

                if (fileSize == 0)
                {
                    item.Length = offset + size;
                    Session.Update(item);
                }

                trx.Commit();
            }
            //using (var buffer = (inputStream.CanSeek && (inputStream.Length <= int.MaxValue) ? new MemoryStream((int)inputStream.Length) : new MemoryStream()))
            //{
            //    AssertParentExists(virtualPath);

            //    inputStream.Position = 0;
            //    CopyStream(inputStream, buffer);
            //    inputStream.Position = 0;

            //    var item = new FileSystemItem
            //                   {
            //                       Path = virtualPath,
            //                       Data = (inputStream.CanSeek && inputStream.Length == buffer.GetBuffer().Length ) ? buffer.GetBuffer() : buffer.ToArray(),
            //                       Created = Utility.CurrentTime(),
            //                       Updated = Utility.CurrentTime(),
            //                       Length = buffer.Length
            //                   };

            //    Session.Save(item);
            //    trx.Commit();
            //}
        }
Пример #10
0
 private static void CheckStreamSize(Stream inputStream, FileSystemPath virtualPath)
 {
     if (!IsStreamSizeOk(inputStream))
         throw new Exception(virtualPath + " could not be uploaded created because its size (" + inputStream.Length + ") exceeds the configured UploadFileMaxSize");
 }
Пример #11
0
        internal void UpdateFile(FileSystemPath virtualPath, Stream inputStream)
        {
            CheckStreamSize(inputStream, virtualPath);
            if (inputStream.CanSeek && inputStream.Position > 0)
                inputStream.Position = 0;

            var file = GetSpecificItem(virtualPath);
            file.Updated = Utility.CurrentTime();

            var s = Session;
            using (var trx = s.BeginTransaction())
            {
                var temp = new byte[chunkSize];
                int offset = 0;
                int length = 0;

                bool endOfInputFile = false;
                foreach (var chunk in GetChunks(file.Path))
                {
                    if (endOfInputFile)
                    {
                        s.Delete(chunk);
                        s.Flush();
                        s.Evict(chunk);
                        continue;
                    }

                    int size = inputStream.Read(temp, 0, temp.Length);
                    endOfInputFile = size <= 0;
                    length += size;

                    if (endOfInputFile)
                    {
                        s.Delete(chunk);
                        s.Flush();
                        s.Evict(chunk);
                        continue;
                    }

                    var buffer = Copy(temp, size);

                    chunk.Offset = offset;
                    chunk.Data = buffer;
                    s.Update(chunk);
                    s.Flush();
                    s.Evict(chunk);

                    offset += size;
                }

                while (true && !endOfInputFile)
                {
                    int size = inputStream.Read(temp, 0, temp.Length);
                    if (size <= 0)
                        break;

                    length += size;
                    var buffer = Copy(temp, size);
                    var chunk = CreateChunk(file, offset, buffer);
                    s.Save(chunk);
                    s.Flush();
                    s.Evict(chunk);
                }

                file.Length = length;
                s.Update(file);

                trx.Commit();
            }
        }
Пример #12
0
 public bool IsDescendantOf(FileSystemPath source)
 {
     return Parent.StartsWith(source.ToString());
 }
Пример #13
0
        private void CreateFile(FileSystemPath virtualPath, Stream inputStream)
        {
            EnsureParentExists(virtualPath);

            CheckStreamSize(inputStream, virtualPath);
            long fileSize = inputStream.CanSeek ? inputStream.Length : 0;

            using (var trx = Session.BeginTransaction())
            {
                var item = new FileSystemItem
                {
                    Path    = virtualPath,
                    Created = Utility.CurrentTime(),
                    Updated = Utility.CurrentTime(),
                    Length  = fileSize
                };
                Session.Save(item);

                var temp   = new byte[chunkSize];
                int offset = 0;
                int size   = 0;
                while (true)
                {
                    size = inputStream.Read(temp, 0, temp.Length);
                    if (size <= 0)
                    {
                        break;
                    }

                    var buffer = Copy(temp, size);

                    var chunk = CreateChunk(item, offset, buffer);
                    Session.Save(chunk);

                    if (size < temp.Length)
                    {
                        break;
                    }

                    offset += size;
                }

                if (fileSize == 0)
                {
                    item.Length = offset + size;
                    Session.Update(item);
                }

                trx.Commit();
            }
            //using (var buffer = (inputStream.CanSeek && (inputStream.Length <= int.MaxValue) ? new MemoryStream((int)inputStream.Length) : new MemoryStream()))
            //{
            //    AssertParentExists(virtualPath);

            //    inputStream.Position = 0;
            //    CopyStream(inputStream, buffer);
            //    inputStream.Position = 0;

            //    var item = new FileSystemItem
            //                   {
            //                       Path = virtualPath,
            //                       Data = (inputStream.CanSeek && inputStream.Length == buffer.GetBuffer().Length ) ? buffer.GetBuffer() : buffer.ToArray(),
            //                       Created = Utility.CurrentTime(),
            //                       Updated = Utility.CurrentTime(),
            //                       Length = buffer.Length
            //                   };

            //    Session.Save(item);
            //    trx.Commit();
            //}
        }
Пример #14
0
        internal void UpdateFile(FileSystemPath virtualPath, Stream inputStream)
        {
            CheckStreamSize(inputStream, virtualPath);
            if (inputStream.CanSeek && inputStream.Position > 0)
            {
                inputStream.Position = 0;
            }

            var file = GetSpecificItem(virtualPath);

            file.Updated = Utility.CurrentTime();

            var s = Session;

            using (var trx = s.BeginTransaction())
            {
                var temp   = new byte[chunkSize];
                int offset = 0;
                int length = 0;

                bool endOfInputFile = false;
                foreach (var chunk in GetChunks(file.Path))
                {
                    if (endOfInputFile)
                    {
                        s.Delete(chunk);
                        s.Flush();
                        s.Evict(chunk);
                        continue;
                    }

                    int size = inputStream.Read(temp, 0, temp.Length);
                    endOfInputFile = size <= 0;
                    length        += size;

                    if (endOfInputFile)
                    {
                        s.Delete(chunk);
                        s.Flush();
                        s.Evict(chunk);
                        continue;
                    }

                    var buffer = Copy(temp, size);

                    chunk.Offset = offset;
                    chunk.Data   = buffer;
                    s.Update(chunk);
                    s.Flush();
                    s.Evict(chunk);

                    offset += size;
                }

                while (true && !endOfInputFile)
                {
                    int size = inputStream.Read(temp, 0, temp.Length);
                    if (size <= 0)
                    {
                        break;
                    }

                    length += size;
                    var buffer = Copy(temp, size);
                    var chunk  = CreateChunk(file, offset, buffer);
                    s.Save(chunk);
                    s.Flush();
                    s.Evict(chunk);
                }

                file.Length = length;
                s.Update(file);

                trx.Commit();
            }
        }