public async Task List(ImportArguments arguments) { var session = configuration.StartSession(); var assets = session.Assets; var folder = new DirectoryInfo(arguments.Folder); var folderTree = new FolderTree(session); foreach (var file in folder.GetFiles("*.*", SearchOption.AllDirectories)) { var relativeFolder = Path.GetRelativePath(folder.FullName, file.Directory.FullName); var relativePath = Path.GetRelativePath(folder.FullName, file.FullName); var targetFolder = arguments.TargetFolder; if (!string.IsNullOrWhiteSpace(relativePath) && relativePath != ".") { targetFolder = Path.Combine(targetFolder, relativeFolder); } var parentId = await folderTree.GetIdAsync(targetFolder); await log.DoSafeLineAsync($"Uploading {relativePath}", async() => { await assets.PostAssetAsync(session.App, parentId, duplicate: arguments.Duplicate, file: file); }); } log.WriteLine("> Import completed"); }
private static Task <string?> MapFoldersAsync(string?folderId, FolderTree folders, bool fromId) { if (fromId) { return(folders.GetPathAsync(folderId)); } else { return(folders.GetIdAsync(folderId)); } }
public async Task Export(ImportArguments arguments) { var session = configuration.StartSession(arguments.App); var assets = session.Assets; using (var fs = await FileSystems.CreateAsync(arguments.Path, session.WorkingDirectory)) { var folderTree = new FolderTree(session); var folderNames = new HashSet <string>(); var parentId = await folderTree.GetIdAsync(arguments.TargetFolder); var downloadPipeline = new DownloadPipeline(session, log, fs) { FilePathProviderAsync = async asset => { var assetFolder = await folderTree.GetPathAsync(asset.ParentId); var assetPath = asset.FileName; if (!string.IsNullOrWhiteSpace(assetFolder)) { assetPath = Path.Combine(assetFolder, assetPath); } if (!folderNames.Add(assetPath)) { assetPath = Path.Combine(assetFolder !, $"{asset.Id}_{asset.FileName}"); } return(FilePath.Create(assetPath)); } }; try { await assets.GetAllByQueryAsync(session.App, async asset => { await downloadPipeline.DownloadAsync(asset); }, new AssetQuery { ParentId = parentId }); } finally { await downloadPipeline.CompleteAsync(); } log.WriteLine("> Export completed"); } }
public async Task Import(ImportArguments arguments) { var session = configuration.StartSession(arguments.App); var assets = session.Assets; using (var fs = await FileSystems.CreateAsync(arguments.Path, session.WorkingDirectory)) { var folders = new FolderTree(session); var assetQuery = new AssetQuery(); foreach (var file in fs.GetFiles(FilePath.Root, ".*")) { var targetFolder = file.LocalFolderPath(); if (!string.IsNullOrWhiteSpace(arguments.TargetFolder)) { targetFolder = Path.Combine(arguments.TargetFolder, targetFolder); } assetQuery.ParentId = await folders.GetIdAsync(targetFolder); assetQuery.Filter = $"fileName eq '{file.Name}'"; var existings = await assets.GetAssetsAsync(session.App, assetQuery); var existing = existings.Items.FirstOrDefault(); var fileHash = file.GetFileHash(); try { var fileParameter = new FileParameter(file.OpenRead(), file.Name, MimeTypesMap.GetMimeType(file.Name)); log.WriteLine($"Uploading: {file.FullName}"); if (existings.Items.Any(x => string.Equals(x.FileHash, fileHash, StringComparison.Ordinal))) { log.StepSkipped("Same hash."); } else if (existings.Items.Count > 1) { log.StepSkipped("Multiple candidates found."); } else if (existing != null) { await assets.PutAssetContentAsync(session.App, existing.Id, fileParameter); log.StepSuccess("Existing Asset"); } else { var result = await assets.PostAssetAsync(session.App, assetQuery.ParentId, null, arguments.Duplicate, fileParameter); if (result._meta?.IsDuplicate == "true") { log.StepSkipped("duplicate."); } else { log.StepSuccess("New Asset"); } } } catch (Exception ex) { LogExtensions.HandleException(ex, error => log.WriteLine("Error: {0}", error)); } finally { log.WriteLine(); } } log.WriteLine("> Import completed"); } }
public async Task Should_provide_null_if_for_root_path(string path) { var id = await sut.GetIdAsync(path); Assert.Null(id); }