public static ClipData.Item AddAsClipDataItem(IMimeItem mimeItem) { var uri = NextItemUri; UriItems[uri] = mimeItem; return(new ClipData.Item(uri)); }
public static string AsWindowsHtmlFragment(this IMimeItem mimeItem) { if (mimeItem.MimeType == "text/html" && mimeItem.Value is string html) { var start = html.Substring(0, Math.Min(html.Length, 300)); if (!start.ToLower().Contains("<html>")) { // we are going to assume we were given a fragment and need to encapsulate it for other Windows apps to recognize it (argh!) var fragment = html; var htmlStartIndex = 105; var fragStartIndex = htmlStartIndex + 36; var fragEndIndex = fragStartIndex + fragment.Length; var htmlEndIndex = fragEndIndex + 36; html = "Version:0.9"; html += "\r\nStartHTML:" + htmlStartIndex.ToString("D10"); html += "\r\nEndHTML:" + htmlEndIndex.ToString("D10"); html += "\r\nStartFragment:" + fragStartIndex.ToString("D10"); html += "\r\nEndFragment:" + fragEndIndex.ToString("D10"); html += "\r\n<html>\r\n<body>\r\n<!--StartFragment-->"; html += fragment; html += "<!--EndFragment-->\r\n</body>\r\n</html>"; } return(html); } return(null); }
public static StorageFile ToStorageFile(this IMimeItem mimeItem) { StorageFile result = null; if (MimeSharp.Current.Extension(mimeItem.MimeType) is List <string> extensions && extensions.Count > 0 && extensions[0] is string ext) { var value = mimeItem.Value; if (value is string text) { var fileName = Guid.NewGuid().ToString() + "." + ext; var path = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, fileName); File.WriteAllText(path, text); result = AsyncHelper.RunSync(() => StorageFile.GetFileFromPathAsync(path).AsTask()); } else if (value is byte[] byteArray) { var fileName = Guid.NewGuid().ToString() + "." + ext; var path = Path.Combine(ApplicationData.Current.TemporaryFolder.Path, fileName); File.WriteAllBytes(path, byteArray); result = AsyncHelper.RunSync(() => StorageFile.GetFileFromPathAsync(path).AsTask()); } else if (value is FileInfo fileInfo) { var path = fileInfo.FullName; result = AsyncHelper.RunSync(() => StorageFile.GetFileFromPathAsync(path).AsTask()); } } return(result); }
public static KeyValuePair <NSString, NSObject> ToUiPasteboardItem(this IMimeItem mimeItem) { if (mimeItem.MimeType?.ToNsUti() is NSString nsUti) // && mimeItem.Value.ToNSObject() is NSObject nSObject) { NSData nsData = null; if (mimeItem.Value is byte[] byteArray) { nsData = NSData.FromArray(byteArray); } else if (mimeItem.Value is FileInfo fileInfo) { nsData = NSData.FromFile(fileInfo.FullName); } else if (mimeItem.Value is Uri uri) { var nsUrl = new NSUrl(uri.AbsoluteUri); nsData = NSData.FromUrl(nsUrl); } if (mimeItem.MimeType.StartsWith("image/", StringComparison.InvariantCultureIgnoreCase) && nsData != null && nsData.Length > 0) { var uiImage = UIImage.LoadFromData(nsData); return(new KeyValuePair <NSString, NSObject>(nsUti, uiImage)); } NSObject nsObject = nsData ?? mimeItem.Value.ToNSObject(); if (nsObject != null) { return(new KeyValuePair <NSString, NSObject>(nsUti, nsObject)); } } return(new KeyValuePair <NSString, NSObject>()); }
public static Android.Net.Uri AsAsAndroidUri(IMimeItem mimeItem) { Android.Net.Uri uri = null; if (mimeItem.Value != null) { uri = NextItemUri; UriItems[uri] = mimeItem; } return(uri); }
public static NSDataItem Create(IMimeItem item) { var result = new NSDataItem(item); if (result.KeyedArchiver == null) { return(null); } return(result); }
private NSDataItem(IMimeItem item) { var nsObject = item.Value.ToNSObject(); if (nsObject != null) { NSUti = item.ToNsUti(); KeyedArchiver = NSKeyedArchiver.ArchivedDataWithRootObject(nsObject); } }
public static string AsString(this IMimeItem mimeItem) { var value = mimeItem.Value; if (value is FileInfo fileInfo && fileInfo.Exists && fileInfo.Length > 0) { return(File.ReadAllText(fileInfo.FullName)); } if (value is string text) { return(text); } return(null); }
public static NSString ToNsUti(this IMimeItem item) { return(item.MimeType.ToNsUti()); }