示例#1
0
        public IFormFile[] CheckFiles(IFormFile[] files, string hash, List <EmailAttachment> attachments, string driveName)
        {
            if (files != null)
            {
                var filesList = files.ToList();
                foreach (var attachment in attachments)
                {
                    var fileView = _fileManager.ExportFileFolder(attachment.FileId.ToString(), driveName).Result;
                    //check if file with same hash and email id already exists
                    foreach (var file in files)
                    {
                        hash = GetHash(hash, file);
                        var originalHash = string.Empty;
                        var stream       = IOFile.OpenRead(fileView.StoragePath);
                        using (stream)
                        {
                            IFormFile originalFile = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name));
                            originalHash = GetHash(hash, originalFile);
                            stream.FlushAsync();
                            stream.DisposeAsync();
                            stream.Close();
                        }

                        //if email attachment already exists and hash is the same: remove from files list
                        if (fileView.ContentType == file.ContentType && originalHash == hash && fileView.Size == file.Length)
                        {
                            filesList.Remove(file);
                        }

                        //if email attachment exists but the hash is not the same: update the attachment and file, remove from files list
                        else if (fileView.ContentType == file.ContentType && fileView.Name == file.FileName)
                        {
                            fileView = new FileFolderViewModel()
                            {
                                ContentType = file.ContentType,
                                Files       = new IFormFile[] { file },
                                IsFile      = true,
                                StoragePath = fileView.StoragePath,
                                Name        = file.FileName,
                                Id          = fileView.Id
                            };
                            attachment.SizeInBytes = file.Length;
                            _emailAttachmentRepository.Update(attachment);
                            _fileManager.UpdateFile(fileView);
                            filesList.Remove(file);
                        }
                    }
                }
                //if file doesn't exist, keep it in files list and return files to be attached
                var filesArray = filesList.ToArray();
                return(filesArray);
            }
            else
            {
                return(Array.Empty <IFormFile>());
            }
        }
        public EmailAttachment UpdateAttachment(string id, UpdateEmailAttachmentViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAttachment = _emailAttachmentRepository.GetOne(entityId);

            if (existingAttachment == null)
            {
                throw new EntityOperationException("No file found to update");
            }

            request.DriveId = CheckDriveId(request.DriveId);
            var drive = _storageDriveRepository.GetOne(Guid.Parse(request.DriveId));

            var file = _fileManager.GetFileFolder(existingAttachment.FileId.ToString(), request.DriveId, "Files");

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

            //update email attachment entity
            var    formFile    = request.File;
            string storagePath = Path.Combine(drive.Name, "Email Attachments", formFile.FileName);

            if (!string.IsNullOrEmpty(formFile.FileName))
            {
                existingAttachment.Name = formFile.FileName;
            }

            existingAttachment.ContentType           = formFile.ContentType;
            existingAttachment.SizeInBytes           = formFile.Length;
            existingAttachment.ContentStorageAddress = storagePath;
            _emailAttachmentRepository.Update(existingAttachment);

            //update file entity and file
            file.Files       = new IFormFile[] { formFile };
            file.StoragePath = storagePath;
            var fileView = _fileManager.GetFileFolder(existingAttachment.FileId.ToString(), request.DriveId, "Files");

            file.FullStoragePath = fileView.FullStoragePath;
            _fileManager.UpdateFile(file);

            return(existingAttachment);
        }
示例#3
0
        public IFormFile[] CheckFiles(IFormFile[] files, Guid id, string hash, List <EmailAttachment> attachments)
        {
            if (files != null)
            {
                var filesList = files.ToList();
                foreach (var attachment in attachments)
                {
                    var binaryObject = binaryObjectRepository.GetOne((Guid)attachment.BinaryObjectId);
                    //check if file with same hash and email id already exists
                    foreach (var file in files)
                    {
                        hash = GetHash(hash, file);

                        //if email attachment already exists and hash is the same: remove from files list
                        if (binaryObject.ContentType == file.ContentType && binaryObject.CorrelationEntityId == id && binaryObject.Name == file.FileName && binaryObject.HashCode == hash)
                        {
                            filesList.Remove(file);
                        }
                        //if email attachment exists but the hash is not the same: update the attachment and file, remove from files list
                        else if (binaryObject.ContentType == file.ContentType && binaryObject.CorrelationEntityId == id && binaryObject.Name == file.Name)
                        {
                            emailAttachmentRepository.Update(attachment);
                            var organizationId = binaryObjectManager.GetOrganizationId();
                            binaryObjectManager.Update(file, organizationId, "EmailAPI", (Guid)binaryObject.Id);
                            filesList.Remove(file);
                        }
                    }
                }
                //if file doesn't exist, keep it in files list and return files to be attached
                var filesArray = filesList.ToArray();
                return(filesArray);
            }
            else
            {
                return(Array.Empty <IFormFile>());
            }
        }