Beispiel #1
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));
        }