public async Task <IActionResult> Rename(string id, [FromBody] FileFolderViewModel request, string driveName = null) { try { var file = _manager.GetFileFolder(id, driveName); if (file.StoragePath.Contains("Assets") || file.StoragePath.Contains("Automations") || file.StoragePath.Contains("Email Attachments") || file.StoragePath.Contains("Queue Item Attachments")) { throw new EntityOperationException("Component files and folders cannot be added or changed in File Manager"); } if (string.IsNullOrEmpty(request.Name)) { ModelState.AddModelError("Rename", "No name given in request"); return(BadRequest(ModelState)); } string name = request.Name; var response = _manager.RenameFileFolder(id, name, driveName); return(Ok(response)); } catch (Exception ex) { return(ex.GetActionResult()); } }
public void DeleteAll(string emailId, string driveId) { var attachments = _emailAttachmentRepository.Find(null, q => q.EmailId == Guid.Parse(emailId))?.Items; if (attachments.Count != 0) { if (string.IsNullOrEmpty(driveId)) { var fileToDelete = _storageFileRepository.GetOne(attachments[0].FileId.Value); driveId = fileToDelete.StorageDriveId.ToString(); } var fileView = new FileFolderViewModel(); foreach (var attachment in attachments) { fileView = _fileManager.DeleteFileFolder(attachment.FileId.ToString(), driveId, "Files"); _emailAttachmentRepository.SoftDelete(attachment.Id.Value); } 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 }); } } else { throw new EntityDoesNotExistException("No attachments found to delete"); } }
public FileFolderViewModel DeleteFileFolder(string id, string driveName = null) { FileFolderViewModel fileFolder = new FileFolderViewModel(); string adapter = GetAdapterType(driveName); if (adapter.Equals(AdapterType.LocalFileStorage.ToString())) { fileFolder = _localFileStorageAdapter.DeleteFileFolder(id, driveName); } //else if (adapter.Equals("AzureBlobStorageAdapter") && storageProvider.Equals("FileSystem.Azure")) // azureBlobStorageAdapter.SaveFile(request); //else if (adapter.Equals("AmazonEC2StorageAdapter") && storageProvider.Equals("FileSystem.Amazon")) // amazonEC2StorageAdapter.SaveFile(request); //else if (adapter.Equals("GoogleBlobStorageAdapter") && storageProvider.Equals("FileSystem.Google")) // googleBlobStorageAdapter.SaveFile(request); else { throw new EntityOperationException("Configuration is not set up for local file storage"); } if (fileFolder.Size > 0 && (fileFolder.IsFile == true && !fileFolder.StoragePath.Contains("Queue Item Attachments") && !fileFolder.StoragePath.Contains("Email Attachments") && !fileFolder.StoragePath.Contains("Automations") && !fileFolder.StoragePath.Contains("Assets") || fileFolder.IsFile == false && (fileFolder.StoragePath.Contains("Automations") || fileFolder.StoragePath.Contains("Assets") || fileFolder.StoragePath.Contains("Email Attachments") || fileFolder.StoragePath.Contains("Queue Item Attachments")))) { AddBytesToFoldersAndDrive(new List <FileFolderViewModel> { fileFolder }); } return(fileFolder); }
public void DeleteQueueItem(QueueItem existingQueueItem, string driveName) { var attachments = _queueItemAttachmentRepository.Find(null, q => q.QueueItemId == existingQueueItem.Id)?.Items; if (attachments.Count != 0) { var fileView = new FileFolderViewModel(); foreach (var attachment in attachments) { fileView = _fileManager.DeleteFileFolder(attachment.FileId.ToString(), driveName); _queueItemAttachmentRepository.SoftDelete(attachment.Id.Value); } 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 }); } } else { throw new EntityDoesNotExistException("No attachments found to delete"); } }
public async Task <IActionResult> Post([FromForm] FileFolderViewModel request, string driveId, string type) { try { if (request.StoragePath.Contains("Assets") || request.StoragePath.Contains("Automations") || request.StoragePath.Contains("Email Attachments") || request.StoragePath.Contains("Queue Item Attachments")) { throw new EntityOperationException("Component files and folders cannot be added or changed in File Manager"); } if (request.IsFile == null && type == "Files") { request.IsFile = true; } else if (request.IsFile == null && type == "Folders") { request.IsFile = false; } var response = _manager.AddFileFolder(request, driveId); return(Ok(response)); } catch (Exception ex) { return(ex.GetActionResult()); } }
public List <EmailAttachment> AddAttachments(IFormFile[] files, Guid id, string driveName) { 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"; } var attachments = new List <EmailAttachment>(); if (files?.Length != 0 && files != null) { //add files to drive string storagePath = Path.Combine(driveName, "Email Attachments", 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, id, driveName); var fileViewList = _fileManager.AddFileFolder(fileView, driveName); foreach (var file in fileViewList) { //create email attachment EmailAttachment emailAttachment = new EmailAttachment() { Name = file.Name, FileId = file.Id, ContentType = file.ContentType, ContentStorageAddress = file.FullStoragePath, SizeInBytes = file.Size, EmailId = id, CreatedOn = DateTime.UtcNow, CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name }; _emailAttachmentRepository.Add(emailAttachment); attachments.Add(emailAttachment); } } else { throw new EntityOperationException("No files found to attach"); } return(attachments); }
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 List <EmailAttachment> AddAttachments(IFormFile[] files, Guid id, string driveId) { driveId = CheckDriveId(driveId); var drive = GetDrive(driveId); driveId = drive.Id.ToString(); var attachments = new List <EmailAttachment>(); if (files?.Length != 0 && files != null) { //add files to drive string storagePath = Path.Combine(drive.Name, "Email Attachments", 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, id, driveId, drive.Name); var fileViewList = _fileManager.AddFileFolder(fileView, driveId); foreach (var file in fileViewList) { string[] fileNameArray = file.Name.Split("."); string extension = fileNameArray[1]; //create email attachment EmailAttachment emailAttachment = new EmailAttachment() { Name = file.Name, FileId = file.Id, ContentType = file.ContentType, ContentStorageAddress = Path.Combine(drive.OrganizationId.ToString(), drive.Id.ToString(), $"{file.Id}.{extension}"), SizeInBytes = file.Size, EmailId = id, CreatedOn = DateTime.UtcNow, CreatedBy = _httpContextAccessor.HttpContext.User.Identity.Name }; _emailAttachmentRepository.Add(emailAttachment); attachments.Add(emailAttachment); } } else { throw new EntityOperationException("No files found to attach"); } return(attachments); }
public IFormFile[] CheckFiles(IFormFile[] files, string hash, List <QueueItemAttachment> attachments, string driveId) { if (files != null) { var filesList = files.ToList(); if (string.IsNullOrEmpty(driveId)) { var fileToCheck = _storageFileRepository.GetOne(attachments[0].FileId); var drive = _storageDriveRepository.GetOne(fileToCheck.StorageDriveId.Value); driveId = drive.Id.ToString(); } foreach (var attachment in attachments) { var fileView = _fileManager.GetFileFolder(attachment.FileId.ToString(), driveId, "Files"); var originalHash = fileView.Hash; //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 (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; _queueItemAttachmentRepository.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 Asset CreateAsset(Asset asset, IFormFile file, string driveName) { AssetNameAvailability(asset); if (asset.Type == "Text") { asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.TextValue); } else if (asset.Type == "Number") { asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.NumberValue.ToString()); } else if (asset.Type == "Json") { asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.JsonValue); } else if (asset.Type == "File") { 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"; } if (file != null) { IFormFile[] fileArray = { file }; asset.Id = Guid.NewGuid(); var fileView = new FileFolderViewModel() { ContentType = file.ContentType, Files = fileArray, StoragePath = Path.Combine(driveName, "Assets", asset.Id.ToString()), IsFile = true }; var request = new AgentAssetViewModel(); request = request.Map(asset, file, driveName); CheckStoragePathExists(fileView, request); fileView = _fileManager.AddFileFolder(fileView, driveName)[0]; asset.FileId = fileView.Id; asset.SizeInBytes = file.Length; } else { throw new EntityDoesNotExistException("File does not exist"); } } return(asset); }
public void CheckStoragePathExists(FileFolderViewModel view, AutomationViewModel request, string driveName) { //check if storage path exists; if it doesn't exist, create folder var folder = _fileManager.GetFileFolderByStoragePath(view.StoragePath, driveName); CreateFolderIfEmpty(folder, request, "Automations", view.StoragePath); folder = _fileManager.GetFileFolderByStoragePath(view.FullStoragePath, driveName); folder.StoragePath = view.StoragePath; CreateFolderIfEmpty(folder, request, request.Id.ToString(), view.StoragePath); }
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); }
private void CreateFolderIfEmpty(FileFolderViewModel folder, AutomationViewModel request, string folderName, string storagePath) { if (string.IsNullOrEmpty(folder.Name)) { folder.Name = folderName; folder.IsFile = false; folder.Size = request.File.Length; folder.StoragePath = storagePath; _fileManager.AddFileFolder(folder, request.DriveId); } }
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); }
public Automation AddAutomation(AutomationViewModel request) { var drive = new StorageDrive(); if (string.IsNullOrEmpty(request.DriveId)) { drive = _storageDriveRepository.Find(null, q => q.IsDefault == true).Items?.FirstOrDefault(); if (drive == null) { throw new EntityDoesNotExistException("Default drive does not exist or could not be found"); } else { request.DriveId = drive.Id.ToString(); } } else { drive = _storageDriveRepository.GetOne(Guid.Parse(request.DriveId)); } IFormFile[] fileArray = { request.File }; string shortPath = Path.Combine(drive.Name, "Automations"); string path = Path.Combine(shortPath, request.Id.ToString()); var fileView = new FileFolderViewModel() { Files = fileArray, StoragePath = shortPath, FullStoragePath = path, ContentType = fileArray[0].ContentType, IsFile = true }; CheckStoragePathExists(fileView, request, drive.Name); fileView.StoragePath = fileView.FullStoragePath; fileView = _fileManager.AddFileFolder(fileView, request.DriveId)[0]; var automationEngine = GetAutomationEngine(request.AutomationEngine); var automation = new Automation() { Name = request.Name, AutomationEngine = automationEngine, Id = request.Id, FileId = fileView.Id, OriginalPackageName = request.File.FileName }; AddAutomationVersion(request); return(automation); }
public void DeleteQueueItem(QueueItem existingQueueItem, string driveId) { if (existingQueueItem == null) { throw new EntityDoesNotExistException("Queue item does not exist or you do not have authorized access."); } UpdateItemsStates(existingQueueItem.QueueId.ToString()); //soft delete each queue item attachment entity and file entity that correlates to the queue item var attachments = _queueItemAttachmentRepository.Find(null, q => q.QueueItemId == existingQueueItem.Id)?.Items; if (attachments.Count != 0) { var fileView = new FileFolderViewModel(); if (string.IsNullOrEmpty(driveId)) { if (attachments.Count() > 0) { var fileToDelete = _storageFileRepository.GetOne(attachments[0].FileId); driveId = fileToDelete.StorageDriveId.ToString(); } else { var drive = _storageDriveRepository.Find(null, q => q.IsDefault == true).Items?.FirstOrDefault(); driveId = drive.Id.ToString(); } } foreach (var attachment in attachments) { fileView = _fileManager.DeleteFileFolder(attachment.FileId.ToString(), driveId, "Files"); _queueItemAttachmentRepository.SoftDelete(attachment.Id.Value); } 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 }); } } else { throw new EntityDoesNotExistException("No attachments found to delete"); } }
public FileFolderViewModel CheckStoragePathExists(FileFolderViewModel view, AgentAssetViewModel request) { //check if storage path exists; if it doesn't exist, create folder var folder = _fileManager.GetFileFolderByStoragePath(view.StoragePath, request.DriveName); if (folder.Name == null) { folder.Name = request.AgentId.ToString(); folder.StoragePath = Path.Combine(request.DriveName, "Assets"); folder.IsFile = false; folder.Size = request.File.Length; folder = _fileManager.AddFileFolder(folder, request.DriveName)[0]; } return(folder); }
public FileFolderViewModel CheckStoragePathExists(FileFolderViewModel view, long?size, Guid?id, string driveName) { //check if storage path exists; if it doesn't exist, create folder var folder = _fileManager.GetFileFolderByStoragePath(view.FullStoragePath, driveName); if (folder.Name == null) { folder.Name = id.ToString(); folder.StoragePath = Path.Combine(driveName, "Email Attachments"); folder.IsFile = false; folder.Size = size; folder = _fileManager.AddFileFolder(folder, driveName)[0]; } return(folder); }
public FileFolderViewModel GetFileFolderByStoragePath(string storagePath, string driveName = null) { var response = new FileFolderViewModel(); string adapter = GetAdapterType(driveName); if (adapter.Equals(AdapterType.LocalFileStorage.ToString())) { response = _localFileStorageAdapter.GetFileFolderByStoragePath(storagePath, driveName); } else { throw new EntityOperationException("Configuration is not set up for local file storage"); } return(response); }
public void UpdateFile(FileFolderViewModel request) { string adapter = GetAdapterType("Files"); if (adapter.Equals(AdapterType.LocalFileStorage.ToString())) { _localFileStorageAdapter.UpdateFile(request); } //else if (adapter.Equals("AzureBlobStorageAdapter") && storageProvider.Equals("FileSystem.Azure")) // azureBlobStorageAdapter.SaveFile(request); //else if (adapter.Equals("AmazonEC2StorageAdapter") && storageProvider.Equals("FileSystem.Amazon")) // amazonEC2StorageAdapter.SaveFile(request); //else if (adapter.Equals("GoogleBlobStorageAdapter") && storageProvider.Equals("FileSystem.Google")) // googleBlobStorageAdapter.SaveFile(request); else { throw new EntityOperationException("Configuration is not set up for local file storage"); } }
public Automation AddAutomation(AutomationViewModel request) { if (request.DriveName != "Files" && !string.IsNullOrEmpty(request.DriveName)) { throw new EntityOperationException("Component files can only be saved in the Files drive"); } else if (string.IsNullOrEmpty(request.DriveName)) { request.DriveName = "Files"; } IFormFile[] fileArray = { request.File }; string path = Path.Combine(request.DriveName, "Automations", request.Id.ToString()); var fileView = new FileFolderViewModel() { Files = fileArray, StoragePath = path, FullStoragePath = path, ContentType = fileArray[0].ContentType, IsFile = true }; CheckStoragePathExists(fileView, request); fileView = _fileManager.AddFileFolder(fileView, request.DriveName)[0]; var automationEngine = GetAutomationEngine(request.AutomationEngine); var automation = new Automation() { Name = request.Name, AutomationEngine = automationEngine, Id = request.Id, FileId = fileView.Id, OriginalPackageName = request.File.FileName }; AddAutomationVersion(request); return(automation); }
private void SaveNode(FileFolderViewModel node, string path) { path = Path.Combine(path, node.Name); if (node.Children == null) { System.IO.File.WriteAllText(path, node.Content ?? string.Empty, Encoding.UTF8); } else { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } foreach (var child in node.Children) { SaveNode(child, path); } } }
private List <FileFolderViewModel> GetFolders(SiteDb siteDb, string path) { var SubFolders = siteDb.Folders.GetSubFolders(path, ConstObjectType.CmsFile); List <FileFolderViewModel> Result = new List <FileFolderViewModel>(); foreach (var item in SubFolders) { FileFolderViewModel model = new FileFolderViewModel(); // model.Id = path; model.Name = item.Segment; model.FullPath = item.FullPath; model.Count = siteDb.Folders.GetFolderObjects <CmsFile>(item.FullPath, true, false).Count + siteDb.Folders.GetSubFolders(item.FullPath, ConstObjectType.CmsFile).Count; model.LastModified = PathService.GetLastModified(siteDb, item.FullPath, ConstObjectType.CmsFile); Result.Add(model); } return(Result); }
public FileFolderViewModel CheckStoragePathExists(FileFolderViewModel view, AgentAssetViewModel request, bool getShortPath, StorageDrive drive) { //check if storage path exists; if it doesn't exist, create folder var folder = _fileManager.GetFileFolderByStoragePath(view.StoragePath, drive.Name); if (folder.Name == null) { string storagePath = view.StoragePath; if (getShortPath) { storagePath = _fileManager.GetShortPath(view.StoragePath); } folder.Name = request.AgentId.ToString(); folder.StoragePath = storagePath; folder.IsFile = false; folder.Size = (request.File == null) ? view.Size : request.File.Length; folder = _fileManager.AddFileFolder(folder, drive.Id.ToString())[0]; } return(folder); }
public async Task <FileFolderViewModel> ExportFileFolder(string id, string driveId) { var response = new FileFolderViewModel(); string adapter = GetAdapterType(driveId); if (adapter.Equals(AdapterType.LocalFileStorage.ToString())) { response = await _localFileStorageAdapter.ExportFile(id, driveId); } //else if (adapter.Equals("AzureBlobStorageAdapter") && storageProvider.Equals("FileSystem.Azure")) // azureBlobStorageAdapter.SaveFile(request); //else if (adapter.Equals("AmazonEC2StorageAdapter") && storageProvider.Equals("FileSystem.Amazon")) // amazonEC2StorageAdapter.SaveFile(request); //else if (adapter.Equals("GoogleBlobStorageAdapter") && storageProvider.Equals("FileSystem.Google")) // googleBlobStorageAdapter.SaveFile(request); else { throw new EntityOperationException("Configuration is not set up for local file storage"); } return(response); }
public FileFolderViewModel CopyFileFolder(string fileFolderId, string parentFolderId, string driveName = null) { var response = new FileFolderViewModel(); string adapter = GetAdapterType(driveName); if (adapter.Equals(AdapterType.LocalFileStorage.ToString())) { response = _localFileStorageAdapter.CopyFileFolder(fileFolderId, parentFolderId, driveName); } //else if (adapter.Equals("AzureBlobStorageAdapter") && storageProvider.Equals("FileSystem.Azure")) // azureBlobStorageAdapter.SaveFile(request); //else if (adapter.Equals("AmazonEC2StorageAdapter") && storageProvider.Equals("FileSystem.Amazon")) // amazonEC2StorageAdapter.SaveFile(request); //else if (adapter.Equals("GoogleBlobStorageAdapter") && storageProvider.Equals("FileSystem.Google")) // googleBlobStorageAdapter.SaveFile(request); else { throw new EntityOperationException("Configuration is not set up for local file storage"); } return(response); }
public Asset CreateAsset(Asset asset, IFormFile file, string driveId = null) { AssetNameAvailability(asset); if (asset.Type == "Text") { asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.TextValue); } else if (asset.Type == "Number") { asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.NumberValue.ToString()); } else if (asset.Type == "Json") { asset.SizeInBytes = System.Text.Encoding.Unicode.GetByteCount(asset.JsonValue); } else if (asset.Type == "File") { var drive = new StorageDrive(); if (string.IsNullOrEmpty(driveId)) { drive = _storageDriveRepository.Find(null).Items.Where(q => q.IsDefault == true).FirstOrDefault(); } else { drive = _fileManager.GetDriveById(driveId); } if (drive == null) { throw new EntityDoesNotExistException("Default drive could not be found or does not exist"); } else { driveId = drive.Id.ToString(); } if (file != null) { IFormFile[] fileArray = { file }; asset.Id = Guid.NewGuid(); string shortPath = Path.Combine(drive.Name, "Assets"); var fileView = new FileFolderViewModel() { ContentType = file.ContentType, Files = fileArray, StoragePath = shortPath, IsFile = true }; var request = new AgentAssetViewModel(); request = request.Map(asset, file, drive.Id); CheckStoragePathExists(fileView, request, true, drive); fileView.StoragePath = Path.Combine(shortPath, asset.Id.ToString()); CheckStoragePathExists(fileView, request, true, drive); fileView = _fileManager.AddFileFolder(fileView, driveId)[0]; asset.FileId = fileView.Id; asset.SizeInBytes = file.Length; } else { throw new EntityDoesNotExistException("File does not exist"); } } return(asset); }
public Asset CreateAgentAsset(AgentAssetViewModel request) { Asset globalAsset = _repo.Find(null, a => a.Name == request.Name && a.AgentId == null).Items?.FirstOrDefault(); Asset agentAsset = new Asset(); if (globalAsset == null) { throw new EntityDoesNotExistException("No global asset exists with the given name"); } agentAsset.Name = request.Name; agentAsset.AgentId = request.AgentId; agentAsset.Type = globalAsset.Type; AssetNameAvailability(agentAsset); switch (agentAsset.Type.ToLower()) { case "text": if (request.TextValue == null) { agentAsset.TextValue = globalAsset.TextValue; } else { agentAsset.TextValue = request.TextValue; } agentAsset = GetSizeInBytes(agentAsset); break; case "number": if (request.NumberValue == null) { agentAsset.NumberValue = globalAsset.NumberValue; } else { agentAsset.NumberValue = request.NumberValue; } agentAsset = GetSizeInBytes(agentAsset); break; case "json": if (request.JsonValue == null) { agentAsset.JsonValue = globalAsset.JsonValue; } else { agentAsset.JsonValue = request.JsonValue; } agentAsset = GetSizeInBytes(agentAsset); break; case "file": var drive = new StorageDrive(); string driveId = request.DriveId.ToString(); if (string.IsNullOrEmpty(driveId)) { drive = _storageDriveRepository.Find(null).Items.Where(q => q.IsDefault == true).FirstOrDefault(); } else { drive = _fileManager.GetDriveById(driveId); } if (drive == null) { throw new EntityDoesNotExistException("Default drive could not be found or does not exist"); } else { driveId = drive.Id.ToString(); } //if file is in request, use file; else, get file from global asset IFormFile[] fileArray; string agentIdStr = request.AgentId.ToString(); string storagePath = Path.Combine(drive.Name, "Assets", globalAsset.Id.ToString(), agentIdStr); if (request.File != null) { fileArray = new IFormFile[] { request.File }; string fullStoragePath = Path.Combine(storagePath, request.File.FileName); var fileView = new FileFolderViewModel() { ContentType = request.File.ContentType, Files = fileArray, StoragePath = storagePath, IsFile = true, FullStoragePath = fullStoragePath }; CheckStoragePathExists(fileView, request, true, drive); fileView = _fileManager.AddFileFolder(fileView, driveId)[0]; agentAsset.FileId = fileView.Id; agentAsset.SizeInBytes = request.File.Length; } else { var fileViewModel = _fileManager.GetFileFolder(globalAsset.FileId.ToString(), driveId, "Files"); fileViewModel.StoragePath = storagePath; var folder = CheckStoragePathExists(fileViewModel, request, true, drive); fileViewModel = _fileManager.CopyFileFolder(fileViewModel.Id.ToString(), folder.Id.ToString(), driveId, "Files"); agentAsset.FileId = fileViewModel.Id; agentAsset.SizeInBytes = fileViewModel.Size; } break; } return(agentAsset); }
public Asset CreateAgentAsset(AgentAssetViewModel request) { Asset globalAsset = _repo.Find(null, a => a.Name == request.Name && a.AgentId == null).Items?.FirstOrDefault(); Asset agentAsset = new Asset(); if (globalAsset == null) { throw new EntityDoesNotExistException("No global asset exists with the given name"); } agentAsset.Name = request.Name; agentAsset.AgentId = request.AgentId; agentAsset.Type = globalAsset.Type; AssetNameAvailability(agentAsset); switch (agentAsset.Type.ToLower()) { case "text": if (request.TextValue == null) { agentAsset.TextValue = globalAsset.TextValue; } else { agentAsset.TextValue = request.TextValue; } agentAsset = GetSizeInBytes(agentAsset); break; case "number": if (request.NumberValue == null) { agentAsset.NumberValue = globalAsset.NumberValue; } else { agentAsset.NumberValue = request.NumberValue; } agentAsset = GetSizeInBytes(agentAsset); break; case "json": if (request.JsonValue == null) { agentAsset.JsonValue = globalAsset.JsonValue; } else { agentAsset.JsonValue = request.JsonValue; } agentAsset = GetSizeInBytes(agentAsset); break; case "file": if (request.DriveName != "Files" && !string.IsNullOrEmpty(request.DriveName)) { throw new EntityOperationException("Component files can only be saved in the Files drive"); } else if (string.IsNullOrEmpty(request.DriveName)) { request.DriveName = "Files"; } //if file is in request, use file; else, get file from global asset IFormFile[] fileArray; string agentIdStr = request.AgentId.ToString(); string storagePath = Path.Combine(request.DriveName, "Assets", agentIdStr); if (request.File != null) { fileArray = new IFormFile[] { request.File }; var fileView = new FileFolderViewModel() { ContentType = request.File.ContentType, Files = fileArray, StoragePath = storagePath, IsFile = true, FullStoragePath = Path.Combine(storagePath, request.File.FileName) }; var folder = CheckStoragePathExists(fileView, request); fileView = _fileManager.AddFileFolder(fileView, request.DriveName)[0]; agentAsset.FileId = fileView.Id; agentAsset.SizeInBytes = request.File.Length; } else { var fileViewModel = _fileManager.GetFileFolder(globalAsset.FileId.ToString(), request.DriveName); fileViewModel.FullStoragePath = storagePath; var folder = CheckStoragePathExists(fileViewModel, request); fileViewModel = _fileManager.CopyFileFolder(fileViewModel.Id.ToString(), folder.Id.ToString(), request.DriveName); agentAsset.FileId = fileViewModel.Id; agentAsset.SizeInBytes = fileViewModel.Size; } break; } return(agentAsset); }
public async Task <IActionResult> Replace(string id, string type, string driveId, [FromForm] FileFolderViewModel request) { try { if (type == "Folders") { throw new EntityOperationException("Folders cannot be replaced"); } request.Id = Guid.Parse(id); if (request.StorageDriveId == null || request.StorageDriveId == Guid.Empty) { request.StorageDriveId = Guid.Parse(driveId); } var existingFile = _manager.UpdateFile(request); return(Ok(existingFile)); } catch (Exception ex) { return(ex.GetActionResult()); } }