/// <summary> /// Process a renamed event /// </summary> /// <param name="factory"></param> /// <param name="fileSystemEvent"></param> /// <param name="token"></param> protected abstract Task ProcessRenamedAsync(IConnectionFactory factory, IRenamedFileSystemEvent fileSystemEvent, CancellationToken token);
/// <inheritdoc /> protected override async Task ProcessRenamedAsync(IConnectionFactory factory, IRenamedFileSystemEvent e, CancellationToken token) { // get the new name as well as the one one // either of those could be null var file = e.FileSystemInfo as FileInfo; var oldFile = e.PreviousFileSystemInfo as FileInfo; // if both are null then we cannot do anything with it if (null == file && null == oldFile) { Logger.Error($"File event: I was unable to use the renamed files, (old:{e.PreviousFullName} / new:{e.FullName})"); return; } // if the old directory is null then we can use // the new directory only. if (null == oldFile) { // but of course, only if it is usable as well... if (!CanProcessFile(file)) { return; } // just add the new directly. if (!await Files.AddOrUpdateFileAsync(file, token).ConfigureAwait(false)) { Logger.Error($"File event: Unable to add file {e.FullName} during rename."); } return; } // so we now know that the old file is not null // so the new directory could be null and/or not usubale. // so, if we cannot use it, we will simply delete the old one. if (!CanProcessFile(file)) { // if the old path is not an ignored path // then we might be able to delete that file. if (Directory.IsIgnored(oldFile)) { return; } // if the old file does not exist on record ... then there is nothing more for us to do . if (!await Files.FileExistsAsync(oldFile, token).ConfigureAwait(false)) { return; } // delete the old folder only, in case it did exist. if (!await Files.DeleteFileAsync(oldFile, token).ConfigureAwait(false)) { Logger.Error($"File event: Unable to remove old file {e.PreviousFullName} durring rename"); } return; } // the given file is going to be processed. Logger.Verbose($"File event: {e.PreviousFullName} > {e.FullName} (Renamed)"); // at this point we know we have a new file that we can use // and an old file that we could also use. // // if we do not have the old file on record then it is not a rename // but rather is is a new one. if (!await Files.FileExistsAsync(oldFile, token).ConfigureAwait(false)) { // just add the new directly. if (!await Files.AddOrUpdateFileAsync(file, token).ConfigureAwait(false)) { Logger.Error($"File event: Unable to add file {e.FullName} during rename."); } return; } // we have the old name on record so we can try and rename it. // if we ever have an issue, it could be because we are trying to rename to // something that already exists. if (-1 == await Files.RenameOrAddFileAsync(file, oldFile, token).ConfigureAwait(false)) { Logger.Error($"File event: Unable to rename file {e.PreviousFullName} > {e.FullName}"); } }
private async Task OnRenamedAsync(IRenamedFileSystemEvent rfse, CancellationToken token) { await AddMessage(ConsoleColor.Cyan, rfse.DateTimeUtc, $"[{(rfse.IsFile ? "F" : "D")}][R]:{rfse.PreviousFileSystemInfo.FullName} > {rfse.FileSystemInfo.FullName}", token).ConfigureAwait(false); }
private Task OnRenamedAsync(IRenamedFileSystemEvent rfse, CancellationToken token) { _output.AddInformationMessage(rfse.DateTimeUtc, $"[{(rfse.IsFile ? "F" : "D")}][R]:{rfse.PreviousFileSystemInfo.FullName} > {rfse.FileSystemInfo.FullName}", token); return(Task.CompletedTask); }