Пример #1
0
        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));
        }
Пример #2
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));
        }