private void CreateZip() { // Delete the old zip file if (File.Exists(ZipFilePath)) { m_Logger.Debug("Deleted old asset zip file"); File.Delete(ZipFilePath); } // Get the asset file info AssetFileInfo fileInfo = new AssetFileInfo(m_Asset); // Ensure asset file exists before continuing if (!fileInfo.FileExists) { return; } using (ZipOutputStream zos = new ZipOutputStream(File.Create(ZipFilePath))) { zos.SetLevel(9); ZipEntry ze = new ZipEntry(m_Asset.Filename); zos.PutNextEntry(ze); using (FileStream fs = File.OpenRead(fileInfo.FilePath)) { int sourceBytes; byte[] buffer = new byte[10000]; do { sourceBytes = fs.Read(buffer, 0, buffer.Length); zos.Write(buffer, 0, sourceBytes); }while (sourceBytes > 0); } m_Logger.DebugFormat("Added asset file: {0} to zip: {1}", fileInfo.FilePath, ZipFilePath); foreach (AssetFile file in m_Asset.GetAttachedFiles()) { ZipEntry fze = new ZipEntry(file.Filename); zos.PutNextEntry(fze); zos.Write(file.FileContent, 0, file.FileContent.Length); m_Logger.DebugFormat("Added attached file: {0} to zip: {1}", file.Filename, ZipFilePath); } } }
public bool ProcessFile(Asset asset, bool notify, FileOutputs output) { AssetFileInfo info = new AssetFileInfo(asset); string watermark = GetWatermarkImagePath(asset.BrandId); Job job = new Job { AssetId = asset.AssetId.GetValueOrDefault(), InputPath = info.FilePath, WatermarkPath = (asset.WatermarkPreview && !String.IsNullOrEmpty(watermark)) ? watermark : string.Empty, CreatePreview = output.IsSet(FileOutputs.Preview), CreateThumbnail = output.IsSet(FileOutputs.Thumbnail), CallbackUrl = "~/ProcessingCallback.ashx", AdditionalData = string.Format("<AdditionalData><Notify>{0}</Notify></AdditionalData>", notify) }; long jobId = SubmitJob(job); return(jobId > 0); }
public static void Generate(Asset asset, AssetBitmapGroup abg) { if (abg == null) { throw new NullReferenceException("AssetBitmapGroup cannot be null"); } if (!APSGateway.Instance.CanProcess(asset.FileExtension)) { m_Logger.WarnFormat("APS cannot process extension '{0}'. Asset Bitmap Group will not be generated", asset.FileExtension); return; } AssetFileInfo afi = new AssetFileInfo(asset); if (!afi.FileExists) { return; } Job job = new Job { AssetId = asset.AssetId.GetValueOrDefault(), InputPath = afi.FilePath, CreatePreview = false, CreateThumbnail = true, OverrideWidth = abg.MaxSize.Width, OverrideHeight = abg.MaxSize.Height, CallbackUrl = "~/BitmapProcessingCallback.ashx", AdditionalData = string.Format("<AdditionalData><AssetBitmapReference>{0}</AssetBitmapReference><Notify>False</Notify></AdditionalData>", abg.Reference) }; long jobId = APSGateway.Instance.SubmitJob(job); m_Logger.DebugFormat("Asset Bitmap Generation submitted to APS with Job Id: {0}. BitmapGroup reference: {1}, MaxWidth: {2}, MaxHeight: {3}", jobId, abg.Reference, abg.MaxSize.Width, abg.MaxSize.Height); }
public static string GetResizedAssetImage(Asset asset, int assetImageSizeId, DownloadFormat downloadFormat, bool create) { //--------------------------------------------------------------------------------------------------------- // Get asset file info //--------------------------------------------------------------------------------------------------------- AssetFileInfo fileInfo = new AssetFileInfo(asset); //--------------------------------------------------------------------------------------------------------- // Ensure asset file exists //--------------------------------------------------------------------------------------------------------- if (!fileInfo.FileExists) { return(string.Empty); } //--------------------------------------------------------------------------------------------------------- // No options specified //--------------------------------------------------------------------------------------------------------- if (assetImageSizeId == 0 && downloadFormat == DownloadFormat.Original) { return(fileInfo.FilePath); } //--------------------------------------------------------------------------------------------------------- // Only images can be resized //--------------------------------------------------------------------------------------------------------- if (!AssetTypeChecker.IsImage(asset.FileExtension)) { return(fileInfo.FilePath); } //--------------------------------------------------------------------------------------------------------- // PDFs are an exception as they are classified as images but cant be resized //--------------------------------------------------------------------------------------------------------- if (StringUtils.GetFileExtension(asset.Filename) == "pdf") { return(fileInfo.FilePath); } //--------------------------------------------------------------------------------------------------------- // Get the requested image size //--------------------------------------------------------------------------------------------------------- AssetImageSize ais = AssetImageSizeCache.Instance.GetById(assetImageSizeId); //--------------------------------------------------------------------------------------------------------- // Construct the path to the output filename //--------------------------------------------------------------------------------------------------------- string suffix = (StringUtils.IsBlank(ais.FileSuffix)) ? "original" : ais.FileSuffix; string reference = Path.GetFileNameWithoutExtension(asset.Filename) + "_" + suffix; string extension = GetDownloadFormat(downloadFormat, asset.FileExtension); string cacheFolder = VirtualPathUtility.AppendTrailingSlash(Path.Combine(Settings.CachedAssetFilesFolder, asset.AssetId.ToString())); string outputFilename = Path.Combine(cacheFolder, reference) + "." + extension; if (File.Exists(outputFilename)) { return(outputFilename); } if (!create) { return(string.Empty); } //--------------------------------------------------------------------------------------------------------- // Create the cache folder if it doesn't exist //--------------------------------------------------------------------------------------------------------- if (!Directory.Exists(cacheFolder)) { Directory.CreateDirectory(cacheFolder); m_Logger.DebugFormat("Created cache folder: {0}", cacheFolder); } //------------------------------------------------------------------------------------------------ // Now we've verified that we need to do some work. //------------------------------------------------------------------------------------------------ string tempPath = Path.Combine(Settings.TempFolder, asset.AssetId + "_" + Guid.NewGuid() + Path.GetExtension(fileInfo.FilePath)); //--------------------------------------------------------------------------------------------------------- // Create resized image //--------------------------------------------------------------------------------------------------------- if (ais.Width > 0 && ais.Height > 0) { ImageProcessingJob job = new ImageProcessingJob { SourceFilePath = fileInfo.FilePath, TargetFilePath = tempPath, Width = ais.Width, Height = ais.Height }; job.Go(); m_Logger.DebugFormat("Generated resized image for asset id: {0}, assetImageSize: {1}, height: {2}, width: {3} at: {4}", asset.AssetId, ais.Description, ais.Height, ais.Width, job.TargetFilePath); } //--------------------------------------------------------------------------------------------------------- // Ensure we've got our image //--------------------------------------------------------------------------------------------------------- if (!File.Exists(tempPath)) { File.Copy(fileInfo.FilePath, tempPath); m_Logger.DebugFormat("Original asset copied to: {0}", tempPath); } //--------------------------------------------------------------------------------------------------------- // Now convert the DPI to the value requested //--------------------------------------------------------------------------------------------------------- if (ais.DotsPerInch > 0) { ImageUtils.ChangeDPI(tempPath, ais.DotsPerInch); m_Logger.DebugFormat("Changed DPI of file: {0} to: {1}", tempPath, ais.DotsPerInch); } //------------------------------------------------------------------------------------------------ // Do download format conversion //------------------------------------------------------------------------------------------------ if (StringUtils.GetFileExtension(tempPath) != StringUtils.GetFileExtension(outputFilename)) { ImageUtils.ConvertFormat(tempPath, outputFilename); } //------------------------------------------------------------------------------------------------ // Ensure we've got the output file now //------------------------------------------------------------------------------------------------ if (!File.Exists(outputFilename)) { File.Move(tempPath, outputFilename); } //--------------------------------------------------------------------------------------------------------- // All done, return path //--------------------------------------------------------------------------------------------------------- return(outputFilename); }