Exemple #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 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 <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));
        }
Exemple #3
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 = 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));
        }