Example #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));
        }
Example #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));
            }
        }
        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();
                }
            }
        }
Example #4
0
        public async Task <JsonResult> MakeFileAsync(FullPath path, string name)
        {
            var newFile = new AzureStorageFile(AzureStorageAPI.PathCombine(path.Directory.FullName, name));
            await newFile.CreateAsync();

            var response = new AddResponseModel();

            response.Added.Add(await BaseModel.CreateAsync(newFile, path.RootVolume));
            return(await Json(response));
        }
Example #5
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));
        }
Example #6
0
        public async Task <JsonResult> UploadAsync(FullPath path, IEnumerable <IFormFile> files, bool?overwrite, IEnumerable <FullPath> uploadPaths, IEnumerable <string> renames, string suffix)
        {
            var response = new AddResponseModel();

            // Check if max upload size is set and that no files exceeds it
            if (path.RootVolume.MaxUploadSize.HasValue && files.Any(x => x.Length > path.RootVolume.MaxUploadSize))
            {
                // Max upload size exceeded
                return(Error.MaxUploadFileSize());
            }

            foreach (string rename in renames)
            {
                var    fileInfo    = new FileInfo(Path.Combine(path.Directory.FullName, rename));
                string destination = Path.Combine(path.Directory.FullName, $"{Path.GetFileNameWithoutExtension(rename)}{suffix}{Path.GetExtension(rename)}");
                fileInfo.MoveTo(destination);
                response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(destination), path.RootVolume));
            }

            foreach (var uploadPath in uploadPaths)
            {
                var dir = uploadPath.Directory;
                while (dir.FullName != path.RootVolume.RootDirectory)
                {
                    response.Added.Add(await BaseModel.CreateAsync(new AzureStorageDirectory(dir.FullName), path.RootVolume));
                    dir = dir.Parent;
                }
            }

            var i = 0;

            foreach (var file in files)
            {
                string destination = uploadPaths.Count() > i?uploadPaths.ElementAt(i).Directory.FullName : path.Directory.FullName;

                var azureFile = new AzureStorageFile(AzureStorageAPI.PathCombine(destination, Path.GetFileName(file.FileName)));

                if (await azureFile.ExistsAsync)
                {
                    if (overwrite ?? path.RootVolume.UploadOverwrite)
                    {
                        await azureFile.DeleteAsync();

                        await AzureStorageAPI.UploadAsync(file, azureFile.FullName);

                        response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(azureFile.FullName), path.RootVolume));
                    }
                    else
                    {
                        var newName = await CreateNameForCopy(azureFile, suffix);

                        await AzureStorageAPI.UploadAsync(file, AzureStorageAPI.PathCombine(azureFile.DirectoryName, newName));

                        response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newName), path.RootVolume));
                    }
                }
                else
                {
                    await AzureStorageAPI.UploadAsync(file, azureFile.FullName);

                    response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(azureFile.FullName), path.RootVolume));
                }

                i++;
            }
            return(await Json(response));
        }
Example #7
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));
        }
        public async Task <JsonResult> UploadAsync(FullPath path, IEnumerable <IFormFile> files)
        {
            int fileCount = files.Count();

            var response = new AddResponseModel();

            // Check if max upload size is set and that no files exceeds it
            if (path.RootVolume.MaxUploadSize.HasValue && files.Any(x => x.Length > path.RootVolume.MaxUploadSize))
            {
                // Max upload size exceeded
                return(Error.MaxUploadFileSize());
            }

            // Loop files
            foreach (var file in files)
            {
                // Validate file name
                if (string.IsNullOrWhiteSpace(file.FileName))
                {
                    throw new ArgumentNullException(nameof(IFormFile.FileName));
                }

                // Get path
                var p = new AzureStorageFile(AzureStorageAPI.UrlCombine(path.Directory.FullName, Path.GetFileName(file.FileName)));

                // Check if it already exists
                if (await p.ExistsAsync)
                {
                    // Check if overwrite on upload is supported
                    if (path.RootVolume.UploadOverwrite)
                    {
                        // If file already exist we rename the current file.
                        // If upload is successful, delete temp file. Otherwise, we restore old file.
                        var tmpPath = p.FullName + Guid.NewGuid();

                        // Save file
                        var uploaded = false;

                        try
                        {
                            await AzureStorageAPI.Upload(file, tmpPath);

                            uploaded = true;
                        }
                        catch (Exception) { }
                        finally
                        {
                            // Check that file was saved correctly
                            if (uploaded)
                            {
                                // Delete file
                                await p.DeleteAsync();

                                // Move file
                                await AzureStorageAPI.MoveFile(tmpPath, p.FullName);
                            }
                            else
                            {
                                // Delete temporary file
                                await AzureStorageAPI.DeleteFile(tmpPath);
                            }
                        }
                    }
                    else
                    {
                        // Ensure directy name is set
                        if (string.IsNullOrEmpty(p.Directory.Name))
                        {
                            throw new ArgumentNullException("Directory");
                        }

                        // Save file
                        await AzureStorageAPI.Upload(file, AzureStorageAPI.UrlCombine(p.Directory.FullName, await AzureStorageAPI.GetDuplicatedName(p)));
                    }
                }
                else
                {
                    // Save file
                    await AzureStorageAPI.Upload(file, p.FullName);
                }

                response.Added.Add((FileModel)await BaseModel.Create(this, new AzureStorageFile(p.FullName), path.RootVolume));
            }
            return(await Json(response));
        }