示例#1
0
        public async Task <JsonResult> ListAsync(FullPath path, IEnumerable <string> intersect)
        {
            var response = new ListResponseModel();

            // Get all files and directories
            var items = await AzureStorageAPI.ListFilesAndDirectoriesAsync(path.Directory.FullName);

            // Add visible files
            foreach (var file in items.Where(i => i is CloudFile))
            {
                var f = new AzureStorageFile(file as CloudFile);
                if (!f.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.List.Add(f.Name);
                }
            }

            // Add visible directories
            foreach (var dir in items.Where(i => i is CloudFileDirectory))
            {
                var d = new AzureStorageDirectory(dir as CloudFileDirectory);
                if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.List.Add(d.Name);
                }
            }

            if (intersect.Any())
            {
                response.List.RemoveAll(l => !intersect.Contains(l));
            }

            return(await Json(response));
        }
示例#2
0
        public async Task <FullPath> ParsePathAsync(string target)
        {
            if (string.IsNullOrEmpty(target))
            {
                return(null);
            }

            string volumePrefix = null;
            string pathHash     = null;

            for (int i = 0; i < target.Length; i++)
            {
                if (target[i] == '_')
                {
                    pathHash     = target.Substring(i + 1);
                    volumePrefix = target.Substring(0, i + 1);
                    break;
                }
            }

            var    root   = Roots.First(r => r.VolumeId == volumePrefix);
            string path   = HttpEncoder.DecodePath(pathHash);
            string dirUrl = path != root.RootDirectory ? path : string.Empty;
            var    dir    = new AzureStorageDirectory(root.RootDirectory + dirUrl);

            if (await dir.ExistsAsync)
            {
                return(new FullPath(root, dir, target));
            }
            else
            {
                var file = new AzureStorageFile(root.RootDirectory + dirUrl);
                return(new FullPath(root, file, target));
            }
        }
示例#3
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));
        }
        private async void RemoveThumbs(FullPath path)
        {
            if (path.IsDirectory)
            {
                var thumbPath = path.RootVolume.GenerateThumbPath(path.Directory);
                if (thumbPath == null)
                {
                    return;
                }

                var thumbDir = new AzureStorageDirectory(thumbPath);
                if (await thumbDir.ExistsAsync)
                {
                    await thumbDir.DeleteAsync();
                }
            }
            else
            {
                var thumbPath = await path.RootVolume.GenerateThumbPath(path.File);

                if (thumbPath == null)
                {
                    return;
                }

                var thumbFile = new AzureStorageFile(thumbPath);
                if (await thumbFile.ExistsAsync)
                {
                    await thumbFile.DeleteAsync();
                }
            }
        }
示例#5
0
        public async Task <JsonResult> PasteAsync(FullPath dest, IEnumerable <FullPath> paths, bool isCut, IEnumerable <string> renames, string suffix)
        {
            var response = new ReplaceResponseModel();

            foreach (var src in paths)
            {
                if (src.IsDirectory)
                {
                    var newDir = new AzureStorageDirectory(AzureStorageAPI.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 AzureStorageAPI.MoveDirectoryAsync(src.Directory.FullName, newDir.FullName);

                        response.Removed.Add(src.HashedTarget);
                    }
                    else
                    {
                        // Copy directory
                        await AzureStorageAPI.CopyDirectoryAsync(src.Directory.FullName, newDir.FullName);
                    }
                    response.Added.Add(await BaseModel.CreateAsync(newDir, dest.RootVolume));
                }
                else
                {
                    string newFilePath = AzureStorageAPI.PathCombine(dest.Directory.FullName, src.File.Name);
                    await AzureStorageAPI.DeleteFileIfExistsAsync(newFilePath);

                    if (isCut)
                    {
                        await RemoveThumbsAsync(src);

                        // Move file
                        await AzureStorageAPI.MoveFileAsync(src.File.FullName, newFilePath);

                        response.Removed.Add(src.HashedTarget);
                    }
                    else
                    {
                        // Copy file
                        await AzureStorageAPI.CopyFileAsync(src.File.FullName, newFilePath);
                    }

                    response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newFilePath), dest.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));
        }
示例#7
0
        public async Task <JsonResult> OpenAsync(FullPath path, bool tree, IEnumerable <string> mimeTypes)
        {
            var response = new OpenResponse(await BaseModel.CreateAsync(path.Directory, path.RootVolume), path);

            // Get all files and directories
            var items = await AzureStorageAPI.ListFilesAndDirectoriesAsync(path.Directory.FullName);

            // Add visible files
            foreach (var file in items.Where(i => i is CloudFile))
            {
                var f = new AzureStorageFile(file as CloudFile);
                if (!f.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    if (mimeTypes != null && mimeTypes.Count() > 0 && !mimeTypes.Contains(f.MimeType) && !mimeTypes.Contains(f.MimeType.Type))
                    {
                        continue;
                    }

                    response.Files.Add(await BaseModel.CreateAsync(f, path.RootVolume));
                }
            }

            // Add visible directories
            foreach (var dir in items.Where(i => i is CloudFileDirectory))
            {
                var d = new AzureStorageDirectory(dir as CloudFileDirectory);
                if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(d, path.RootVolume));
                }
            }

            // Add parents
            if (tree)
            {
                var parent = path.Directory;

                while (parent != null && parent.FullName != path.RootVolume.RootDirectory)
                {
                    // Update parent
                    parent = parent.Parent;

                    // Ensure it's a child of the root
                    if (parent != null && path.RootVolume.RootDirectory.Contains(parent.FullName))
                    {
                        response.Files.Insert(0, await BaseModel.CreateAsync(parent, path.RootVolume));
                    }
                }
            }

            return(await Json(response));
        }
        public async Task <JsonResult> ParentsAsync(FullPath path)
        {
            var response = new TreeResponseModel();

            if (path.Directory.FullName == path.RootVolume.RootDirectory)
            {
                response.Tree.Add(await BaseModel.Create(this, path.Directory, path.RootVolume));
            }
            else
            {
                // Not root level

                // Go back to root
                var parent = path.Directory;

                while (parent != null && parent.Name != path.RootVolume.RootDirectory)
                {
                    // Update parent
                    parent = parent.Parent;

                    // Ensure it's a child of the root
                    if (parent != null && path.RootVolume.RootDirectory.Contains(parent.Name))
                    {
                        response.Tree.Insert(0, await BaseModel.Create(this, parent, path.RootVolume));
                    }
                }

                // Check that directory has a parent
                if (path.Directory.Parent != null)
                {
                    var items = await AzureStorageAPI.ListFilesAndDirectories(path.Directory.Parent.FullName);

                    // Add all visible directories except the target
                    foreach (var dir in items.Where(i => i is CloudFileDirectory && ((CloudFileDirectory)i).Name != path.Directory.Name))
                    {
                        var d = new AzureStorageDirectory(dir as CloudFileDirectory);
                        if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                        {
                            response.Tree.Add(await BaseModel.Create(this, d, path.RootVolume));
                        }
                    }
                }
            }
            return(await Json(response));
        }
示例#9
0
        public async Task <JsonResult> TreeAsync(FullPath path)
        {
            var response = new TreeResponseModel();

            var items = await AzureStorageAPI.ListFilesAndDirectoriesAsync(path.Directory.FullName);

            // Add visible directories
            foreach (var dir in items.Where(i => i is CloudFileDirectory))
            {
                var d = new AzureStorageDirectory(dir as CloudFileDirectory);
                if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Tree.Add(await BaseModel.CreateAsync(d, path.RootVolume));
                }
            }

            return(await Json(response));
        }
示例#10
0
        public async Task <JsonResult> InitAsync(FullPath path, IEnumerable <string> mimeTypes)
        {
            if (path == null)
            {
                var root = Roots.FirstOrDefault(r => r.StartDirectory != null);
                if (root == null)
                {
                    root = Roots.First();
                }

                path = new FullPath(root, new AzureStorageDirectory(root.StartDirectory ?? root.RootDirectory), null);
            }

            var response = new InitResponseModel(await BaseModel.CreateAsync(path.Directory, path.RootVolume), new Options(path));

            // Get all files and directories
            var items = await AzureStorageAPI.ListFilesAndDirectoriesAsync(path.Directory.FullName);

            // Add visible files
            foreach (var file in items.Where(i => i is CloudFile))
            {
                var f = new AzureStorageFile(file as CloudFile);
                if (!f.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    if (mimeTypes != null && mimeTypes.Count() > 0 && !mimeTypes.Contains(f.MimeType) && !mimeTypes.Contains(f.MimeType.Type))
                    {
                        continue;
                    }

                    response.Files.Add(await BaseModel.CreateAsync(f, path.RootVolume));
                }
            }

            // Add visible directories
            foreach (var dir in items.Where(i => i is CloudFileDirectory))
            {
                var d = new AzureStorageDirectory(dir as CloudFileDirectory);
                if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(d, path.RootVolume));
                }
            }

            // Add roots
            foreach (var root in Roots)
            {
                response.Files.Add(await BaseModel.CreateAsync(new AzureStorageDirectory(root.RootDirectory), root));
            }

            if (path.RootVolume.RootDirectory != path.Directory.FullName)
            {
                // Get all files and directories
                var entries = await AzureStorageAPI.ListFilesAndDirectoriesAsync(path.RootVolume.RootDirectory);

                // Add visible directories
                foreach (var dir in entries.Where(i => i is CloudFileDirectory))
                {
                    var d = new AzureStorageDirectory(dir as CloudFileDirectory);
                    if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        response.Files.Add(await BaseModel.CreateAsync(d, path.RootVolume));
                    }
                }
            }

            if (path.RootVolume.MaxUploadSize.HasValue)
            {
                response.Options.UploadMaxSize = $"{path.RootVolume.MaxUploadSizeInKb.Value}K";
            }

            return(await Json(response));
        }
示例#11
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));
        }