Exemplo n.º 1
0
        public async Task DeleteFielTest()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddHybridFS_Sqlite();

            var serviceProvider = services.BuildServiceProvider();

            IFileStore store = serviceProvider.GetService <IFileStore>();

            Random rd = new Random();

            var       content = new byte[rd.Next(1, 3097152)];
            FileEntry entry;

            using (var stream = new MemoryStream(content))
            {
                entry = await store.CreateFileFromStreamAsync(stream);
            }

            var file = await store.GetFileEntryAsync(entry.Id);

            Assert.Equal(content.Length, file.Length);

            await store.TryDeleteFileEntryAsync(entry.Id);

            var fileEntry = await store.GetFileEntryAsync(entry.Id);

            Assert.Null(fileEntry);
        }
Exemplo n.º 2
0
        public async Task AutoShardinDatabaseTest()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddHybridFS_Sqlite(options =>
            {
                options.ExpandMode = DbExpandByDateMode.PerMinute;
            });

            var serviceProvider = services.BuildServiceProvider();

            IFileStore store = serviceProvider.GetService <IFileStore>();

            for (int i = 0; i < 100; i++)
            {
                var       content = new byte[10000];
                FileEntry entry;
                using (var stream = new MemoryStream(content))
                {
                    entry = await store.CreateFileFromStreamAsync(stream);
                }

                Thread.Sleep(1000);
            }
        }
Exemplo n.º 3
0
        /// <inheritdoc/>
        public async Task <HybridFileInfo> CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)
        {
            // 检查文件夹记录是否完整
            var index = await _context.FileIndexs.Where(c => c.Path == path).FirstOrDefaultAsync();

            if (index != null && overwrite == false)
            {
                throw new FileHasExistedException("同名文件已经存在!", path);
            }

            // 创建文件记录
            if (index == null)
            {
                index = new FileIndex();
            }

            index.Id            = IdHelper.GetLongId();
            index.Path          = PathHelper.NormalizePath(path);
            index.DirectoryPath = PathHelper.NormalizePath(Path.GetDirectoryName(path));

            // 检查并创建目录
            await TryCreateDirectoryAsync(index.DirectoryPath);

            index.Name        = Path.GetFileName(path);
            index.Extension   = Path.GetExtension(path);
            index.Length      = inputStream.Length;
            index.IsDirectory = false;

            var nowTime = DateTime.UtcNow;

            index.LastModifiedUtc  = nowTime;
            index.Visits           = 0;
            index.LastVisitTimeUtc = nowTime;

            var md5  = GetStreamMD5(inputStream);
            var file = await(from c in _context.FileIndexs
                             where c.MD5 == md5 && c.Length == inputStream.Length
                             select c).FirstOrDefaultAsync();

            if (file == null)
            {
                // 把文件流交给文件存储保存
                var entry = await _store.CreateFileFromStreamAsync(inputStream);

                index.MD5         = entry.MD5;
                index.FileEntryId = entry.Id;
            }
            else
            {
                index.MD5         = file.MD5;
                index.FileEntryId = file.Id;
            }

            _context.FileIndexs.Add(index);

            await _context.SaveChangesAsync();

            return(index.ToFileInfo());
        }
        public virtual async Task <string> CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)
        {
            if (_mediaCreatingEventHandlers.Any())
            {
                // Follows https://rules.sonarsource.com/csharp/RSPEC-3966
                // Assumes that each stream should be disposed of only once by it's caller.
                var outputStream = inputStream;
                try
                {
                    var context = new MediaCreatingContext
                    {
                        Path = path
                    };

                    foreach (var mediaCreatingEventHandler in _mediaCreatingEventHandlers)
                    {
                        // Creating stream disposed by using.
                        using (var creatingStream = outputStream)
                        {
                            // Stop disposal of inputStream, as creating stream is the object to dispose.
                            inputStream = null;
                            // Outputstream must be created by event handler.
                            outputStream = null;

                            outputStream = await mediaCreatingEventHandler.MediaCreatingAsync(context, creatingStream);
                        }
                    }

                    return(await _fileStore.CreateFileFromStreamAsync(context.Path, outputStream, overwrite));
                }
                finally
                {
                    // This disposes the last outputStream.
                    outputStream?.Dispose();
                }
            }
            else
            {
                return(await _fileStore.CreateFileFromStreamAsync(path, inputStream, overwrite));
            }
        }
Exemplo n.º 5
0
        public async Task <string> UploadAsync(FileUploadViewModel viewModel)
        {
            foreach (var file in viewModel.Files)
            {
                var path = viewModel.CurrentDirectory?.TrimEnd('/') + '/' + file.FileName;
                var info = await _fileStore.GetFileInfoAsync(path);

                if (info != null)
                {
                    return("文件名冲突!");
                }
                var stream = file.OpenReadStream();
                var result = await _fileStore.CreateFileFromStreamAsync(path, stream);

                if (string.IsNullOrEmpty(result))
                {
                    return("文件上传失败!");
                }
            }
            return(string.Empty);
        }
Exemplo n.º 6
0
        public async Task FileMD5Test()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddHybridFS_Sqlite();

            var serviceProvider = services.BuildServiceProvider();

            IFileStore store = serviceProvider.GetService <IFileStore>();

            Random rand = new Random();

            var content = new byte[10000];

            rand.NextBytes(content);

            FileEntry entry;
            string    filemd5;

            using (var stream = new MemoryStream(content))
            {
                using (MD5 md5 = MD5.Create())
                {
                    //¿ªÊ¼¼ÓÃÜ
                    byte[] output = md5.ComputeHash(stream);

                    filemd5 = BitConverter.ToString(output).Replace("-", "");
                }

                entry = await store.CreateFileFromStreamAsync(stream);
            }

            Assert.NotEqual(0, entry.Id);

            var file = await store.GetFileEntryAsync(entry.Id);

            Assert.Equal(filemd5, file.MD5);
        }
Exemplo n.º 7
0
        public async Task AddFileEntryTest()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddHybridFS_Sqlite();

            var serviceProvider = services.BuildServiceProvider();

            IFileStore store = serviceProvider.GetService <IFileStore>();

            var       content = new byte[10000];
            FileEntry entry;

            using (var stream = new MemoryStream(content))
            {
                entry = await store.CreateFileFromStreamAsync(stream);
            }

            Assert.NotEqual(0, entry.Id);

            var file = await store.GetFileEntryAsync(entry.Id);

            Assert.Equal(content.Length, file.Length);
        }
Exemplo n.º 8
0
        public async Task AddRandomFileSizeTest()
        {
            ServiceCollection services = new ServiceCollection();

            services.AddHybridFS_Sqlite();

            var serviceProvider = services.BuildServiceProvider();

            IFileStore store = serviceProvider.GetService <IFileStore>();

            Random rd = new Random();

            for (int i = 0; i < 1000; i++)
            {
                var       content = new byte[rd.Next(1, 97152)];
                FileEntry entry;
                using (var stream = new MemoryStream(content))
                {
                    entry = await store.CreateFileFromStreamAsync(stream);
                }
            }

            Assert.True(true);
        }
Exemplo n.º 9
0
 public Task <string> CreateFileFromStreamAsync(string path, Stream inputStream)
 {
     return(_fileStore.CreateFileFromStreamAsync(path, inputStream, true));
 }
Exemplo n.º 10
0
 public Task CreateFileFromStreamAsync(string path, Stream inputStream, bool overwrite = false)
 {
     return(_fileStore.CreateFileFromStreamAsync(path, inputStream, overwrite));
 }