public void TestFileUris() { foreach (var test in tests) { var suri = new SafeUri (test.Uri); Assert.AreEqual (suri.AbsoluteUri, test.AbsoluteUri, String.Format("AbsoluteUri for {0}", test.Uri)); Assert.AreEqual (suri.GetExtension (), test.Extension, String.Format("Extension for {0}", test.Uri)); Assert.AreEqual (suri.GetBaseUri ().ToString (), test.BaseUri, String.Format("BaseUri for {0}", test.Uri)); Assert.AreEqual (suri.GetFilename (), test.Filename, String.Format("Filename for {0}", test.Uri)); Assert.AreEqual (suri.GetFilenameWithoutExtension (), test.FilenameWithoutExtension, String.Format("FilenameWithoutExtension for {0}", test.Uri)); } }
public static bool IsRaw (SafeUri uri) { string [] raw_extensions = { ".arw", ".crw", ".cr2", ".dng", ".mrw", ".nef", ".orf", ".pef", ".raw", ".raf", ".rw2", ".srw", }; var extension = uri.GetExtension ().ToLower (); return raw_extensions.Any (x => x == extension); }
public static bool IsJpeg (SafeUri uri) { string [] jpg_extensions = {".jpg", ".jpeg", ".jpe", ".jfi", ".jfif", ".jif"}; var extension = uri.GetExtension ().ToLower (); return jpg_extensions.Any (x => x == extension); }
static Type GetLoaderType (SafeUri uri) { // check if GIO can find the file, which is not the case // with filenames with invalid encoding var file = GLib.FileFactory.NewForUri (uri); if (!file.Exists) return null; string extension = uri.GetExtension ().ToLower (); // Ignore video thumbnails if (extension == ".thm") return null; // Detect mime-type var info = file.QueryInfo ("standard::content-type,standard::size", FileQueryInfoFlags.None, null); string mime = info.ContentType; long size = info.Size; // Empty file if (size == 0) return null; Type t = null; if (NameTable.TryGetValue (mime, out t)) return t; if (NameTable.TryGetValue (extension, out t)) return t; return null; }
SafeUri GetUniqueFilename (SafeUri dest) { // Find an unused name int i = 1; var base_uri = dest.GetBaseUri (); var filename = dest.GetFilenameWithoutExtension (); var extension = dest.GetExtension (); while (file_system.File.Exists (dest)) { dest = base_uri.Append (String.Format ("{0}-{1}{2}", filename, i++, extension)); } return dest; }
SafeUri FindImportDestination(SafeUri uri, DateTime time) { // Find a new unique location inside the photo folder string name = uri.GetFilename (); var dest_uri = FSpot.Core.Global.PhotoUri.Append (time.Year.ToString ()) .Append (String.Format ("{0:D2}", time.Month)) .Append (String.Format ("{0:D2}", time.Day)); EnsureDirectory (dest_uri); // If the destination we'd like to use is the file itself return that if (dest_uri.Append (name) == uri) return uri; // Find an unused name int i = 1; var dest = dest_uri.Append (name); var file = GLib.FileFactory.NewForUri (dest); while (file.Exists) { var filename = uri.GetFilenameWithoutExtension (); var extension = uri.GetExtension (); dest = dest_uri.Append (String.Format ("{0}-{1}{2}", filename, i++, extension)); file = GLib.FileFactory.NewForUri (dest); } return dest; }
private static void SaveToSuitableFormat(SafeUri destination, Pixbuf pixbuf, uint jpeg_quality) { // FIXME: this needs to work on streams rather than filenames. Do that when we switch to // newer GDK. var extension = destination.GetExtension ().ToLower (); if (extension == ".png") pixbuf.Save (destination.LocalPath, "png"); else if (extension == ".jpg" || extension == ".jpeg") pixbuf.Save (destination.LocalPath, "jpeg", jpeg_quality); else throw new NotImplementedException ("Saving this file format is not supported"); }
public static void CreateDerivedVersion(SafeUri source, SafeUri destination, uint jpeg_quality = 95) { if (source.GetExtension () == destination.GetExtension ()) { // Simple copy will do! var file_from = GLib.FileFactory.NewForUri (source); var file_to = GLib.FileFactory.NewForUri (destination); file_from.Copy (file_to, GLib.FileCopyFlags.AllMetadata | GLib.FileCopyFlags.Overwrite, null, null); return; } // Else make a derived copy with metadata copied using (var img = ImageFile.Create (source)) { using (var pixbuf = img.Load ()) { CreateDerivedVersion (source, destination, jpeg_quality, pixbuf); } } }