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)); } }
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>()); }
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)); } }
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); } }
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()); }
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)); } }
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)); }