Beispiel #1
0
        public async Task <JsonResult> MakeFileAsync(FullPath path, string name)
        {
            var newFile = new AzureBlobFile(AzureBlobStorageApi.PathCombine(path.Directory.FullName, name));
            await newFile.CreateAsync();

            var response = new AddResponseModel();

            response.Added.Add(await CustomBaseModel.CustomCreateAsync(newFile, path.RootVolume));

            return(await Json(response));
        }
Beispiel #2
0
        public async Task <JsonResult> ListAsync(FullPath path, IEnumerable <string> intersect, IEnumerable <string> mimeTypes)
        {
            var response = new ListResponseModel();

            // Get all files and directories
            var items    = AzureBlobStorageApi.ListFilesAndDirectoriesAsync(path.Directory.FullName);
            var itemList = items.ToList();

            var mimeTypesList = mimeTypes.ToList();

            // Add visible files
            foreach (var file in itemList.Where(i => !i.Name.EndsWith("/")))
            {
                var f = new AzureBlobFile(file);

                if (f.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    continue;
                }

                if (mimeTypesList.Any() && !mimeTypesList.Contains(f.MimeType) && !mimeTypesList.Contains(f.MimeType.Type))
                {
                    continue;
                }

                response.List.Add(f.Name);
            }

            // Add visible directories
            foreach (var file in itemList.Where(i => i.Name.EndsWith("/")))
            {
                var d = new AzureBlobFile(file);

                if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.List.Add(d.Name);
                }
            }

            var intersectList = intersect.ToList();

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

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

            var split = StringHelpers.Split('_', target);

            var root   = Roots.First(r => r.VolumeId == split.Prefix);
            var path   = HttpEncoder.DecodePath(split.Content);
            var dirUrl = path != root.RootDirectory ? path : string.Empty;
            var dir    = new AzureBlobDirectory(root.RootDirectory + dirUrl);

            if (await dir.ExistsAsync)
            {
                return(new FullPath(root, dir, target));
            }

            var file = new AzureBlobFile(root.RootDirectory + dirUrl);

            return(new FullPath(root, file, target));
        }
Beispiel #4
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();

            var fileList = files.ToList();

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

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

            var uploadPathList = uploadPaths.ToList();

            foreach (var uploadPath in uploadPathList)
            {
                var dir = uploadPath.Directory;

                while (dir.FullName != path.RootVolume.RootDirectory)
                {
                    response.Added.Add(await BaseModel.CreateAsync(new AzureBlobDirectory(dir.FullName), path.RootVolume));
                    dir = dir.Parent;
                }
            }

            var i = 0;

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

                var azureFile = new AzureBlobFile(AzureBlobStorageApi.PathCombine(destination, Path.GetFileName(file.FileName)));

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

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

                        response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(azureFile.FullName), path.RootVolume));
                    }
                    else
                    {
                        var newName = await CreateNameForCopy(azureFile, suffix);

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

                        response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newName), path.RootVolume));
                    }
                }
                else
                {
                    await AzureBlobStorageApi.UploadAsync(file, azureFile.FullName);

                    response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(azureFile.FullName), path.RootVolume));
                }

                i++;
            }

            return(await Json(response));
        }
Beispiel #5
0
        public async Task <JsonResult> OpenAsync(FullPath path, bool tree, IEnumerable <string> mimeTypes)
        {
            // todo: BaseModel.CreateAsync internally calls GetDirectoriesAsync() which calls AzureBlobStorageApi.ListFilesAndDirectoriesAsync
            // todo: AzureBlobStorageApi.ListFilesAndDirectoriesAsync is then called again here few lines below;
            // todo: we should be able to reduce it to one single call
            var response = new OpenResponse(await BaseModel.CreateAsync(path.Directory, path.RootVolume), path);

            // Get all files and directories
            var items    = AzureBlobStorageApi.ListFilesAndDirectoriesAsync(path.Directory.FullName);
            var itemList = items.ToList();

            var mimeTypesList = mimeTypes.ToList();

            // Add visible files
            foreach (var file in itemList.Where(i => !i.Name.EndsWith("/")))
            {
                var f = new AzureBlobFile(file);

                if (f.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    continue;
                }

                if (mimeTypesList.Any() && !mimeTypesList.Contains(f.MimeType) && !mimeTypesList.Contains(f.MimeType.Type))
                {
                    continue;
                }

                response.Files.Add(await CustomBaseModel.CustomCreateAsync(f, path.RootVolume));
            }

            // Add visible directories
            foreach (var dir in itemList.Where(i => i.Name.EndsWith("/")))
            {
                var d = new AzureBlobDirectory(dir);

                if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(d, path.RootVolume));
                }
            }

            // Add parents
            if (!tree)
            {
                return(await Json(response));
            }

            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));
        }
Beispiel #6
0
        public async Task <JsonResult> InitAsync(FullPath path, IEnumerable <string> mimeTypes)
        {
            if (path == null)
            {
                var root = Roots.FirstOrDefault(r => r.StartDirectory != null) ?? Roots.First();

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

            // todo: BaseModel.CreateAsync internally calls GetDirectoriesAsync() which calls AzureBlobStorageApi.ListFilesAndDirectoriesAsync
            // todo: AzureBlobStorageApi.ListFilesAndDirectoriesAsync is then called again here few lines below;
            // todo: we should be able to reduce it to one single call
            var response = new InitResponseModel(await BaseModel.CreateAsync(path.Directory, path.RootVolume), new Options(path));

            // Get all files and directories
            var items    = AzureBlobStorageApi.ListFilesAndDirectoriesAsync(path.Directory.FullName);
            var itemList = items.ToList();

            // Add visible files
            foreach (var file in itemList.Where(i => !i.Name.EndsWith("/")))
            {
                var f = new AzureBlobFile(file);

                if (f.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    continue;
                }

                var mimeTypesList = mimeTypes.ToList();

                if (mimeTypesList.Any() && !mimeTypesList.Contains(f.MimeType) && !mimeTypesList.Contains(f.MimeType.Type))
                {
                    continue;
                }

                response.Files.Add(await CustomBaseModel.CustomCreateAsync(f, path.RootVolume));
            }

            // Add visible directories
            foreach (var dir in itemList.Where(i => i.Name.EndsWith("/")))
            {
                var d = new AzureBlobDirectory(dir);

                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 AzureBlobDirectory(root.RootDirectory), root));
            }

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

                // Add visible directories
                foreach (var dir in entries.Where(i => i.Name.EndsWith("/")))
                {
                    var d = new AzureBlobDirectory(dir);

                    if (!d.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        response.Files.Add(await BaseModel.CreateAsync(d, path.RootVolume));
                    }
                }
            }

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

            return(await Json(response));
        }