public List <BinaryObject> AttachFiles(List <IFormFile> files, Guid queueItemId, QueueItem queueItem) { var binaryObjects = new List <BinaryObject>(); long payload = 0; if (files.Count != 0 || files != null) { foreach (var file in files) { if (file == null) { throw new FileNotFoundException("No file attached"); } long size = file.Length; if (size <= 0) { throw new InvalidDataException($"File size of file {file.FileName} cannot be 0"); } string organizationId = binaryObjectManager.GetOrganizationId(); string apiComponent = "QueueItemAPI"; //create binary object BinaryObject binaryObject = new BinaryObject() { Name = file.FileName, Folder = apiComponent, CreatedOn = DateTime.UtcNow, CreatedBy = httpContextAccessor.HttpContext.User.Identity.Name, CorrelationEntityId = queueItemId }; string filePath = Path.Combine("BinaryObjects", organizationId, apiComponent, binaryObject.Id.ToString()); //upload file to the Server binaryObjectManager.Upload(file, organizationId, apiComponent, binaryObject.Id.ToString()); binaryObjectManager.SaveEntity(file, filePath, binaryObject, apiComponent, organizationId); binaryObjectRepository.Add(binaryObject); //create queue item attachment QueueItemAttachment attachment = new QueueItemAttachment() { BinaryObjectId = (Guid)binaryObject.Id, QueueItemId = queueItemId, CreatedBy = httpContextAccessor.HttpContext.User.Identity.Name, CreatedOn = DateTime.UtcNow, SizeInBytes = file.Length }; queueItemAttachmentRepository.Add(attachment); binaryObjects.Add(binaryObject); payload += attachment.SizeInBytes; } } //update queue item payload queueItem.PayloadSizeInBytes += payload; repo.Update(queueItem); return(binaryObjects); }
public List <QueueItemAttachment> AddFileAttachments(QueueItem queueItem, string[] requests, string driveId) { if (requests.Length == 0 || requests == null) { throw new EntityOperationException("No files found to attach"); } var drive = GetDrive(driveId); driveId = drive.Id.ToString(); long?payload = 0; var queueItemAttachments = new List <QueueItemAttachment>(); var files = new List <FileFolderViewModel>(); foreach (var request in requests) { var file = _fileManager.GetFileFolder(request, driveId, "Files"); var fileToAdd = _storageFileRepository.GetOne(file.Id.Value); if (file == null) { throw new EntityDoesNotExistException($"File could not be found"); } long?size = file.Size; if (size <= 0) { throw new EntityOperationException($"File size of file {file.Name} cannot be 0"); } //create queue item attachment file under queue item id folder var path = Path.Combine(drive.Name, "Queue Item Attachments", queueItem.Id.ToString()); using (var stream = IOFile.OpenRead(fileToAdd.StorageLocation)) { file.Files = new IFormFile[] { new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) }; file.StoragePath = path; file.FullStoragePath = path; CheckStoragePathExists(file, 0, queueItem.Id, driveId, drive.Name); file = _fileManager.AddFileFolder(file, driveId)[0]; files.Add(file); } //create queue item attachment QueueItemAttachment queueItemAttachment = new QueueItemAttachment() { FileId = file.Id.Value, QueueItemId = queueItem.Id.Value, CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name, CreatedOn = DateTime.UtcNow, SizeInBytes = file.Size.Value }; _queueItemAttachmentRepository.Add(queueItemAttachment); payload += queueItemAttachment.SizeInBytes; queueItemAttachments.Add(queueItemAttachment); } _fileManager.AddBytesToFoldersAndDrive(files); //update queue item payload queueItem.PayloadSizeInBytes += payload.Value; _repo.Update(queueItem); return(queueItemAttachments); }