/// <summary> /// Ensure we have a good destination name and determine whether that file exists already. /// </summary> /// <param name="task"></param> public void BuildDestinationForItem(CopyTask task) { string targetDirFileName = DetermineTargetDir(task); task.DestinationFile = BuildUniqueDestinationFileName(targetDirFileName); task.FileExistsAlready = File.Exists(task.DestinationFile); }
public async Task ExecuteCopyTask(CopyTask copyTask) { FileHelper.CreateDirectoryForFile(copyTask.DestinationFile); await FileHelper.CopyFile(copyTask); copyTask.FileCopiedOn = DateTime.Now; }
/// <summary> /// Copy a file from one location to another. /// </summary> /// <param name="copyTask"></param> /// <returns></returns> public async Task CopyFile(CopyTask copyTask) { byte[] fileBytes = File.ReadAllBytes(copyTask.SourceFile); using (FileStream destStream = new FileStream(copyTask.DestinationFile, FileMode.CreateNew, FileAccess.Write, FileShare.Write, bufferSize: 4096, useAsync: true)) { await destStream.WriteAsync(fileBytes, 0, fileBytes.Length); }; }
/// <summary> /// Find all files in the source directory, and create a collection of CopyTasks. Analyzes the destination for each file, determines those that have been previously copied and should be skipped. Determines the destination file name. /// </summary> /// <returns>A list of CopyTask objects. </returns> public IEnumerable<CopyTask> BuildFileCopyTasks() { var allMatchingFilesInDir = FileHelper.GetAllFilesWithExtensions(Config.SourceDir, Config.FileExtensions); foreach (var mediaFile in allMatchingFilesInDir) { var task = new CopyTask { SourceFile = mediaFile.FullName, ImageTakenOnDate = MediaComparer.GetImageTakenOnDate(mediaFile.FullName), FileCreatedOn = mediaFile.CreationTime, }; BuildDestinationForItem(task); yield return task; } }
/// <summary> /// Find all files in the source directory, and create a collection of CopyTasks. Analyzes the destination for each file, determines those that have been previously copied and should be skipped. Determines the destination file name. /// </summary> /// <returns>A list of CopyTask objects. </returns> public IEnumerable <CopyTask> BuildFileCopyTasks() { var allMatchingFilesInDir = FileHelper.GetAllFilesWithExtensions(Config.SourceDir, Config.FileExtensions); foreach (var mediaFile in allMatchingFilesInDir) { var task = new CopyTask { SourceFile = mediaFile.FullName, ImageTakenOnDate = MediaComparer.GetImageTakenOnDate(mediaFile.FullName), FileCreatedOn = mediaFile.CreationTime, }; BuildDestinationForItem(task); yield return(task); } }
/// <summary> /// Builds the target file path directory in the user's pref. Like year/monthname/date/file.jpg. Take it from the config. /// </summary> /// <param name="task"></param> /// <returns></returns> private string DetermineTargetDir(CopyTask task) { DateTime targetDate = task.ImageTakenOnDate.GetValueOrDefault(task.FileCreatedOn); var replacements = new Dictionary<string, string> { {"{year}", targetDate.Year.ToString()}, {"{month}", targetDate.ToShortMonthName()}, {"{day}", targetDate.Day.ToString()} }; string destinationDir = Config.DestinationDir; foreach (var item in replacements) { destinationDir = destinationDir.Replace(item.Key, item.Value); } return Path.Combine(destinationDir, Path.GetFileName(task.SourceFile)); }
/// <summary> /// Builds the target file path directory in the user's pref. Like year/monthname/date/file.jpg. Take it from the config. /// </summary> /// <param name="task"></param> /// <returns></returns> private string DetermineTargetDir(CopyTask task) { DateTime targetDate = task.ImageTakenOnDate.GetValueOrDefault(task.FileCreatedOn); var replacements = new Dictionary <string, string> { { "{year}", targetDate.Year.ToString() }, { "{month}", targetDate.ToShortMonthName() }, { "{day}", targetDate.Day.ToString() } }; string destinationDir = Config.DestinationDir; foreach (var item in replacements) { destinationDir = destinationDir.Replace(item.Key, item.Value); } return(Path.Combine(destinationDir, Path.GetFileName(task.SourceFile))); }