Exemplo n.º 1
0
        public async Task <FileInfoVm> SaveFileAsync(long?userId, string filename, string url, long fileSize, byte[] fileHash, string storageType, ImageMetadata imageMetadata = null)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                FileInfo newFile = new FileInfo
                {
                    Id            = RandomExtensions.NextString(64),
                    FileName      = filename,
                    Url           = url,
                    UploaderId    = userId,
                    UploadDate    = DateTime.UtcNow.ToUnixTime(),
                    NodeId        = NodeSettings.Configs.Node.Id,
                    Size          = fileSize,
                    Hash          = fileHash,
                    Storage       = storageType,
                    NumericId     = await poolsService.GetFileIdAsync().ConfigureAwait(false),
                    ImageMetadata = imageMetadata
                };
                await context.FilesInfo.AddAsync(newFile).ConfigureAwait(false);

                await context.SaveChangesAsync().ConfigureAwait(false);

                return(FileInfoConverter.GetFileInfoVm(newFile, false));
            }
        }
Exemplo n.º 2
0
        public async Task <List <FileInfoVm> > SearchAsync(string prefix)
        {
            var files = Directory.GetFiles(DIR_NAME);

            if (!files.IsNullOrEmpty())
            {
                List <FileInfoVm> resultFiles = new List <FileInfoVm>();
                foreach (var fileName in files)
                {
                    try
                    {
                        if (fileName.ToLowerInvariant().Contains(prefix.ToLowerInvariant()))
                        {
                            var fileInfo = await filesService.GetFileInfoAsync(fileName);

                            if (fileInfo != null)
                            {
                                resultFiles.Add(FileInfoConverter.GetFileInfoVm(fileInfo, false));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.WriteLog(ex);
                    }
                }
                return(resultFiles);
            }
            return(new List <FileInfoVm>());
        }
Exemplo n.º 3
0
        public async Task <FileInfoVm> UpdateFileInformationAsync(string fileName, string fileId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                FileInfo fileInfo = await context.FilesInfo.FindAsync(fileId).ConfigureAwait(false);

                fileInfo.Url = fileName;
                context.Update(fileInfo);
                await context.SaveChangesAsync().ConfigureAwait(false);

                return(FileInfoConverter.GetFileInfoVm(fileInfo));
            }
        }
Exemplo n.º 4
0
        public async Task <FileInfoVm> SaveFileAsync(FileInfoVm fileInfo, long userId, string url = null)
        {
            try
            {
                using (MessengerDbContext context = contextFactory.Create())
                {
                    FileInfo newFile = new FileInfo(fileInfo, url, userId);
                    await context.FilesInfo.AddAsync(newFile).ConfigureAwait(false);

                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(FileInfoConverter.GetFileInfoVm(newFile));
                }
            }
            catch (Exception ex)
            {
                throw new SaveFileInformationException("Failed to save file information", ex);
            }
        }
Exemplo n.º 5
0
        public async Task GetFileInfo()
        {
            var uploader = fillTestDbHelper.Users.FirstOrDefault();
            var expected = new FileInfoVm
            {
                Filename   = "Filename",
                Hash       = RandomExtensions.NextBytes(26),
                NumericId  = 0,
                UploaderId = uploader.Id,
                Uploaded   = DateTime.UtcNow.ToUnixTime(),
                Size       = 1024,
                NodeId     = 1
            };

            expected = await filesService.SaveFileAsync(expected, uploader.Id, "ssylca");

            var actual = FileInfoConverter.GetFileInfoVm(await filesService.GetFileInfoAsync(expected.FileId));

            Assert.Equal(expected.ToJson(), actual.ToJson());
        }
Exemplo n.º 6
0
        public async Task <FileInfoVm> LoadFileInfoAsync(string fileId)
        {
            using (MessengerDbContext context = contextFactory.Create())
            {
                var fileInfo = await context.FilesInfo
                               .AsNoTracking()
                               .FirstOrDefaultAsync(file => file.Id == fileId && file.Deleted == false).ConfigureAwait(false);

                if (fileInfo == null)
                {
                    List <FileInfoVm> filesInfo = await _nodeRequestSender.GetFilesInformationAsync(new List <string> {
                        fileId
                    }, null).ConfigureAwait(false);

                    if (!filesInfo.IsNullOrEmpty())
                    {
                        return(filesInfo.FirstOrDefault());
                    }
                }
                return(FileInfoConverter.GetFileInfoVm(fileInfo));
            }
        }
Exemplo n.º 7
0
        public async Task DeleteFiles()
        {
            var uploader = fillTestDbHelper.Users.Skip(1).FirstOrDefault();
            var fileinfo = new FileInfoVm
            {
                Filename   = "Filename",
                Hash       = RandomExtensions.NextBytes(26),
                NumericId  = 0,
                UploaderId = uploader.Id,
                Uploaded   = DateTime.UtcNow.ToUnixTime(),
                Size       = 1024,
                NodeId     = 1
            };

            fileinfo = await filesService.SaveFileAsync(fileinfo, uploader.Id, "ssylca");

            var expected = FileInfoConverter.GetFileInfoVm(await filesService.GetFileInfoAsync(fileinfo.FileId));
            await filesService.DeleteFilesAsync(new List <string> {
                expected.FileId
            }, uploader.Id);

            Assert.Null(fillTestDbHelper.Files.FirstOrDefault(opt => opt.Id == expected.FileId && !opt.Deleted));
        }