public NewFilesNodeNotice(FileInfoVm file, byte[] privateData, long keyId) { FilesValuePairs = new List <ValuePair <FileInfoVm, byte[]> > { new ValuePair <FileInfoVm, byte[]>(file, privateData) }; KeyId = keyId; NoticeCode = Enums.NodeNoticeCode.NewFiles; }
public async void SendNewFilesNodeNoticeAsync(FileInfoVm fileInfo, byte[] privateData, long keyId) { try { NewFilesNodeNotice notice = new NewFilesNodeNotice(fileInfo, privateData, keyId); await SendNoticeToNodesAsync(notice).ConfigureAwait(false); } catch (Exception ex) { Logger.WriteLog(ex); } }
public FileInfo(FileInfoVm fileInfo, string url, long uploaderId) { Id = fileInfo.FileId ?? RandomExtensions.NextString(64); FileName = fileInfo.Filename; Url = url; UploaderId = fileInfo.UploaderId.GetValueOrDefault(); UploadDate = fileInfo.Uploaded.GetValueOrDefault(DateTime.UtcNow.ToUnixTime()); NodeId = fileInfo.NodeId.GetValueOrDefault(); Size = fileInfo.Size; Hash = fileInfo.Hash; NumericId = fileInfo.NumericId.GetValueOrDefault(); ImageMetadata = fileInfo.ImageMetadata; }
public static FileInfo GetFileInfo(FileInfoVm file) { return(new FileInfo { FileName = file.Filename, Id = file.FileId, Hash = file.Hash, NodeId = file.NodeId.GetValueOrDefault(), NumericId = file.NumericId.GetValueOrDefault(), Size = file.Size, UploadDate = file.Uploaded.GetValueOrDefault(), UploaderId = file.UploaderId, ImageMetadata = file.ImageMetadata }); }
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 SaveFileInfo() { 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 }; var actual = await filesService.SaveFileAsync(expected, uploader.Id, "ssylca"); Assert.True(expected.Hash.SequenceEqual(actual.Hash) && expected.Filename == actual.Filename && expected.Uploaded == actual.Uploaded && expected.UploaderId == actual.UploaderId && expected.NumericId == actual.NumericId); }
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)); }
private async Task DownloadMediaAttachmentAsync(AttachmentVm attachment, NodeConnection connection) { FileInfoVm fileInfoPayload = (FileInfoVm)attachment.Payload; if (fileInfoPayload == null) { return; } string fileId = fileInfoPayload.FileId; FileInfo fileInfo = await _filesService.GetFileInfoAsync(fileId).ConfigureAwait(false); if (fileInfo != null && string.IsNullOrWhiteSpace(fileInfo.Url)) { try { await _nodeRequestSender.DownloadFileNodeRequestAsync(fileId, connection).ConfigureAwait(false); } catch (DownloadFileException ex) { Logger.WriteLog(ex); } } }
public async Task <IActionResult> Post([FromForm] IFormFile file, [FromQuery] bool isDocument = false) { try { if (file == null || file.Length == 0) { return(BadRequest()); } string fileAccessToken = HttpContext.Request.Headers["FileAccessToken"]; if (string.IsNullOrWhiteSpace(fileAccessToken)) { return(BadRequest()); } ClientConnection uploaderClient = connectionsService.GetClientConnections() .FirstOrDefault(opt => opt.FileAccessToken == fileAccessToken); if (uploaderClient == null) { return(BadRequest()); } if (uploaderClient.Banned == true || uploaderClient.Confirmed == false) { return(Forbid()); } Stream uploadStream; ImageMetadata imageMetadata = null; if (!isDocument && file.TryGetImage(out Image <Rgba32> imageFile, out var imageFormat)) { if (imageFile.Height > 800 || imageFile.Width > 800) { imageFile.Mutate(opt => { opt.Resize(new ResizeOptions { Mode = ResizeMode.Max, Size = new SixLabors.Primitives.Size(800) }); }); } if (!Directory.Exists("tmp")) { Directory.CreateDirectory("tmp"); } var tempFileName = $"{Guid.NewGuid()}.{imageFormat.Name}"; uploadStream = new FileStream($"tmp/{tempFileName}", FileMode.OpenOrCreate, FileAccess.ReadWrite); imageFile.Save(uploadStream, imageFormat); imageMetadata = new ImageMetadata { Format = imageFormat.Name, Height = imageFile.Height, Width = imageFile.Width }; } else { uploadStream = file.OpenReadStream(); } string storageFileName = $"[{RandomExtensions.NextString(10)}]{file.FileName}"; SHA256 sha256 = SHA256.Create(); var fileHash = sha256.ComputeHash(uploadStream); FileInfoVm fileInfo = null; uploadStream.Position = 0; string fileUrl; string uploadFileName; if (fileStorage.GetType() != typeof(LocalFileStorage)) { fileUrl = string.Empty; } else { fileUrl = Path.Combine(NodeSettings.LOCAL_FILE_STORAGE_PATH, storageFileName); } fileInfo = await filesService.SaveFileAsync( uploaderClient.UserId.GetValueOrDefault(), file.FileName, fileUrl, file.Length, fileHash, fileStorage.StorageName, imageMetadata).ConfigureAwait(false); if (fileStorage.GetType() != typeof(LocalFileStorage)) { uploadFileName = fileInfo.FileId; } else { uploadFileName = storageFileName; } await fileStorage.UploadAsync(uploadStream, uploadFileName).ConfigureAwait(false); var segment = await BlockSegmentsService.Instance.CreateNewFileSegmentAsync( fileInfo, NodeSettings.Configs.Node.Id, NodeData.Instance.NodeKeys.PrivateKey, NodeData.Instance.NodeKeys.SymmetricKey, NodeData.Instance.NodeKeys.Password, NodeData.Instance.NodeKeys.KeyId).ConfigureAwait(false); BlockGenerationHelper.Instance.AddSegment(segment); nodeNoticeService.SendNewFilesNodeNoticeAsync(fileInfo, segment.PrivateData, NodeData.Instance.NodeKeys.KeyId); FileResponse response = new FileResponse(0, fileInfo, null); sha256.Dispose(); uploadStream.Dispose(); return(Content(ObjectSerializer.ObjectToJson(response), MediaTypeNames.Application.Json)); } catch (DownloadFileException ex) { Logger.WriteLog(ex); if (ex.InnerException is WebException webException) { return(StatusCode(StatusCodes.Status503ServiceUnavailable)); } return(StatusCode(StatusCodes.Status500InternalServerError)); } catch (Exception ex) { Logger.WriteLog(ex); return(StatusCode(StatusCodes.Status500InternalServerError)); } }