Esempio n. 1
0
        public async Task <JsonResult> RenameAsync(FullPath path, string name)
        {
            var response = new ReplaceResponseModel();

            response.Removed.Add(path.HashedTarget);
            await RemoveThumbsAsync(path);

            if (path.IsDirectory)
            {
                // Get new path
                var newPath = AzureStorageAPI.PathCombine(path.Directory.Parent.FullName, name);

                // Move file
                await AzureStorageAPI.MoveDirectoryAsync(path.Directory.FullName, newPath);

                // Add it to added entries list
                response.Added.Add(await BaseModel.CreateAsync(new AzureStorageDirectory(newPath), path.RootVolume));
            }
            else
            {
                // Get new path
                var newPath = AzureStorageAPI.PathCombine(path.File.DirectoryName ?? string.Empty, name);

                // Move file
                await AzureStorageAPI.MoveFileAsync(path.File.FullName, newPath);

                // Add it to added entries list
                response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newPath), path.RootVolume));
            }

            return(await Json(response));
        }
        public async Task <ConnectorResult> MakeDirAsync(FullPath path, string name, IEnumerable <string> dirs)
        {
            var response = new AddResponseModel();

            if (!string.IsNullOrEmpty(name))
            {
                var newDir = new FileSystemDirectory(Path.Combine(path.Directory.FullName, name));
                await newDir.CreateAsync();

                response.Added.Add(await BaseModel.CreateAsync(newDir, path.RootVolume));
            }

            foreach (string dir in dirs)
            {
                string dirName = dir.StartsWith("/") ? dir.Substring(1) : dir;
                var    newDir  = new FileSystemDirectory(Path.Combine(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(new ConnectorResult(response));
        }
Esempio n. 3
0
        public async Task <JsonResult> ParentsAsync(FullPath path)
        {
            var response = new TreeResponseModel();

            if (path.Directory.FullName == path.RootVolume.RootDirectory)
            {
                response.Tree.Add(await BaseModel.CreateAsync(path.Directory, path.RootVolume));
            }
            else
            {
                var parent = path.Directory;

                foreach (var item in await parent.Parent.GetDirectoriesAsync())
                {
                    response.Tree.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                }

                while (parent.FullName != path.RootVolume.RootDirectory)
                {
                    parent = parent.Parent;
                    response.Tree.Add(await BaseModel.CreateAsync(parent, path.RootVolume));
                }
            }

            return(await Json(response));
        }
Esempio n. 4
0
        public async Task <JsonResult> PasteAsync(FullPath dest, IEnumerable <FullPath> paths, bool isCut, IEnumerable <string> renames, string suffix)
        {
            var response = new ReplaceResponseModel();

            var pathList = paths.ToList();

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

                        response.Removed.Add(src.HashedTarget);
                    }
                    else
                    {
                        // Copy directory
                        await AzureBlobStorageApi.CopyDirectoryAsync(src.Directory.FullName, newDir.FullName);
                    }

                    response.Added.Add(await BaseModel.CreateAsync(newDir, dest.RootVolume));
                }
                else
                {
                    var newFilePath = AzureBlobStorageApi.PathCombine(dest.Directory.FullName, src.File.Name);
                    await AzureBlobStorageApi.DeleteFileIfExistsAsync(newFilePath);

                    if (isCut)
                    {
                        await RemoveThumbsAsync(src);

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

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

                    response.Added.Add(await CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newFilePath), dest.RootVolume));
                }
            }

            return(await Json(response));
        }
Esempio n. 5
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 <ConnectorResult> SearchAsync(FullPath path, string query, IEnumerable <string> mimeTypes)
        {
            var response = new SearchResponseModel();

            if (!query.Any(Path.GetInvalidFileNameChars().Contains))
            {
                foreach (var item in await path.Directory.GetFilesAsync(mimeTypes, string.Concat("*", query, "*")))
                {
                    if (!item.Attributes.HasFlag(FileAttributes.Hidden) && !item.Directory.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        response.Files.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                    }
                }

                if (!mimeTypes.Any())
                {
                    foreach (var item in await path.Directory.GetDirectoriesAsync(string.Concat("*", query, "*")))
                    {
                        if (!item.Attributes.HasFlag(FileAttributes.Hidden))
                        {
                            response.Files.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                        }
                    }
                }
            }

            return(new ConnectorResult(response));
        }
        public async Task <ConnectorResult> ArchiveAsync(FullPath parentPath, IEnumerable <FullPath> paths, string filename, string mimeType)
        {
            var response = new AddResponseModel();

            if (paths == null)
            {
                throw new NotSupportedException();
            }

            if (mimeType != "application/zip")
            {
                throw new NotSupportedException("Only .zip files are currently supported.");
            }

            // Parse target path

            var directoryInfo = parentPath.Directory;

            if (directoryInfo != null)
            {
                if (filename is null)
                {
                    filename = "newfile";
                }

                if (filename.EndsWith(".zip"))
                {
                    filename = filename.Replace(".zip", "");
                }

                string newPath = Path.Combine(directoryInfo.FullName, filename + ".zip");

                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }

                using (var newFile = ZipFile.Open(newPath, ZipArchiveMode.Create))
                {
                    foreach (var tg in paths)
                    {
                        if (tg.IsDirectory)
                        {
                            await AddDirectoryToArchiveAsync(newFile, tg.Directory, "");
                        }
                        else
                        {
                            newFile.CreateEntryFromFile(tg.File.FullName, tg.File.Name);
                        }
                    }
                }

                response.Added.Add(await BaseModel.CreateAsync(new FileSystemFile(newPath), parentPath.RootVolume));
            }

            return(new ConnectorResult(response));
        }
        public async Task <ConnectorResult> MakeFileAsync(FullPath path, string name)
        {
            var newFile = new FileSystemFile(Path.Combine(path.Directory.FullName, name));
            await newFile.CreateAsync();

            var response = new AddResponseModel();

            response.Added.Add(await BaseModel.CreateAsync(newFile, path.RootVolume));
            return(new ConnectorResult(response));
        }
Esempio n. 9
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));
        }
Esempio n. 10
0
        public async Task <JsonResult> PutAsync(FullPath path, byte[] content)
        {
            var response = new ChangedResponseModel();

            // Write content
            await AzureStorageAPI.PutAsync(path.File.FullName, content);

            response.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));
            return(await Json(response));
        }
Esempio n. 11
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 FileSystemDirectory(root.StartDirectory ?? root.RootDirectory), null);
            }

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

            foreach (var item in await path.Directory.GetFilesAsync(mimeTypes))
            {
                if (!item.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                }
            }
            foreach (var item in await path.Directory.GetDirectoriesAsync())
            {
                if (!item.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                }
            }

            foreach (var item in Roots)
            {
                response.Files.Add(await BaseModel.CreateAsync(new FileSystemDirectory(item.RootDirectory), item));
            }

            if (path.RootVolume.RootDirectory != path.Directory.FullName)
            {
                var dirInfo = new DirectoryInfo(path.RootVolume.RootDirectory);
                foreach (var item in dirInfo.GetDirectories())
                {
                    var attributes = item.Attributes;
                    if (!attributes.HasFlag(FileAttributes.Hidden))
                    {
                        response.Files.Add(await BaseModel.CreateAsync(new FileSystemDirectory(item), path.RootVolume));
                    }
                }
            }

            if (path.RootVolume.MaxUploadSize.HasValue)
            {
                response.Options.UploadMaxSize = $"{path.RootVolume.MaxUploadSizeInKb.Value}K";
            }
            return(await Json(response));
        }
Esempio n. 12
0
        public async Task <JsonResult> PutAsync(FullPath path, byte[] content)
        {
            var response = new ChangedResponseModel();

            using (var fileStream = new FileStream(path.File.FullName, FileMode.Create))
                using (var writer = new BinaryWriter(fileStream))
                {
                    writer.Write(content);
                }
            response.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));
            return(await Json(response));
        }
        public async Task <ConnectorResult> PutAsync(FullPath path, string content)
        {
            var response = new ChangedResponseModel();

            using (var fileStream = new FileStream(path.File.FullName, FileMode.Create))
                using (var writer = new StreamWriter(fileStream))
                {
                    writer.Write(content);
                }
            response.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));
            return(new ConnectorResult(response));
        }
Esempio n. 14
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 FileSystemDirectory(Path.Combine(dest.Directory.FullName, src.Directory.Name));
                    if (await newDir.ExistsAsync)
                    {
                        Directory.Delete(newDir.FullName, true);
                    }

                    if (isCut)
                    {
                        await RemoveThumbsAsync(src);

                        Directory.Move(src.Directory.FullName, newDir.FullName);
                        response.Removed.Add(src.HashedTarget);
                    }
                    else
                    {
                        DirectoryCopy(src.Directory.FullName, newDir.FullName, true);
                    }

                    response.Added.Add(await BaseModel.CreateAsync(newDir, dest.RootVolume));
                }
                else
                {
                    var newFile = new FileSystemFile(Path.Combine(dest.Directory.FullName, src.File.Name));
                    if (await newFile.ExistsAsync)
                    {
                        await newFile.DeleteAsync();
                    }

                    if (isCut)
                    {
                        await RemoveThumbsAsync(src);

                        File.Move(src.File.FullName, newFile.FullName);
                        response.Removed.Add(src.HashedTarget);
                    }
                    else
                    {
                        File.Copy(src.File.FullName, newFile.FullName);
                    }
                    response.Added.Add(await BaseModel.CreateAsync(newFile, dest.RootVolume));
                }
            }
            return(await Json(response));
        }
Esempio n. 15
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));
        }
Esempio n. 16
0
        public async Task <JsonResult> TreeAsync(FullPath path)
        {
            var response = new TreeResponseModel();

            foreach (var item in await path.Directory.GetDirectoriesAsync())
            {
                if (!item.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Tree.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                }
            }
            return(await Json(response));
        }
Esempio n. 17
0
        /// <summary>
        /// Сохранить текстовый документ
        /// </summary>
        /// <param name="target">Цель в формате elFinder</param>
        /// <param name="content">Текст</param>
        /// <param name="conv">Кодировка</param>
        async public Task <JsonResult> PutAsync(FullPath path, string content, string conv)
        {
            var response = new ChangedResponseModel();

            using (var fileStream = new FileStream(path.File.FullName, FileMode.Create))
            {
                using (var writer = new StreamWriter(fileStream, GetEncoding(conv)))
                {
                    writer.Write(content);
                }
            }
            response.Changed.Add((FileModel)await BaseModel.CreateAsync(this, path.File, path.RootVolume));
            return(Json(response));
        }
Esempio n. 18
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));
        }
Esempio n. 19
0
        public async Task <JsonResult> RotateAsync(FullPath path, int degree)
        {
            await RemoveThumbsAsync(path);

            // Crop Image
            ImageWithMimeType image;

            using (var stream = await path.File.OpenReadAsync())
            {
                image = path.RootVolume.PictureEditor.Rotate(stream, degree);
            }

            await AzureStorageAPI.PutAsync(path.File.FullName, image.ImageStream);

            var output = new ChangedResponseModel();

            output.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));
            return(await Json(output));
        }
Esempio n. 20
0
        public async Task <JsonResult> ResizeAsync(FullPath path, int width, int height)
        {
            await RemoveThumbsAsync(path);

            // Resize Image
            ImageWithMimeType image;

            await using (var stream = await path.File.OpenReadAsync())
            {
                image = path.RootVolume.PictureEditor.Resize(stream, width, height);
            }

            await AzureBlobStorageApi.PutAsync(path.File.FullName, image.ImageStream);

            var output = new ChangedResponseModel();

            output.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));

            return(await Json(output));
        }
Esempio n. 21
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);

            foreach (var item in await path.Directory.GetFilesAsync(mimeTypes))
            {
                if (!item.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                }
            }
            foreach (var item in await path.Directory.GetDirectoriesAsync())
            {
                if (!item.Attributes.HasFlag(FileAttributes.Hidden))
                {
                    response.Files.Add(await BaseModel.CreateAsync(item, path.RootVolume));
                }
            }

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

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

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

            return(await Json(response));
        }
        public async Task <ConnectorResult> RotateAsync(FullPath path, int degree)
        {
            await RemoveThumbsAsync(path);

            // Rotate Image
            ImageWithMimeType image;

            using (var stream = new FileStream(path.File.FullName, FileMode.Open))
            {
                image = path.RootVolume.PictureEditor.Rotate(stream, degree);
            }

            using (var fileStream = File.Create(path.File.FullName))
            {
                await image.ImageStream.CopyToAsync(fileStream);
            }

            var output = new ChangedResponseModel();

            output.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));
            return(new ConnectorResult(output));
        }
Esempio n. 23
0
        public async Task <JsonResult> RenameAsync(FullPath path, string name)
        {
            var response = new ReplaceResponseModel();

            response.Removed.Add(path.HashedTarget);
            await RemoveThumbsAsync(path);

            if (path.IsDirectory)
            {
                var newPath = new FileSystemDirectory(Path.Combine(path.Directory.Parent.FullName, name));
                Directory.Move(path.Directory.FullName, newPath.FullName);
                response.Added.Add(await BaseModel.CreateAsync(newPath, path.RootVolume));
            }
            else
            {
                var newPath = new FileSystemFile(Path.Combine(path.File.DirectoryName, name));
                File.Move(path.File.FullName, newPath.FullName);
                response.Added.Add(await BaseModel.CreateAsync(newPath, path.RootVolume));
            }

            return(await Json(response));
        }
Esempio n. 24
0
        public async Task <JsonResult> CropAsync(FullPath path, int x, int y, int width, int height)
        {
            await RemoveThumbsAsync(path);

            // Crop Image
            ImageWithMimeType image;

            using (var stream = new FileStream(path.File.FullName, FileMode.Open))
            {
                image = path.RootVolume.PictureEditor.Crop(stream, x, y, width, height);
            }

            using (var fileStream = File.Create(path.File.FullName))
            {
                await image.ImageStream.CopyToAsync(fileStream);
            }

            var output = new ChangedResponseModel();

            output.Changed.Add(await BaseModel.CreateAsync(path.File, path.RootVolume));
            return(await Json(output));
        }
Esempio n. 25
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));
        }
Esempio n. 26
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));
        }
Esempio n. 27
0
        public async Task <JsonResult> DuplicateAsync(IEnumerable <FullPath> paths)
        {
            var response = new AddResponseModel();

            foreach (var path in paths)
            {
                if (path.IsDirectory)
                {
                    var    parentPath = path.Directory.Parent.FullName;
                    var    name       = path.Directory.Name;
                    string newName    = $"{parentPath}/{name} copy";

                    // Check if directory already exists
                    if (!await AzureStorageAPI.DirectoryExistsAsync(newName))
                    {
                        // Doesn't exist
                        await AzureStorageAPI.CopyDirectoryAsync(path.Directory.FullName, newName);
                    }
                    else
                    {
                        // Already exists, create numbered copy
                        var newNameFound = false;
                        for (int i = 1; i < 100; i++)
                        {
                            newName = $"{parentPath}/{name} copy {i}";

                            // Test that it doesn't exist
                            if (!await AzureStorageAPI.DirectoryExistsAsync(newName))
                            {
                                await AzureStorageAPI.CopyDirectoryAsync(path.Directory.FullName, newName);

                                newNameFound = true;
                                break;
                            }
                        }

                        // Check if new name was found
                        if (!newNameFound)
                        {
                            return(Error.NewNameSelectionException($@"{parentPath}/{name} copy"));
                        }
                    }

                    response.Added.Add(await BaseModel.CreateAsync(new AzureStorageDirectory(newName), path.RootVolume));
                }
                else // File
                {
                    var parentPath = path.File.Directory.FullName;
                    var name       = path.File.Name.Substring(0, path.File.Name.Length - path.File.Extension.Length);
                    var ext        = path.File.Extension;

                    string newName = $"{parentPath}/{name} copy{ext}";

                    // Check if file already exists
                    if (!await AzureStorageAPI.FileExistsAsync(newName))
                    {
                        // Doesn't exist
                        await AzureStorageAPI.CopyFileAsync(path.File.FullName, newName);
                    }
                    else
                    {
                        // Already exists, create numbered copy
                        var newNameFound = false;
                        for (var i = 1; i < 100; i++)
                        {
                            // Compute new name
                            newName = $@"{parentPath}/{name} copy {i}{ext}";

                            // Test that it doesn't exist
                            if (!await AzureStorageAPI.FileExistsAsync(newName))
                            {
                                await AzureStorageAPI.CopyFileAsync(path.File.FullName, newName);

                                newNameFound = true;
                                break;
                            }
                        }

                        // Check if new name was found
                        if (!newNameFound)
                        {
                            return(Error.NewNameSelectionException($@"{parentPath}/{name} copy"));
                        }
                    }

                    response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newName), path.RootVolume));
                }
            }
            return(await Json(response));
        }
Esempio n. 28
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));
        }
Esempio n. 29
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));
        }
Esempio n. 30
0
        public async Task <JsonResult> ArchiveAsync(FullPath parentPath, IEnumerable <FullPath> paths, string filename, string mimeType)
        {
            var response = new AddResponseModel();

            if (paths == null)
            {
                throw new NotSupportedException();
            }

            if (mimeType != "application/zip")
            {
                throw new NotSupportedException("Only .zip files are currently supported.");
            }

            // Parse target path

            var directoryInfo = parentPath.Directory;

            if (directoryInfo != null)
            {
                filename ??= "newfile";

                if (filename.EndsWith(".zip"))
                {
                    filename = filename.Replace(".zip", "");
                }

                var newPath = AzureStorageAPI.PathCombine(directoryInfo.FullName, filename + ".zip");
                await AzureStorageAPI.DeleteFileIfExistsAsync(newPath);

                var archivePath = Path.GetTempFileName();
                using (var newFile = ZipFile.Open(archivePath, ZipArchiveMode.Update))
                {
                    foreach (var tg in paths)
                    {
                        if (tg.IsDirectory)
                        {
                            await AddDirectoryToArchiveAsync(newFile, tg.Directory, "");
                        }
                        else
                        {
                            var filePath = Path.GetTempFileName();
                            File.WriteAllBytes(filePath, await AzureStorageAPI.FileBytesAsync(tg.File.FullName));
                            newFile.CreateEntryFromFile(filePath, tg.File.Name);
                        }
                    }
                }

                using (var stream = new FileStream(archivePath, FileMode.Open))
                {
                    await AzureStorageAPI.PutAsync(newPath, stream);
                }

                // Cleanup
                File.Delete(archivePath);

                response.Added.Add(await BaseModel.CreateAsync(new AzureStorageFile(newPath), parentPath.RootVolume));
            }

            return(await Json(response));
        }