public void DeleteOne(QueueItemAttachment attachment, QueueItem queueItem, string driveId)
        {
            if (attachment != null)
            {
                if (string.IsNullOrEmpty(driveId))
                {
                    var fileToDelete = _storageFileRepository.GetOne(attachment.FileId);
                    driveId = fileToDelete.StorageDriveId.ToString();
                }

                var fileView = _fileManager.DeleteFileFolder(attachment.FileId.ToString(), driveId, "Files");
                var folder   = _fileManager.GetFileFolder(fileView.ParentId.ToString(), driveId, "Folders");
                if (!folder.HasChild.Value)
                {
                    _fileManager.DeleteFileFolder(folder.Id.ToString(), driveId, "Folders");
                }
                else
                {
                    _fileManager.RemoveBytesFromFoldersAndDrive(new List <FileFolderViewModel> {
                        fileView
                    });
                }

                //update queue item payload
                queueItem.PayloadSizeInBytes -= attachment.SizeInBytes;
                _repo.Update(queueItem);
            }
            else
            {
                throw new EntityDoesNotExistException("Attachment could not be found");
            }
        }
Exemplo n.º 2
0
        public void DeleteOne(QueueItemAttachment attachment, QueueItem queueItem, string driveName)
        {
            if (attachment != null)
            {
                var fileView = _fileManager.DeleteFileFolder(attachment.FileId.ToString(), driveName);
                var folder   = _fileManager.GetFileFolder(fileView.ParentId.ToString(), driveName);
                if (!folder.HasChild.Value)
                {
                    _fileManager.DeleteFileFolder(folder.Id.ToString(), driveName);
                }
                else
                {
                    _fileManager.AddBytesToFoldersAndDrive(new List <FileFolderViewModel> {
                        fileView
                    });
                }

                //update queue item payload
                queueItem.PayloadSizeInBytes -= attachment.SizeInBytes;
                _repo.Update(queueItem);
            }
            else
            {
                throw new EntityDoesNotExistException("Attachment could not be found");
            }
        }
Exemplo n.º 3
0
        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> AddNewAttachments(QueueItem queueItem, IFormFile[] files, string driveId)
        {
            if (files.Length == 0 || files == null)
            {
                throw new EntityOperationException("No files found to attach");
            }

            driveId = CheckDriveId(driveId);
            var drive = GetDrive(driveId);

            driveId = drive.Id.ToString();

            //add files to drive
            string storagePath = Path.Combine(drive.Name, "Queue Item Attachments", queueItem.Id.ToString());
            var    fileView    = new FileFolderViewModel()
            {
                StoragePath     = storagePath,
                FullStoragePath = storagePath,
                Files           = files,
                IsFile          = true
            };

            long?size = 0;

            foreach (var file in files)
            {
                size += file.Length;
            }

            CheckStoragePathExists(fileView, size, queueItem.Id, driveId, drive.Name);
            var  fileViewList         = _fileManager.AddFileFolder(fileView, driveId);
            var  queueItemAttachments = new List <QueueItemAttachment>();
            long payloadSizeInBytes   = 0;

            foreach (var file in fileViewList)
            {
                //create queue item attachment
                QueueItemAttachment queueItemAttachment = new QueueItemAttachment()
                {
                    FileId      = file.Id.Value,
                    CreatedOn   = DateTime.UtcNow,
                    CreatedBy   = _httpContextAccessor.HttpContext.User.Identity.Name,
                    QueueItemId = queueItem.Id.Value,
                    SizeInBytes = file.Size.Value,
                };
                _queueItemAttachmentRepository.Add(queueItemAttachment);
                queueItemAttachments.Add(queueItemAttachment);

                payloadSizeInBytes           += queueItemAttachment.SizeInBytes;
                queueItem.PayloadSizeInBytes += payloadSizeInBytes;
                _repo.Update(queueItem);
            }

            return(queueItemAttachments);
        }
Exemplo n.º 5
0
        public List <QueueItemAttachment> AddNewAttachments(QueueItem queueItem, IFormFile[] files, string driveName)
        {
            if (files.Length == 0 || files == null)
            {
                throw new EntityOperationException("No files found to attach");
            }

            if (driveName != "Files" && !string.IsNullOrEmpty(driveName))
            {
                throw new EntityOperationException("Component files can only be saved in the Files drive");
            }
            else if (string.IsNullOrEmpty(driveName))
            {
                driveName = "Files";
            }

            //add files to drive
            string storagePath = Path.Combine(driveName, "Queue Item Attachments", queueItem.Id.ToString());
            var    fileView    = new FileFolderViewModel()
            {
                StoragePath     = storagePath,
                FullStoragePath = storagePath,
                Files           = files,
                IsFile          = true
            };

            long?size = 0;

            foreach (var file in files)
            {
                size += file.Length;
            }

            CheckStoragePathExists(fileView, size, queueItem.Id, driveName);
            var fileViewList         = _fileManager.AddFileFolder(fileView, driveName);
            var queueItemAttachments = new List <QueueItemAttachment>();

            foreach (var file in fileViewList)
            {
                //create queue item attachment
                QueueItemAttachment queueItemAttachment = new QueueItemAttachment()
                {
                    FileId      = file.Id.Value,
                    CreatedOn   = DateTime.UtcNow,
                    CreatedBy   = _httpContextAccessor.HttpContext.User.Identity.Name,
                    QueueItemId = queueItem.Id.Value,
                    SizeInBytes = file.Size.Value,
                };
                _queueItemAttachmentRepository.Add(queueItemAttachment);
                queueItemAttachments.Add(queueItemAttachment);
            }

            return(queueItemAttachments);
        }
Exemplo n.º 6
0
        public static void DownloadFile(UserInfo userInfo, QueueItemAttachment attachment, string directoryPath, int count = 0)
        {
            var apiInstance = GetAttachmentsApiInstance(userInfo.Token, userInfo.ServerUrl);

            try
            {
                var    response = apiInstance.ExportQueueItemAttachmentAsyncWithHttpInfo(attachment.Id.ToString(), userInfo.ApiVersion, userInfo.OrganizationId, attachment.QueueItemId.ToString()).Result;
                string value;
                var    headers = response.Headers.TryGetValue("Content-Disposition", out value);
                string fileName;
                if (headers == true)
                {
                    string[] valueArray    = value.Split('=');
                    string[] fileNameArray = valueArray[1].Split(';');
                    fileName = fileNameArray[0];
                }
                else
                {
                    var fileId          = attachment.FileId;
                    var fileApiInstance = new FilesApi(userInfo.ServerUrl);
                    fileApiInstance.Configuration.AccessToken = userInfo.Token;
                    var driveApiInstance = new DrivesApi(userInfo.ServerUrl);
                    driveApiInstance.Configuration.AccessToken = userInfo.Token;
                    string filter        = "IsDefault eq true";
                    var    driveResponse = driveApiInstance.ApiVapiVersionStorageDrivesGetAsyncWithHttpInfo(userInfo.ApiVersion, userInfo.OrganizationId, filter).Result.Data.Items.FirstOrDefault();
                    var    fileResponse  = fileApiInstance.GetFileFolderAsyncWithHttpInfo(attachment.FileId.ToString(), userInfo.ApiVersion, userInfo.OrganizationId, driveResponse.Id.ToString()).Result.Data;
                    fileName = fileResponse.Name;
                }
                var    data      = response.Data;
                byte[] fileArray = data.ToArray();
                System.IO.File.WriteAllBytes(SystemFile.Path.Combine(directoryPath, fileName), fileArray);
            }
            catch (Exception ex)
            {
                if (ex.GetType().GetProperty("ErrorCode").GetValue(ex, null).ToString() == "401" && count < 2)
                {
                    UtilityMethods.RefreshToken(userInfo);
                    count++;
                    DownloadFile(userInfo, attachment, directoryPath, count);
                }
                else if (ex.Message != "One or more errors occurred.")
                {
                    throw new InvalidOperationException("Exception when calling QueueItemAttachmentsApi.ExportQueueItemAttachment: " + ex.Message);
                }
                else
                {
                    throw new InvalidOperationException(ex.InnerException.Message);
                }
            }
        }
Exemplo n.º 7
0
        public static void DownloadFile(RestClient client, QueueItemAttachment attachment, string directoryPath)
        {
            var file    = FileMethods.GetFile(client, attachment.FileId);
            var request = new RestRequest($"api/v1/QueueItems/{attachment.QueueItemId}/QueueItemAttachments/{attachment.Id}/Export", Method.GET);

            request.RequestFormat = DataFormat.Json;

            var response = client.Execute(request);

            if (!response.IsSuccessful)
            {
                throw new HttpRequestException($"Status Code: {response.StatusCode} - Error Message: {response.ErrorMessage}");
            }

            byte[] fileBytes = response.RawBytes;
            IOFile.WriteAllBytes(Path.Combine(directoryPath, file.Name), fileBytes);
        }
Exemplo n.º 8
0
        public async Task <FileFolderViewModel> Export(string id, string driveName)
        {
            Guid attachmentId;

            Guid.TryParse(id, out attachmentId);

            QueueItemAttachment attachment = _queueItemAttachmentRepository.GetOne(attachmentId);

            if (attachment == null || attachment.FileId == null || attachment.FileId == Guid.Empty)
            {
                throw new EntityDoesNotExistException($"Queue item attachment with id {id} could not be found or doesn't exist");
            }

            var response = await _fileManager.ExportFileFolder(attachment.FileId.ToString(), driveName);

            return(response);
        }
        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);
        }
Exemplo n.º 10
0
        public static void DownloadFile(string token, string serverUrl, string organizationId, QueueItemAttachment attachment, string directoryPath, string apiVersion)
        {
            var apiInstance = GetAttachmentsApiInstance(token, serverUrl);

            try
            {
                var    response = apiInstance.ExportQueueItemAttachmentAsyncWithHttpInfo(attachment.Id.ToString(), apiVersion, organizationId, attachment.QueueItemId.ToString()).Result;
                string value;
                var    headers = response.Headers.TryGetValue("Content-Disposition", out value);
                string fileName;
                if (headers == true)
                {
                    string[] valueArray    = value.Split('=');
                    string[] fileNameArray = valueArray[1].Split(';');
                    fileName = fileNameArray[0];
                }
                else
                {
                    var fileId          = attachment.FileId;
                    var fileApiInstance = new FilesApi(serverUrl);
                    fileApiInstance.Configuration.AccessToken = token;
                    var driveApiInstance = new DrivesApi(serverUrl);
                    driveApiInstance.Configuration.AccessToken = token;
                    string filter        = "IsDefault eq true";
                    var    driveResponse = driveApiInstance.ApiVapiVersionStorageDrivesGetAsyncWithHttpInfo(apiVersion, organizationId, filter).Result.Data.Items.FirstOrDefault();
                    var    fileResponse  = fileApiInstance.GetFileFolderAsyncWithHttpInfo(attachment.FileId.ToString(), apiVersion, organizationId, driveResponse.Id.ToString()).Result.Data;
                    fileName = fileResponse.Name;
                }
                var    data      = response.Data;
                byte[] fileArray = data.ToArray();
                System.IO.File.WriteAllBytes(SystemFile.Path.Combine(directoryPath, fileName), fileArray);
            }
            catch (Exception ex)
            {
                if (ex.Message != "One or more errors occurred.")
                {
                    throw new InvalidOperationException("Exception when calling QueueItemAttachmentsApi.ExportQueueItemAttachment: " + ex.Message);
                }
                else
                {
                    throw new InvalidOperationException(ex.InnerException.Message);
                }
            }
        }