コード例 #1
0
        public async Task <JsonResult> PasteAsync(FullPath dest, IEnumerable <FullPath> paths, bool isCut)
        {
            var response = new ReplaceResponseModel();

            foreach (var src in paths)
            {
                if (src.IsDirectory)
                {
                    var newDir = new AzureStorageDirectory(AzureStorageAPI.UrlCombine(dest.Directory.FullName, src.Directory.Name));

                    // Check if it already exists
                    if (await newDir.ExistsAsync)
                    {
                        // Exists
                        await newDir.DeleteAsync();
                    }

                    if (isCut)
                    {
                        RemoveThumbs(src);
                        await AzureStorageAPI.MoveDirectory(src.Directory.FullName, newDir.FullName);

                        response.Removed.Add(src.HashedTarget);
                    }
                    else
                    {
                        // Copy directory
                        await AzureStorageAPI.CopyDirectory(src.Directory.FullName, newDir.FullName);
                    }
                    response.Added.Add(await BaseModel.Create(this, newDir, dest.RootVolume));
                }
                else
                {
                    string newFilePath = AzureStorageAPI.UrlCombine(dest.Directory.FullName, src.File.Name);
                    await AzureStorageAPI.DeleteFileIfExists(newFilePath);

                    if (isCut)
                    {
                        RemoveThumbs(src);

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

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

                    response.Added.Add(await BaseModel.Create(this, new AzureStorageFile(newFilePath), dest.RootVolume));
                }
            }

            return(await Json(response));
        }
コード例 #2
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.DirectoryExists(newName))
                    {
                        // Doesn't exist
                        await AzureStorageAPI.CopyDirectory(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.DirectoryExists(newName))
                            {
                                await AzureStorageAPI.CopyDirectory(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.Create(this, 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.FileExists(newName))
                    {
                        // Doesn't exist
                        await AzureStorageAPI.CopyFile(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.FileExists(newName))
                            {
                                await AzureStorageAPI.CopyFile(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.Create(this, new AzureStorageFile(newName), path.RootVolume));
                }
            }
            return(await Json(response));
        }