public static bool IsImage(this NSUrl url) { if (url.TryGetResource(NSUrl.TypeIdentifierKey, out var typeObj, out var error)) { var typeIdentifier = (NSString)typeObj; return(CGImageSource.TypeIdentifiers.Any(imageType => MobileCoreServices.UTType.ConformsTo(typeIdentifier, imageType))); }
public void IsExcludedFromBackupKey() { //TestRuntime.AssertSystemVersion (PlatformName.MacOSX, 10, 8, throwIfOtherPlatform: false); TestRuntime.AssertSystemVersion(PlatformName.MacOSX, 10, 9, throwIfOtherPlatform: false); // 10.8 fails DoNotBackupMe-1 TestRuntime.AssertSystemVersion(PlatformName.MacOSX, 10, 10, throwIfOtherPlatform: false); // 10.10 fails DoNotBackupMe-1 // NOTE: this test was failing with either NullReferenceException or InvalidCastException // when we used CFBoolean as a NSObject (i.e. CFBoolean.TrueObject). The test order execution // was important to track this down NSObject value; Assert.True(NSBundle.MainBundle.ExecutableUrl.TryGetResource(NSUrl.IsExcludedFromBackupKey, out value), "MainBundle"); Assert.That(value, Is.TypeOf(typeof(NSNumber)), "NSNumber"); Assert.That((int)(value as NSNumber), Is.EqualTo(0), "0"); string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), $"DoNotBackupMe-NSUrl-{Process.GetCurrentProcess ().Id}"); try { File.WriteAllText(filename, "not worth a bit"); using (NSUrl url = NSUrl.FromFilename(filename)) { Assert.True(url.TryGetResource(NSUrl.IsExcludedFromBackupKey, out value)); Assert.That((int)(value as NSNumber), Is.EqualTo(0), "DoNotBackupMe-0"); url.SetResource(NSUrl.IsExcludedFromBackupKey, (NSNumber)1); Assert.True(url.TryGetResource(NSUrl.IsExcludedFromBackupKey, out value)); Assert.That((int)(value as NSNumber), Is.EqualTo(1), "DoNotBackupMe-1"); NSError error; NSDictionary dict = url.GetResourceValues(new NSString [] { NSUrl.IsExcludedFromBackupKey }, out error); Assert.Null(error, "error"); Assert.That(dict.Keys [0], Is.EqualTo(NSUrl.IsExcludedFromBackupKey), "Key"); Assert.That((int)(dict.Values [0] as NSNumber), Is.EqualTo(1), "Value"); } } finally { // otherwise the attribute won't reset even if the file is overwritten File.Delete(filename); } }
public static Tuple <string, string, string> GetAvUtiConformance(this NSUrl filePath) { NSError error; NSObject typeValue; if (filePath.TryGetResource(NSUrl.TypeIdentifierKey, out typeValue, out error)) { var typeValueString = typeValue as NSString; if (typeValueString != null) { if (UTType.ConformsTo(typeValueString, UTType.Movie)) { if (UTType.ConformsTo(typeValueString, UTType.MPEG4)) { return(new Tuple <string, string, string> (UTType.Movie, UTType.MPEG4, filePath.LastPathComponent)); } if (UTType.ConformsTo(typeValueString, UTType.QuickTimeMovie)) { return(new Tuple <string, string, string> (UTType.Movie, UTType.QuickTimeMovie, filePath.LastPathComponent)); } // alert that file is recognized as a movie, but we don't handle it (yet) log it return(new Tuple <string, string, string> (UTType.Movie, UTType.Movie, filePath.LastPathComponent)); } if (UTType.ConformsTo(typeValueString, UTType.Audio)) { if (UTType.ConformsTo(typeValueString, UTType.MP3)) { return(new Tuple <string, string, string> (UTType.Audio, UTType.MP3, filePath.LastPathComponent)); } if (UTType.ConformsTo(typeValueString, UTType.WaveformAudio)) { return(new Tuple <string, string, string> (UTType.Audio, UTType.WaveformAudio, filePath.LastPathComponent)); } // alert that file is recognized as a audio, but we don't handle it (yet) log it return(new Tuple <string, string, string> (UTType.Audio, UTType.Audio, filePath.LastPathComponent)); } } } else if (error != null) { //Log.Debug ($"Error trying to get resource identifier\n\t{error.Code}\n\t{error.Domain}\n\t{error.Description}"); return(new Tuple <string, string, string> (UTType.PlainText, $"Error trying to get resource identifier\n\t{error.Code}\n\t{error.Domain}\n\t{error.Description}", filePath.LastPathComponent)); } return(new Tuple <string, string, string> (UTType.PlainText, $"Unsupported file type or path", filePath.LastPathComponent)); }
public static bool IsDir(this NSUrl url) { NSError error; NSObject isDirNum; if (url.TryGetResource(NSUrl.IsDirectoryKey, out isDirNum, out error)) { bool isDir = ((NSNumber)isDirNum).BoolValue; return(isDir); } throw new NSErrorException(error); }