public string NewMediaObject(FileData mediaObject) { var mediaFile = new WorkflowMediaFile { FileName = mediaObject.name, Title = mediaObject.name, Culture = DataLocalizationFacade.DefaultLocalizationCulture.Name, Length = mediaObject.bits.Count(), MimeType = MimeTypeInfo.GetCanonical(mediaObject.type) }; if (mediaFile.MimeType == MimeTypeInfo.Default) { mediaFile.MimeType = MimeTypeInfo.GetCanonicalFromExtension(Path.GetExtension(mediaFile.FileName)); } using (Stream readStream = new MemoryStream(mediaObject.bits)) { using (Stream writeStream = mediaFile.GetNewWriteStream()) { readStream.CopyTo(writeStream); } } string folderPath = string.Format("/Blog/{0}/{1:yyyy-MM-dd}", Author.Name, DateTime.Now); mediaFile.FolderPath = ForceGetMediaFolderPath(folderPath); var addedFile = DataFacade.AddNew <IMediaFile>(mediaFile); return(MediaUrlHelper.GetUrl(addedFile)); }
private void CopyFileData(IMediaFile from, IMediaFileData to) { to.CultureInfo = from.Culture; to.Description = from.Description; to.FileName = from.FileName; to.FolderPath = from.FolderPath; to.Length = from.Length; to.MimeType = MimeTypeInfo.GetCanonical(from.MimeType); to.Title = from.Title; }
private IMediaFile ImportMedia(string src, string folder) { if (!ImportedImages.ContainsKey("src")) { ImportedImages[src] = null; ForceMediaFolder(folder); var client = new WebClient { Encoding = System.Text.Encoding.UTF8 }; client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); var stream = client.OpenRead(src); var filename = CleanFileName(Path.GetFileName(src)); var title = Path.GetFileNameWithoutExtension(src); var mediaFile = new WorkflowMediaFile { FileName = Path.GetFileName(filename), FolderPath = folder, Title = title, Description = string.Empty, Culture = Composite.C1Console.Users.UserSettings.ActiveLocaleCultureInfo.Name, MimeType = MimeTypeInfo.GetCanonicalFromExtension(Path.GetExtension(filename)) }; if (mediaFile.MimeType == MimeTypeInfo.Default) { mediaFile.MimeType = MimeTypeInfo.GetCanonical(client.ResponseHeaders["content-type"]); } using (Stream readStream = stream) { using (Stream writeStream = mediaFile.GetNewWriteStream()) { readStream.CopyTo(writeStream); } } int counter = 0; string extension = Path.GetExtension(mediaFile.FileName); string name = mediaFile.FileName.GetNameWithoutExtension(); while (Exists(mediaFile)) { counter++; mediaFile.FileName = name + counter.ToString() + extension; } IMediaFile addedFile = DataFacade.AddNew <IMediaFile>(mediaFile); ImportedImages[src] = addedFile; } return(ImportedImages[src]); }
private string GetTextToIndex(IMediaFile mediaFile) { var mimeType = MimeTypeInfo.GetCanonical(mediaFile.MimeType); if (!IsIndexableMimeType(mimeType)) { return(null); } // Checking if the parsing results are preserved in the cache var outputFilePath = GetTextOutputFilePath(mediaFile); var lastWriteTime = mediaFile.LastWriteTime ?? mediaFile.CreationTime; if (File.Exists(outputFilePath) && lastWriteTime != null && File.GetCreationTime(outputFilePath) == lastWriteTime) { return(File.ReadAllText(outputFilePath)); } string extension; try { var fileName = mediaFile.FileName; foreach (var ch in Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(ch, '_'); } extension = Path.GetExtension(fileName); } catch (ArgumentException) { Log.LogWarning(LogTitle, $"Failed to extract extension from file: '{mediaFile.FileName}', mime type: '{mediaFile.MimeType}' "); return(null); } if (string.IsNullOrEmpty(extension)) { extension = MimeTypeInfo.GetExtensionFromMimeType(mediaFile.MimeType); } if (string.IsNullOrEmpty(extension)) { Log.LogWarning(LogTitle, $"Failed to extract extension from file: '{mediaFile.FileName}', mime type: '{mediaFile.MimeType}' "); return(null); } // Saving the file to a temp directory var tempSourceFile = GetTempFileName(extension); try { using (var mediaStream = mediaFile.GetReadStream()) using (var file = File.Create(tempSourceFile)) { mediaStream.CopyTo(file); } } catch (FileNotFoundException) { if (Interlocked.Increment(ref _missingFilesLogged) <= MaxMissingFilesLogMessages) { Log.LogWarning(LogTitle, $"Missing an underlying content file for the media file '{mediaFile.KeyPath}'"); } return(null); } var exePath = PathUtil.Resolve(IFilterExecutableRelativePath); var workingDirectory = Path.GetDirectoryName(exePath); string stdout, stderr; int exitCode; using (var process = new Process { StartInfo = { WorkingDirectory = workingDirectory, FileName = "\"" + exePath + "\"", Arguments = $"\"{tempSourceFile}\" \"{outputFilePath}\"", RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true, StandardOutputEncoding = Encoding.UTF8, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden } }) { process.Start(); stdout = process.StandardOutput.ReadToEnd(); stderr = process.StandardError.ReadToEnd(); process.WaitForExit(); exitCode = process.ExitCode; } C1File.Delete(tempSourceFile); if (exitCode != 0) { var msg = $"Failed to parse the content of the media file '{Path.GetFileName(mediaFile.FileName)}'."; if ((uint)exitCode == 0x80004005 /*E_FAIL*/) { Log.LogVerbose(LogTitle, msg + " Unspecified error."); return(null); } if ((uint)exitCode == 0x80004002 /*E_NOINTERFACE*/) { Log.LogWarning(LogTitle, msg + " IFilter not found for the given file extension."); return(null); } Log.LogWarning(LogTitle, msg + $"\r\nExit Code: {exitCode}\r\nOutput: {stdout}" + (!string.IsNullOrEmpty(stderr) ? $"\r\nError: {stderr}" : "")); return(null); } if (!File.Exists(outputFilePath)) { return(null); } if (lastWriteTime != null) { File.SetLastWriteTime(outputFilePath, lastWriteTime.Value); } return(File.ReadAllText(outputFilePath)); }
private string GetTextToIndex(IMediaFile mediaFile) { var mimeType = MimeTypeInfo.GetCanonical(mediaFile.MimeType); if (!IsIndexableMimeType(mimeType)) { return(null); } // Checking if the parsing results are preserved in the cache var outputFilePath = GetTextOutputFilePath(mediaFile); var lastWriteTime = mediaFile.LastWriteTime ?? mediaFile.CreationTime; if (File.Exists(outputFilePath) && lastWriteTime != null && File.GetCreationTime(outputFilePath) == lastWriteTime) { string text = File.ReadAllText(outputFilePath); if (!string.IsNullOrEmpty(text)) { return(text); } } string extension = GetExtension(mediaFile); if (string.IsNullOrEmpty(extension)) { Log.LogWarning(LogTitle, $"Failed to extract extension from file: '{mediaFile.FileName}', mime type: '{mediaFile.MimeType}' "); return(null); } // Saving the file to a temp directory var tempSourceFile = GetTempFileName(extension); try { using (var mediaStream = mediaFile.GetReadStream()) using (var file = File.Create(tempSourceFile)) { mediaStream.CopyTo(file); } } catch (FileNotFoundException) { if (Interlocked.Increment(ref _missingFilesLogged) <= MaxMissingFilesLogMessages) { Log.LogWarning(LogTitle, $"Missing an underlying content file for the media file '{mediaFile.KeyPath}'"); } return(null); } bool success = ExtractText(tempSourceFile, outputFilePath, mediaFile); C1File.Delete(tempSourceFile); if (!success) { return(null); } if (lastWriteTime != null) { File.SetLastWriteTime(outputFilePath, lastWriteTime.Value); } var result = File.ReadAllText(outputFilePath); if (result.Length == 0) { Log.LogWarning(LogTitle, $"Failed to extract text from media file '{mediaFile.FileName}', mime type: '{mediaFile.MimeType}', extension '{extension}'"); } return(result); }
internal static ResourceHandle WebsiteFileIcon(string mimeType) { return(MimeTypeInfo.GetResourceHandleFromMimeType(MimeTypeInfo.GetCanonical(mimeType))); }