public async Task <JsonResult> MakeDirAsync(FullPath path, string name, IEnumerable <string> dirs) { var response = new AddResponseModel(); if (!string.IsNullOrEmpty(name)) { // Create directory var newDir = new AzureStorageDirectory(AzureStorageAPI.PathCombine(path.Directory.FullName, name)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); } if (dirs.Any()) { foreach (string dir in dirs) { string dirName = dir.StartsWith("/") ? dir.Substring(1) : dir; var newDir = new AzureStorageDirectory(AzureStorageAPI.PathCombine(path.Directory.FullName, dirName)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); string relativePath = newDir.FullName.Substring(path.RootVolume.RootDirectory.Length); response.Hashes.Add($"/{dirName}", path.RootVolume.VolumeId + HttpEncoder.EncodePath(relativePath)); } } return(await Json(response)); }
public async Task <ConnectorResult> MakeDirAsync(FullPath path, string name, IEnumerable <string> dirs) { var response = new AddResponseModel(); if (!string.IsNullOrEmpty(name)) { var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, name)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); } foreach (string dir in dirs) { string dirName = dir.StartsWith("/") ? dir.Substring(1) : dir; var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, dirName)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); string relativePath = newDir.FullName.Substring(path.RootVolume.RootDirectory.Length); response.Hashes.Add($"/{dirName}", path.RootVolume.VolumeId + HttpEncoder.EncodePath(relativePath)); } return(new ConnectorResult(response)); }
public async Task <JsonResult> Duplicate(IEnumerable <string> targets) { AddResponseModel response = new AddResponseModel(); foreach (var target in targets) { FullPath fullPath = ParsePath(target); if (fullPath.Directory != null) { var parentPath = fullPath.Directory.Parent.FullName; var name = fullPath.Directory.Name; var newName = string.Format(@"{0}\{1} copy", parentPath, name); if (!Directory.Exists(newName)) { DirectoryCopy(fullPath.Directory, newName, true); } else { for (int i = 1; i < 100; i++) { newName = string.Format(@"{0}\{1} copy {2}", parentPath, name, i); if (!Directory.Exists(newName)) { DirectoryCopy(fullPath.Directory, newName, true); break; } } } response.Added.Add(BaseModel.Create(new DirectoryInfo(newName), fullPath.Root)); } else { var parentPath = fullPath.File.Directory.FullName; var name = fullPath.File.Name.Substring(0, fullPath.File.Name.Length - fullPath.File.Extension.Length); var ext = fullPath.File.Extension; var newName = string.Format(@"{0}\{1} copy{2}", parentPath, name, ext); if (!System.IO.File.Exists(newName)) { fullPath.File.CopyTo(newName); } else { for (int i = 1; i < 100; i++) { newName = string.Format(@"{0}\{1} copy {2}{3}", parentPath, name, i, ext); if (!System.IO.File.Exists(newName)) { fullPath.File.CopyTo(newName); break; } } } response.Added.Add(BaseModel.Create(new FileInfo(newName), fullPath.Root)); } } return(await Json(response)); }
public async Task <JsonResult> DuplicateAsync(IEnumerable <FullPath> paths) { var response = new AddResponseModel(); foreach (var path in paths) { if (path.IsDirectory) { var parentPath = path.Directory.Parent.FullName; var name = path.Directory.Name; string newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy"; if (!Directory.Exists(newName)) { DirectoryCopy(path.Directory.FullName, newName, true); } else { for (int i = 1; i < 100; i++) { newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy {i}"; if (!Directory.Exists(newName)) { DirectoryCopy(path.Directory.FullName, newName, true); break; } } } response.Added.Add(await BaseModel.Create(this, new FileSystemDirectory(newName), path.RootVolume)); } else { var parentPath = path.File.Directory.FullName; var name = path.File.Name.Substring(0, path.File.Name.Length - path.File.Extension.Length); var ext = path.File.Extension; string newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy{ext}"; if (!File.Exists(newName)) { File.Copy(path.File.FullName, newName); } else { for (int i = 1; i < 100; i++) { newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy {i}{ext}"; if (!File.Exists(newName)) { File.Copy(path.File.FullName, newName); break; } } } response.Added.Add(await BaseModel.Create(this, new FileSystemFile(newName), path.RootVolume)); } } return(await Json(response)); }
public async Task <ConnectorResult> ArchiveAsync(FullPath parentPath, IEnumerable <FullPath> paths, string filename, string mimeType) { var response = new AddResponseModel(); if (paths == null) { throw new NotSupportedException(); } if (mimeType != "application/zip") { throw new NotSupportedException("Only .zip files are currently supported."); } // Parse target path var directoryInfo = parentPath.Directory; if (directoryInfo != null) { if (filename is null) { filename = "newfile"; } if (filename.EndsWith(".zip")) { filename = filename.Replace(".zip", ""); } string newPath = Path.Combine(directoryInfo.FullName, filename + ".zip"); if (File.Exists(newPath)) { File.Delete(newPath); } using (var newFile = ZipFile.Open(newPath, ZipArchiveMode.Create)) { foreach (var tg in paths) { if (tg.IsDirectory) { await AddDirectoryToArchiveAsync(newFile, tg.Directory, ""); } else { newFile.CreateEntryFromFile(tg.File.FullName, tg.File.Name); } } } response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(newPath), parentPath.RootVolume)); } return(new ConnectorResult(response)); }
public async Task <JsonResult> MakeFileAsync(FullPath path, string name) { var newFile = new AzureStorageFile(AzureStorageAPI.PathCombine(path.Directory.FullName, name)); await newFile.CreateAsync(); var response = new AddResponseModel(); response.Added.Add(await BaseModel.CreateAsync(newFile, path.RootVolume)); return(await Json(response)); }
public async Task <ConnectorResult> MakeFileAsync(FullPath path, string name) { var newFile = new FileSystemFile(Path.Combine(path.Directory.FullName, name)); await newFile.CreateAsync(); var response = new AddResponseModel(); response.Added.Add(await BaseModel.CreateAsync(newFile, path.RootVolume)); return(new ConnectorResult(response)); }
public async Task <JsonResult> MakeDirAsync(FullPath path, string name) { var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, name)); await newDir.CreateAsync(); var response = new AddResponseModel(); response.Added.Add(await BaseModel.Create(this, newDir, path.RootVolume)); return(await Json(response)); }
public async Task <JsonResult> MakeDirAsync(FullPath path, string name) { // Create directory var newDir = new AzureStorageDirectory(AzureStorageAPI.UrlCombine(path.Directory.FullName, name)); await newDir.CreateAsync(); var response = new AddResponseModel(); response.Added.Add(await BaseModel.Create(this, newDir, path.RootVolume)); return(await Json(response)); }
public AddResponseModel Add([FromBody] TAddRequestModel model) { if (model == null) { return(AddResponseModel.BadInput()); } if (EntityStorage.Select <TEntity>().Any(c => model.IsSameWith(c))) { return(AddResponseModel.AlreadyExists(typeof(TEntity).Name)); } var dbModel = model.ToDbModel(); try { EntityStorage.Add(dbModel); } catch (Exception e) { return(AddResponseModel.Failed(e)); } return(AddResponseModel.Successful(dbModel.Id)); }
public async Task <JsonResult> MakeDirAsync(FullPath path, string name, IEnumerable <string> dirs) { var response = new AddResponseModel(); if (!string.IsNullOrEmpty(name)) { // Create directory var newDir = new AzureBlobDirectory(AzureBlobStorageApi.PathCombine(path.Directory.FullName, name)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); } var enumerable = dirs as string[] ?? dirs.ToArray(); if (!enumerable.Any()) { return(await Json(response)); } foreach (var dir in enumerable) { var dirName = dir.StartsWith("/") ? dir.Substring(1) : dir; var newDir = new AzureBlobDirectory(AzureBlobStorageApi.PathCombine(path.Directory.FullName, dirName)); await newDir.CreateAsync(); response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume)); var relativePath = newDir.FullName.Substring(path.RootVolume.RootDirectory.Length); // response.Hashes.Add(new KeyValuePair<string, string>($"/{dirName}", path.RootVolume.VolumeId + HttpEncoder.EncodePath(relativePath))); response.Hashes.Add($"/{dirName}", path.RootVolume.VolumeId + HttpEncoder.EncodePath(relativePath)); } return(await Json(response)); }
public async Task <JsonResult> UploadAsync(FullPath path, IEnumerable <IFormFile> files, bool?overwrite, IEnumerable <FullPath> uploadPaths, IEnumerable <string> renames, string suffix) { var response = new AddResponseModel(); // Check if max upload size is set and that no files exceeds it if (path.RootVolume.MaxUploadSize.HasValue && files.Any(x => x.Length > path.RootVolume.MaxUploadSize)) { // Max upload size exceeded return(Error.MaxUploadFileSize()); } foreach (string rename in renames) { var fileInfo = new FileInfo(Path.Combine(path.Directory.FullName, rename)); string destination = Path.Combine(path.Directory.FullName, $"{Path.GetFileNameWithoutExtension(rename)}{suffix}{Path.GetExtension(rename)}"); fileInfo.MoveTo(destination); response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(destination), path.RootVolume)); } foreach (var uploadPath in uploadPaths) { var dir = uploadPath.Directory; while (dir.FullName != path.RootVolume.RootDirectory) { response.Added.Add(await BaseModel.CreateAsync(new AzureStorageDirectory(dir.FullName), path.RootVolume)); dir = dir.Parent; } } var i = 0; foreach (var file in files) { string destination = uploadPaths.Count() > i?uploadPaths.ElementAt(i).Directory.FullName : path.Directory.FullName; var azureFile = new AzureStorageFile(AzureStorageAPI.PathCombine(destination, Path.GetFileName(file.FileName))); if (await azureFile.ExistsAsync) { if (overwrite ?? path.RootVolume.UploadOverwrite) { await azureFile.DeleteAsync(); await AzureStorageAPI.UploadAsync(file, azureFile.FullName); response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(azureFile.FullName), path.RootVolume)); } else { var newName = await CreateNameForCopy(azureFile, suffix); await AzureStorageAPI.UploadAsync(file, AzureStorageAPI.PathCombine(azureFile.DirectoryName, newName)); response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newName), path.RootVolume)); } } else { await AzureStorageAPI.UploadAsync(file, azureFile.FullName); response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(azureFile.FullName), path.RootVolume)); } i++; } return(await Json(response)); }
public async Task <JsonResult> ArchiveAsync(FullPath parentPath, IEnumerable <FullPath> paths, string filename, string mimeType) { var response = new AddResponseModel(); if (paths == null) { throw new NotSupportedException(); } if (mimeType != "application/zip") { throw new NotSupportedException("Only .zip files are currently supported."); } // Parse target path var directoryInfo = parentPath.Directory; if (directoryInfo != null) { filename ??= "newfile"; if (filename.EndsWith(".zip")) { filename = filename.Replace(".zip", ""); } var newPath = AzureStorageAPI.PathCombine(directoryInfo.FullName, filename + ".zip"); await AzureStorageAPI.DeleteFileIfExistsAsync(newPath); var archivePath = Path.GetTempFileName(); using (var newFile = ZipFile.Open(archivePath, ZipArchiveMode.Update)) { foreach (var tg in paths) { if (tg.IsDirectory) { await AddDirectoryToArchiveAsync(newFile, tg.Directory, ""); } else { var filePath = Path.GetTempFileName(); File.WriteAllBytes(filePath, await AzureStorageAPI.FileBytesAsync(tg.File.FullName)); newFile.CreateEntryFromFile(filePath, tg.File.Name); } } } using (var stream = new FileStream(archivePath, FileMode.Open)) { await AzureStorageAPI.PutAsync(newPath, stream); } // Cleanup File.Delete(archivePath); response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newPath), parentPath.RootVolume)); } return(await Json(response)); }
public async Task <JsonResult> ExtractAsync(FullPath fullPath, bool newFolder) { var response = new AddResponseModel(); if (fullPath.IsDirectory || fullPath.File.Extension.ToLower() != ".zip") { throw new NotSupportedException("Only .zip files are currently supported."); } var rootPath = fullPath.File.Directory.FullName; if (newFolder) { // Azure doesn't like directory names that look like a file name i.e. blah.png // So iterate through the names until there's no more extension var path = Path.GetFileNameWithoutExtension(fullPath.File.Name); while (Path.HasExtension(path)) { path = Path.GetFileNameWithoutExtension(path); } rootPath = AzureStorageAPI.PathCombine(rootPath, path); var rootDir = new AzureStorageDirectory(rootPath); if (!await rootDir.ExistsAsync) { await rootDir.CreateAsync(); } response.Added.Add(await BaseModel.CreateAsync(rootDir, fullPath.RootVolume)); } // Create temp file var archivePath = Path.GetTempFileName(); File.WriteAllBytes(archivePath, await AzureStorageAPI.FileBytesAsync(fullPath.File.FullName)); using (var archive = ZipFile.OpenRead(archivePath)) { var separator = Path.DirectorySeparatorChar.ToString(); foreach (ZipArchiveEntry entry in archive.Entries) { try { var file = AzureStorageAPI.PathCombine(rootPath, entry.FullName); if (file.EndsWith(separator)) //directory { var dir = new AzureStorageDirectory(file); if (!await dir.ExistsAsync) { await dir.CreateAsync(); } if (!newFolder) { response.Added.Add(await BaseModel.CreateAsync(dir, fullPath.RootVolume)); } } else { var filePath = Path.GetTempFileName(); entry.ExtractToFile(filePath, true); using (var stream = new FileStream(filePath, FileMode.Open)) { await AzureStorageAPI.PutAsync(file, stream); } File.Delete(filePath); if (!newFolder) { response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(file), fullPath.RootVolume)); } } } catch //(Exception ex) { //throw new Exception(entry.FullName, ex); } } } File.Delete(archivePath); return(await Json(response)); }
public async Task <JsonResult> DuplicateAsync(IEnumerable <FullPath> paths) { var response = new AddResponseModel(); foreach (var path in paths) { if (path.IsDirectory) { var parentPath = path.Directory.Parent.FullName; var name = path.Directory.Name; string newName = $"{parentPath}/{name} copy"; // Check if directory already exists if (!await AzureStorageAPI.DirectoryExistsAsync(newName)) { // Doesn't exist await AzureStorageAPI.CopyDirectoryAsync(path.Directory.FullName, newName); } else { // Already exists, create numbered copy var newNameFound = false; for (int i = 1; i < 100; i++) { newName = $"{parentPath}/{name} copy {i}"; // Test that it doesn't exist if (!await AzureStorageAPI.DirectoryExistsAsync(newName)) { await AzureStorageAPI.CopyDirectoryAsync(path.Directory.FullName, newName); newNameFound = true; break; } } // Check if new name was found if (!newNameFound) { return(Error.NewNameSelectionException($@"{parentPath}/{name} copy")); } } response.Added.Add(await BaseModel.CreateAsync(new AzureStorageDirectory(newName), path.RootVolume)); } else // File { var parentPath = path.File.Directory.FullName; var name = path.File.Name.Substring(0, path.File.Name.Length - path.File.Extension.Length); var ext = path.File.Extension; string newName = $"{parentPath}/{name} copy{ext}"; // Check if file already exists if (!await AzureStorageAPI.FileExistsAsync(newName)) { // Doesn't exist await AzureStorageAPI.CopyFileAsync(path.File.FullName, newName); } else { // Already exists, create numbered copy var newNameFound = false; for (var i = 1; i < 100; i++) { // Compute new name newName = $@"{parentPath}/{name} copy {i}{ext}"; // Test that it doesn't exist if (!await AzureStorageAPI.FileExistsAsync(newName)) { await AzureStorageAPI.CopyFileAsync(path.File.FullName, newName); newNameFound = true; break; } } // Check if new name was found if (!newNameFound) { return(Error.NewNameSelectionException($@"{parentPath}/{name} copy")); } } response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newName), path.RootVolume)); } } return(await Json(response)); }
public async Task <JsonResult> UploadAsync(FullPath path, IEnumerable <IFormFile> files, bool?overwrite, IEnumerable <FullPath> uploadPaths, IEnumerable <string> renames, string suffix) { var response = new AddResponseModel(); if (path.RootVolume.MaxUploadSize.HasValue) { foreach (var file in files) { if (file.Length > path.RootVolume.MaxUploadSize.Value) { return(Error.MaxUploadFileSize()); } } } foreach (string rename in renames) { var fileInfo = new FileInfo(Path.Combine(path.Directory.FullName, rename)); string destination = Path.Combine(path.Directory.FullName, $"{Path.GetFileNameWithoutExtension(rename)}{suffix}{Path.GetExtension(rename)}"); fileInfo.MoveTo(destination); response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(destination), path.RootVolume)); } foreach (var uploadPath in uploadPaths) { var directory = uploadPath.Directory; while (directory.FullName != path.RootVolume.RootDirectory) { response.Added.Add(await BaseModel.CreateAsync(new FileSystemDirectory(directory.FullName), path.RootVolume)); directory = directory.Parent; } } int i = 0; foreach (var file in files) { string destination = uploadPaths.Count() > i?uploadPaths.ElementAt(i).Directory.FullName : path.Directory.FullName; var fileInfo = new FileInfo(Path.Combine(destination, Path.GetFileName(file.FileName))); if (fileInfo.Exists) { if (overwrite ?? path.RootVolume.UploadOverwrite) { fileInfo.Delete(); using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Create)) { await file.CopyToAsync(fileStream); } response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(fileInfo.FullName), path.RootVolume)); } else { string newName = CreateNameForCopy(fileInfo, suffix); using (var fileStream = new FileStream(Path.Combine(fileInfo.DirectoryName, newName), FileMode.Create)) { await file.CopyToAsync(fileStream); } response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(newName), path.RootVolume)); } } else { using (var fileStream = new FileStream(fileInfo.FullName, FileMode.Create)) { await file.CopyToAsync(fileStream); } response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(fileInfo.FullName), path.RootVolume)); } i++; } return(await Json(response)); }
public async Task <JsonResult> ExtractAsync(FullPath fullPath, bool newFolder) { var response = new AddResponseModel(); if (fullPath.IsDirectory || fullPath.File.Extension.ToLower() != ".zip") { throw new NotSupportedException("Only .zip files are currently supported."); } string rootPath = fullPath.File.Directory.FullName; if (newFolder) { rootPath = Path.Combine(rootPath, Path.GetFileNameWithoutExtension(fullPath.File.Name)); var rootDir = new FileSystemDirectory(rootPath); if (!await rootDir.ExistsAsync) { await rootDir.CreateAsync(); } response.Added.Add(await BaseModel.CreateAsync(rootDir, fullPath.RootVolume)); } using (var archive = ZipFile.OpenRead(fullPath.File.FullName)) { string separator = Path.DirectorySeparatorChar.ToString(); foreach (ZipArchiveEntry entry in archive.Entries) { try { //Replce zip entry path separator by system path separator string file = Path.Combine(rootPath, entry.FullName) .Replace("/", separator).Replace("\\", separator); if (file.EndsWith(separator)) //directory { var dir = new FileSystemDirectory(file); if (!await dir.ExistsAsync) { await dir.CreateAsync(); } if (!newFolder) { response.Added.Add(await BaseModel.CreateAsync(dir, fullPath.RootVolume)); } } else { entry.ExtractToFile(file, true); if (!newFolder) { response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(file), fullPath.RootVolume)); } } } catch //(Exception ex) { //throw new Exception(entry.FullName, ex); } } } return(await Json(response)); }
public async Task <ConnectorResult> DuplicateAsync(IEnumerable <FullPath> paths) { var response = new AddResponseModel(); foreach (var path in paths) { if (path.IsDirectory) { string parentPath = path.Directory.Parent.FullName; string name = path.Directory.Name; string newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy"; if (!Directory.Exists(newName)) { DirectoryCopy(path.Directory.FullName, newName, true); } else { bool foundNewName = false; for (int i = 1; i < 100; i++) { newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy {i}"; if (!Directory.Exists(newName)) { DirectoryCopy(path.Directory.FullName, newName, true); foundNewName = true; break; } } if (!foundNewName) { return(new ConnectorResult($"Unable to create new file with name {parentPath}{Path.DirectorySeparatorChar}{name} copy")); } } response.Added.Add(await BaseModel.CreateAsync(new FileSystemDirectory(newName), path.RootVolume)); } else { string parentPath = path.File.Directory.FullName; string name = path.File.Name.Substring(0, path.File.Name.Length - path.File.Extension.Length); string ext = path.File.Extension; string newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy{ext}"; if (!File.Exists(newName)) { File.Copy(path.File.FullName, newName); } else { bool foundNewName = false; for (int i = 1; i < 100; i++) { newName = $"{parentPath}{Path.DirectorySeparatorChar}{name} copy {i}{ext}"; if (!File.Exists(newName)) { File.Copy(path.File.FullName, newName); foundNewName = true; break; } } if (!foundNewName) { return(new ConnectorResult($"Unable to create new file with name {parentPath}{Path.DirectorySeparatorChar}{name} copy{ext}")); } } response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(newName), path.RootVolume)); } } return(new ConnectorResult(response)); }
public async Task <JsonResult> Upload(string target, IEnumerable <IFormFile> targets) { int fileCount = targets.Count(); FullPath dest = ParsePath(target); var response = new AddResponseModel(); if (dest.Root.MaxUploadSize.HasValue) { for (int i = 0; i < fileCount; i++) { IFormFile file = targets.ElementAt(i); if (file.Length > dest.Root.MaxUploadSize.Value) { return(Error.MaxUploadFileSize()); } } } for (int i = 0; i < fileCount; i++) { IFormFile file = targets.ElementAt(i); FileInfo path = new FileInfo(Path.Combine(dest.Directory.FullName, Path.GetFileName(file.FileName))); if (path.Exists) { if (dest.Root.UploadOverwrite) { //if file already exist we rename the current file, //and if upload is succesfully delete temp file, in otherwise we restore old file string tmpPath = path.FullName + Guid.NewGuid(); bool uploaded = false; try { using (var fileStream = new FileStream(tmpPath, FileMode.Create)) { await file.CopyToAsync(fileStream); } uploaded = true; } catch { } finally { if (uploaded) { System.IO.File.Delete(path.FullName); System.IO.File.Move(tmpPath, path.FullName); } else { System.IO.File.Delete(tmpPath); } } } else { using (var fileStream = new FileStream(Path.Combine(path.DirectoryName, Utils.GetDuplicatedName(path)), FileMode.Create)) { await file.CopyToAsync(fileStream); } } } else { using (var fileStream = new FileStream(path.FullName, FileMode.Create)) { await file.CopyToAsync(fileStream); } } response.Added.Add((FileModel)BaseModel.Create(new FileInfo(path.FullName), dest.Root)); } return(await Json(response)); }
public async Task <JsonResult> UploadAsync(FullPath path, IEnumerable <IFormFile> files) { int fileCount = files.Count(); var response = new AddResponseModel(); // Check if max upload size is set and that no files exceeds it if (path.RootVolume.MaxUploadSize.HasValue && files.Any(x => x.Length > path.RootVolume.MaxUploadSize)) { // Max upload size exceeded return(Error.MaxUploadFileSize()); } // Loop files foreach (var file in files) { // Validate file name if (string.IsNullOrWhiteSpace(file.FileName)) { throw new ArgumentNullException(nameof(IFormFile.FileName)); } // Get path var p = new AzureStorageFile(AzureStorageAPI.UrlCombine(path.Directory.FullName, Path.GetFileName(file.FileName))); // Check if it already exists if (await p.ExistsAsync) { // Check if overwrite on upload is supported if (path.RootVolume.UploadOverwrite) { // If file already exist we rename the current file. // If upload is successful, delete temp file. Otherwise, we restore old file. var tmpPath = p.FullName + Guid.NewGuid(); // Save file var uploaded = false; try { await AzureStorageAPI.Upload(file, tmpPath); uploaded = true; } catch (Exception) { } finally { // Check that file was saved correctly if (uploaded) { // Delete file await p.DeleteAsync(); // Move file await AzureStorageAPI.MoveFile(tmpPath, p.FullName); } else { // Delete temporary file await AzureStorageAPI.DeleteFile(tmpPath); } } } else { // Ensure directy name is set if (string.IsNullOrEmpty(p.Directory.Name)) { throw new ArgumentNullException("Directory"); } // Save file await AzureStorageAPI.Upload(file, AzureStorageAPI.UrlCombine(p.Directory.FullName, await AzureStorageAPI.GetDuplicatedName(p))); } } else { // Save file await AzureStorageAPI.Upload(file, p.FullName); } response.Added.Add((FileModel)await BaseModel.Create(this, new AzureStorageFile(p.FullName), path.RootVolume)); } return(await Json(response)); }