Exemple #1
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));
        }
Exemple #2
0
        public async Task <JsonResult> DuplicateAsync(IEnumerable <FullPath> paths)
        {
            var response = new AddResponseModel();

            var pathList = paths.ToList();

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

                    // Check if directory already exists
                    if (!await AzureBlobStorageApi.DirectoryExistsAsync(newName))
                    {
                        // Doesn't exist
                        await AzureBlobStorageApi.CopyDirectoryAsync(path.Directory.FullName, newName);
                    }
                    else
                    {
                        // Already exists, create numbered copy
                        var newNameFound = false;

                        for (var i = 1; i < 100; i++)
                        {
                            newName = $"{parentPath}/{name} copy {i}";

                            // Test that it doesn't exist
                            if (await AzureBlobStorageApi.DirectoryExistsAsync(newName))
                            {
                                continue;
                            }

                            await AzureBlobStorageApi.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 AzureBlobDirectory(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;

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

                    // Check if file already exists
                    if (!await AzureBlobStorageApi.FileExistsAsync(newName))
                    {
                        // Doesn't exist
                        await AzureBlobStorageApi.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 AzureBlobStorageApi.FileExistsAsync(newName))
                            {
                                continue;
                            }

                            await AzureBlobStorageApi.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 CustomBaseModel.CustomCreateAsync(new AzureBlobFile(newName), path.RootVolume));
                }
            }

            return(await Json(response));
        }