public async Task <JsonResult> PasteAsync(FullPath dest, IEnumerable <FullPath> paths, bool isCut, IEnumerable <string> renames, string suffix) { var response = new ReplaceResponseModel(); var pathList = paths.ToList(); foreach (var src in pathList) { if (src.IsDirectory) { var newDir = new AzureBlobDirectory(AzureBlobStorageApi.PathCombine(dest.Directory.FullName, src.Directory.Name)); // Check if it already exists if (await newDir.ExistsAsync) { await newDir.DeleteAsync(); } if (isCut) { await RemoveThumbsAsync(src); await AzureBlobStorageApi.MoveDirectoryAsync(src.Directory.FullName, newDir.FullName); response.Removed.Add(src.HashedTarget); } else { // Copy directory await AzureBlobStorageApi.CopyDirectoryAsync(src.Directory.FullName, newDir.FullName); } response.Added.Add(await BaseModel.CreateAsync(newDir, dest.RootVolume)); } else { var newFilePath = AzureBlobStorageApi.PathCombine(dest.Directory.FullName, src.File.Name); await AzureBlobStorageApi.DeleteFileIfExistsAsync(newFilePath); if (isCut) { await RemoveThumbsAsync(src); // Move file await AzureBlobStorageApi.MoveFileAsync(src.File.FullName, newFilePath); response.Removed.Add(src.HashedTarget); } else { // Copy file await AzureBlobStorageApi.CopyFileAsync(src.File.FullName, newFilePath); } response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newFilePath), dest.RootVolume)); } } return(await Json(response)); }
private static async Task RemoveThumbsAsync(FullPath path) { if (path.IsDirectory) { var thumbPath = path.RootVolume.GenerateThumbPath(path.Directory); if (thumbPath == null) { return; } await AzureBlobStorageApi.DeleteDirectoryIfExistsAsync(thumbPath); } else { var thumbPath = await path.RootVolume.GenerateThumbPathAsync(path.File); if (thumbPath == null) { return; } await AzureBlobStorageApi.DeleteFileIfExistsAsync(thumbPath); } }
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."); } var directoryInfo = parentPath.Directory; if (directoryInfo == null) { return(await Json(response)); } filename ??= "newfile"; if (filename.EndsWith(".zip")) { filename = filename.Replace(".zip", ""); } var newPath = AzureBlobStorageApi.PathCombine(directoryInfo.FullName, filename + ".zip"); await AzureBlobStorageApi.DeleteFileIfExistsAsync(newPath); var archivePath = Path.GetTempFileName(); var pathList = paths.ToList(); using (var newFile = ZipFile.Open(archivePath, ZipArchiveMode.Update)) { foreach (var tg in pathList) { if (tg.IsDirectory) { await AddDirectoryToArchiveAsync(newFile, tg.Directory, ""); } else { var filePath = Path.GetTempFileName(); await File.WriteAllBytesAsync(filePath, await AzureBlobStorageApi.FileBytesAsync(tg.File.FullName)); newFile.CreateEntryFromFile(filePath, tg.File.Name); } } } await using (var stream = new FileStream(archivePath, FileMode.Open)) { await AzureBlobStorageApi.PutAsync(newPath, stream); } // Cleanup File.Delete(archivePath); response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newPath), parentPath.RootVolume)); return(await Json(response)); }