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 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 async Task <IActionResult> AddAgentAsset([FromForm] AgentAssetViewModel request) { try { Asset agentAsset = _manager.CreateAgentAsset(request); var response = await base.PostEntity(agentAsset); await _webhookPublisher.PublishAsync("Assets.NewAssetCreated", agentAsset.Id.ToString(), agentAsset.Name).ConfigureAwait(false); return(response); } catch (Exception ex) { return(ex.GetActionResult()); } }
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 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 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); }