/// <summary> /// Populates the auto-fill properties of a content item, for a specified auto-fill configuration. /// </summary> /// <param name="content">The content item.</param> /// <param name="autoFillConfig">The auto-fill configuration.</param> /// <param name="filepath">The filesystem path to the uploaded file.</param> /// <remarks>The <paramref name="filepath"/> parameter is the path relative to the filesystem.</remarks> /// <param name="culture">Variation language.</param> /// <param name="segment">Variation segment.</param> public void Populate(IContentBase content, IImagingAutoFillUploadField autoFillConfig, string filepath, string culture, string segment) { if (content == null) { throw new ArgumentNullException(nameof(content)); } if (autoFillConfig == null) { throw new ArgumentNullException(nameof(autoFillConfig)); } // no file = reset, file = auto-fill if (filepath.IsNullOrWhiteSpace()) { ResetProperties(content, autoFillConfig, culture, segment); } else { // if anything goes wrong, just reset the properties try { using (var filestream = _mediaFileSystem.OpenFile(filepath)) { var extension = (Path.GetExtension(filepath) ?? "").TrimStart('.'); var size = _contentSection.IsImageFile(extension) ? (Size?)ImageHelper.GetDimensions(filestream) : null; SetProperties(content, autoFillConfig, size, filestream.Length, extension, culture, segment); } } catch (Exception ex) { _logger.Error(typeof(UploadAutoFillProperties), ex, "Could not populate upload auto-fill properties for file '{File}'.", filepath); ResetProperties(content, autoFillConfig, culture, segment); } } }
/// <summary> /// Extract text from a PDF file at the given path /// </summary> /// <param name="filePath"></param> /// <returns></returns> public string ExtractText(string filePath) { using (var fs = _mediaFileSystem.OpenFile(filePath)) { if (fs != null) { return(ExceptChars(_pdfTextExtractor.GetTextFromPdf(fs), UnsupportedRange.Value, ReplaceWithSpace)); } else { _logger.Error(this.GetType(), new Exception($"Unable to open PDF file {filePath}")); return(null); } } }
public virtual async Task <string> GenerateMedia(int id, int staticSiteId, IEnumerable <Crop> crops = null) { var mediaItem = GetMedia(id); if (mediaItem == null) { return(null); } var url = mediaItem.Url(); string absoluteUrl = mediaItem.Url(mode: UrlMode.Absolute); var partialPath = GetRelativeMediaPath(mediaItem); if (string.IsNullOrEmpty(partialPath)) { return(null); } var mediaFileStream = _mediaFileSystem.OpenFile(partialPath); var generatedFileLocation = await Save(staticSiteId, mediaFileStream, partialPath); if (crops?.Any() == true) { var fileName = Path.GetFileName(partialPath); var fileExtension = Path.GetExtension(partialPath)?.ToLower(); var pathSegment = partialPath.Replace(fileName, string.Empty); if (ResizeExtensions.Contains(fileExtension.Trim('.'))) { foreach (var crop in crops) { var query = $"?mode=max&width={crop.Width ?? 0}&height={crop.Height ?? 0}"; var cropUrl = absoluteUrl + query; var newName = _imageCropNameGenerator.GetCropFileName(Path.GetFileNameWithoutExtension(partialPath), crop); var newPath = Path.Combine(pathSegment, newName + fileExtension); var destinationPath = _storer.GetFileDestinationPath(staticSiteId.ToString(), newPath); await SaveFileDataFromWebClient(cropUrl, destinationPath); } } } return(generatedFileLocation); }
public string ExtractText(string filePath) { using (var fs = _mediaFileSystem.OpenFile(filePath)) return(ExceptChars(_pdfTextExtractor.GetTextFromPdf(fs), UnsupportedRange.Value, ReplaceWithSpace)); }
public OptimiseResponse Save(SaveModel model) { var media = _mediaService.GetById(model.Id); if (media == null) { return new OptimiseResponse("Media Not Found") { ResultType = Enums.ResultType.Error } } ; string path = media.GetUrl(StaticValues.Properties.UmbracoFile, Logger); string fullPath = _mediaFileSystem.GetFullPath(path); string extension = _mediaFileSystem.GetExtension(path)?.Substring(1); bool updated = false; byte[] data = null; using (Stream inStream = _mediaFileSystem.OpenFile(fullPath)) { if (Reduce(extension)) { data = _imageService.ReduceQuality(inStream); updated = true; } if (Shrink(extension)) { data = _shrinkService.ShrinkImage(inStream); updated = true; } } if (data == null || updated) { using (var outStream = new MemoryStream(data)) { outStream.Position = 0; try { media.SetValue(_contentTypeBaseServiceProvider, StaticValues.Properties.UmbracoFile, Path.GetFileName(path), outStream); _mediaService.Save(media); } catch (Exception ex) { Logger.Error(typeof(OptimiseController), "Save", ex); throw; } } return(new OptimiseResponse("Media optimised without any errors") { ResultType = Enums.ResultType.Success }); } else { return new OptimiseResponse("Media optimising was unsuccessful") { ResultType = Enums.ResultType.Error } }; }