public bool Rename(FileRenameInfo renameInfo) { var pathName = System.IO.Path.GetFileName(renameInfo.CurrentPath); if (pathName != renameInfo.WantedName) { try { var newNamePath = $"{Path.GetDirectoryName(renameInfo.CurrentPath)}\\{renameInfo.WantedName}"; File.Move(renameInfo.CurrentPath, newNamePath); //flip them for later //so we can rename them back to original var tempWantedName = renameInfo.WantedName; renameInfo.WantedName = renameInfo.CurrentName; renameInfo.CurrentName = tempWantedName; renameInfo.CurrentPath = newNamePath; return(true); }catch { return(false); } } else { return(true); } }
internal void LoadImageAsync(FileRenameInfo data, bool applyExifRotation = true) { Dispatcher.Invoke((Action)(() => { var filename = data.GetFileName(); var rotation = applyExifRotation ? GetRotation(data.Exif) : 0.0; // load image var image = new BitmapImage(); try { using (var stream = File.OpenRead(filename)) { image.BeginInit(); image.StreamSource = stream; image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); // load the image from the stream } SetValue(ImageProperty, image); SetValue(RotationProperty, rotation); } catch (Exception ex) { // An error occurred while loading the image file (maybe it does not exist any more...) // Let's make an image indicating the problem. SetValue(ImageProperty, CreateErrorImage(ex)); } })); }
public FileRenameInfo GetFileRenameInfo(string filename) { if (!ShouldBeRenamed(filename)) { new FileRenameInfo(filename, filename); } if (_regex1.IsMatch(filename)) { var result = _regex1.Matches(filename); var matchCollection = _regex1.Matches(filename); var match = matchCollection.Single(); var newFilename = string.Format($"{match.Groups["path"]}{match.Groups["preFix"]}{match.Groups["fileNumber"]}_{match.Groups["index"]}.{match.Groups["fileType"]}"); var fileRenameInfo = new FileRenameInfo(filename, newFilename); return(fileRenameInfo); } else if (_regex2.IsMatch(filename)) { var result = _regex2.Matches(filename); var matchCollection = _regex2.Matches(filename); var match = matchCollection.Single(); var newFilename = string.Format($"{match.Groups["path"]}GP{match.Groups["fileNumber"]}_00.{match.Groups["fileType"]}"); var fileRenameInfo = new FileRenameInfo(filename, newFilename); return(fileRenameInfo); } throw new InvalidOperationException("How did you get here?!?"); }
internal void LoadImageAsync(FileRenameInfo data, bool applyExifRotation = true) { Dispatcher.Invoke((Action)(() => { var filename = data.GetFileName(); var rotation = applyExifRotation ? GetRotation(data.Exif) : 0.0; // load image var image = new BitmapImage(); using (var stream = File.OpenRead(filename)) { image.BeginInit(); image.StreamSource = stream; image.CacheOption = BitmapCacheOption.OnLoad; image.EndInit(); // load the image from the stream } SetValue(ImageProperty, image); //WpfHelper.ReadBitmapSource(filename)); SetValue(RotationProperty, rotation); })); }
private bool SanitizePathNeeded(PkgInfo pkgInfo, out FileRenameInfo fileRenameInfo) { fileRenameInfo = null; var pattern = @"^[\\a-zA-Z0-9-_\.]+$"; var currentFileName = Path.GetFileName(pkgInfo.FilePath); var match = Regex.Match(currentFileName, pattern); if (!Regex.IsMatch(currentFileName, pattern)) { fileRenameInfo = new FileRenameInfo() { CurrentName = currentFileName, CurrentPath = Path.GetFullPath(pkgInfo.FilePath), }; pattern = pattern.Substring(1, pattern.Length - 2); string newFileName = ""; var newMatch = Regex.Match(currentFileName, pattern); while (newMatch.Success) { newFileName += newMatch.Value; newMatch = newMatch.NextMatch(); } int maxFileNameSize = 60; if (newFileName.Length > maxFileNameSize) { //PS4 only seems to like titles < 60 (including ".pkg") //So we eliminate the remainder from the start //this is only used for renaming files, nothing more //so it does not matter if the name looks weird. It's only temporary :) newFileName = newFileName.Remove(0, newFileName.Length - maxFileNameSize); } fileRenameInfo.WantedName = newFileName; return(true); } return(false); }