예제 #1
0
        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 = AzureBlobStorageApi.PathCombine(rootPath, path);
                var rootDir = new AzureBlobDirectory(rootPath);

                if (!await rootDir.ExistsAsync)
                {
                    await rootDir.CreateAsync();
                }

                response.Added.Add(await BaseModel.CreateAsync(rootDir, fullPath.RootVolume));
            }

            // Create temp file
            var archivePath = Path.GetTempFileName();
            await File.WriteAllBytesAsync(archivePath, await AzureBlobStorageApi.FileBytesAsync(fullPath.File.FullName));

            using (var archive = ZipFile.OpenRead(archivePath))
            {
                var separator = Path.DirectorySeparatorChar.ToString();

                foreach (var entry in archive.Entries)
                {
                    try
                    {
                        var file = AzureBlobStorageApi.PathCombine(rootPath, entry.FullName);

                        if (file.EndsWith(separator)) //directory
                        {
                            var dir = new AzureBlobDirectory(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);

                            await using (var stream = new FileStream(filePath, FileMode.Open))
                            {
                                await AzureBlobStorageApi.PutAsync(file, stream);
                            }

                            File.Delete(filePath);

                            if (!newFolder)
                            {
                                response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(file), fullPath.RootVolume));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw new Exception(entry.FullName, ex);
                    }
                }
            }

            File.Delete(archivePath);

            return(await Json(response));
        }
예제 #2
0
        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));
        }
예제 #3
0
        public async Task <JsonResult> DuplicateAsync(IEnumerable <FullPath> paths)
        {
            var response = new AddResponseModel();

            var pathList = paths.ToList();

            foreach (var path in pathList)
            {
                if (path.IsDirectory)
                {
                    var parentPath = path.Directory.Parent.FullName;
                    var name       = path.Directory.Name;
                    var newName    = $"{parentPath}/{name} copy";

                    // Check if directory already exists
                    if (!await AzureBlobStorageApi.DirectoryExistsAsync(newName))
                    {
                        // Doesn't exist
                        await AzureBlobStorageApi.CopyDirectoryAsync(path.Directory.FullName, newName);
                    }
                    else
                    {
                        // Already exists, create numbered copy
                        var newNameFound = false;

                        for (var i = 1; i < 100; i++)
                        {
                            newName = $"{parentPath}/{name} copy {i}";

                            // Test that it doesn't exist
                            if (await AzureBlobStorageApi.DirectoryExistsAsync(newName))
                            {
                                continue;
                            }

                            await AzureBlobStorageApi.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 AzureBlobDirectory(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;

                    var newName = $"{parentPath}/{name} copy{ext}";

                    // Check if file already exists
                    if (!await AzureBlobStorageApi.FileExistsAsync(newName))
                    {
                        // Doesn't exist
                        await AzureBlobStorageApi.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 AzureBlobStorageApi.FileExistsAsync(newName))
                            {
                                continue;
                            }

                            await AzureBlobStorageApi.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 CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newName), path.RootVolume));
                }
            }

            return(await Json(response));
        }