mirror of
https://github.com/revenz/FileFlowsPlugins.git
synced 2025-12-30 19:20:56 -06:00
FF-1190 - updated image nodes to work with file server
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
global using System;
|
||||
global using System.Text;
|
||||
global using System.ComponentModel.DataAnnotations;
|
||||
global using System.Collections.Generic;
|
||||
global using FileFlows.Plugin;
|
||||
global using FileFlows.Plugin.Attributes;
|
||||
global using SixLabors.ImageSharp;
|
||||
global using FileHelper = FileFlows.Plugin.Helpers.FileHelper;
|
||||
global using static FileFlows.ImageNodes.Images.Constants;
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net7.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<RootNamespace>FileFlows.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
|
||||
<FileVersion>1.0.4.189</FileVersion>
|
||||
|
||||
@@ -61,7 +61,7 @@ public class AutoCropImage : ImageNode
|
||||
{
|
||||
c.Grayscale().Resize(originalWidth / scaleFactor, originalHeight / scaleFactor);
|
||||
});
|
||||
string temp = Path.Combine(args.TempPath, Guid.NewGuid() + ".jpg");
|
||||
string temp = FileHelper.Combine(args.TempPath, Guid.NewGuid() + ".jpg");
|
||||
image.SaveAsJpeg(temp);
|
||||
var bounds = GetTrimBounds(temp);
|
||||
bounds.X *= scaleFactor;
|
||||
|
||||
@@ -13,16 +13,22 @@ public abstract class ImageBaseNode:Node
|
||||
|
||||
public override bool PreExecute(NodeParameters args)
|
||||
{
|
||||
var localFile = args.FileService.GetLocalPath(args.WorkingFile);
|
||||
if (localFile.IsFailed)
|
||||
{
|
||||
args.Logger?.ELog("Working file cannot be read: " + localFile.Error);
|
||||
return false;
|
||||
}
|
||||
if (args.WorkingFile.ToLowerInvariant().EndsWith(".heic"))
|
||||
{
|
||||
using var image = new MagickImage(args.WorkingFile);
|
||||
using var image = new MagickImage(localFile);
|
||||
CurrentHeight = image.Height;
|
||||
CurrentWidth = image.Width;
|
||||
CurrentFormat = "HEIC";
|
||||
}
|
||||
else
|
||||
{
|
||||
using var image = Image.Load(args.WorkingFile, out IImageFormat format);
|
||||
using var image = Image.Load(localFile, out IImageFormat format);
|
||||
CurrentHeight = image.Height;
|
||||
CurrentWidth = image.Width;
|
||||
CurrentFormat = format.Name;
|
||||
@@ -38,7 +44,7 @@ public abstract class ImageBaseNode:Node
|
||||
|
||||
protected void UpdateImageInfo(NodeParameters args, Dictionary<string, object> variables = null)
|
||||
{
|
||||
string extension = new FileInfo(args.WorkingFile).Extension[1..].ToLowerInvariant();
|
||||
string extension = FileHelper.GetExtension(args.WorkingFile).ToLowerInvariant();
|
||||
if (extension == "heic")
|
||||
{
|
||||
using var image = new MagickImage(args.WorkingFile);
|
||||
@@ -60,10 +66,7 @@ public abstract class ImageBaseNode:Node
|
||||
};
|
||||
|
||||
variables ??= new Dictionary<string, object>();
|
||||
if (args.Parameters.ContainsKey(IMAGE_INFO))
|
||||
args.Parameters[IMAGE_INFO] = imageInfo;
|
||||
else
|
||||
args.Parameters.Add(IMAGE_INFO, imageInfo);
|
||||
args.Parameters[IMAGE_INFO] = imageInfo;
|
||||
|
||||
variables.AddOrUpdate("img.Width", imageInfo.Width);
|
||||
variables.AddOrUpdate("img.Height", imageInfo.Height);
|
||||
@@ -77,7 +80,6 @@ public abstract class ImageBaseNode:Node
|
||||
metadata.Add("Height", imageInfo.Height);
|
||||
args.SetMetadata(metadata);
|
||||
|
||||
|
||||
args.UpdateVariables(variables);
|
||||
}
|
||||
internal ImageInfo? GetImageInfo(NodeParameters args)
|
||||
@@ -103,14 +105,14 @@ public abstract class ImageBaseNode:Node
|
||||
/// <returns>the filename fo the image to use</returns>
|
||||
protected string ConvertImageIfNeeded(NodeParameters args)
|
||||
{
|
||||
string extension = new FileInfo(args.WorkingFile).Extension[1..].ToLowerInvariant();
|
||||
string extension = FileHelper.GetExtension(args.WorkingFile).ToLowerInvariant();
|
||||
if (extension == "heic")
|
||||
{
|
||||
// special case have to use imagemagick
|
||||
|
||||
using var image = new MagickImage(args.WorkingFile);
|
||||
image.Format = MagickFormat.Png;
|
||||
var newFile = Path.Combine(args.TempPath, Guid.NewGuid().ToString() + ".png");
|
||||
var newFile = FileHelper.Combine(args.TempPath, Guid.NewGuid() + ".png");
|
||||
image.Write(newFile);
|
||||
return newFile;
|
||||
}
|
||||
|
||||
@@ -28,12 +28,10 @@ public class ImageFile : ImageBaseNode
|
||||
{
|
||||
try
|
||||
{
|
||||
var fileInfo = new FileInfo(args.WorkingFile);
|
||||
if (fileInfo.Exists)
|
||||
{
|
||||
args.Variables["ORIGINAL_CREATE_UTC"] = fileInfo.CreationTimeUtc;
|
||||
args.Variables["ORIGINAL_LAST_WRITE_UTC"] = fileInfo.LastWriteTimeUtc;
|
||||
}
|
||||
if (args.FileService.FileCreationTimeUtc(args.WorkingFile).Success(out DateTime createTime))
|
||||
args.Variables["ORIGINAL_CREATE_UTC"] = createTime;
|
||||
if (args.FileService.FileLastWriteTimeUtc(args.WorkingFile).Success(out DateTime writeTime))
|
||||
args.Variables["ORIGINAL_LAST_WRITE_UTC"] = writeTime;
|
||||
|
||||
UpdateImageInfo(args, this.Variables);
|
||||
if(string.IsNullOrEmpty(base.CurrentFormat) == false)
|
||||
|
||||
@@ -22,7 +22,7 @@ public class ImageFormat: ImageNode
|
||||
return 2;
|
||||
}
|
||||
|
||||
string extension = new FileInfo(args.WorkingFile).Extension[1..].ToLowerInvariant();
|
||||
string extension = FileHelper.GetExtension(args.WorkingFile).ToLowerInvariant();
|
||||
if (extension == "heic")
|
||||
{
|
||||
// special case have to use imagemagick
|
||||
|
||||
@@ -27,15 +27,15 @@ public abstract class ImageNode : ImageBaseNode
|
||||
{
|
||||
_FormatOptions = new List<ListOption>
|
||||
{
|
||||
new ListOption { Value = "", Label = "Same as source"},
|
||||
new ListOption { Value = IMAGE_FORMAT_BMP, Label = "Bitmap"},
|
||||
new ListOption { Value = IMAGE_FORMAT_GIF, Label = "GIF"},
|
||||
new ListOption { Value = IMAGE_FORMAT_JPEG, Label = "JPEG"},
|
||||
new ListOption { Value = IMAGE_FORMAT_PBM, Label = "PBM"},
|
||||
new ListOption { Value = IMAGE_FORMAT_PNG, Label = "PNG"},
|
||||
new ListOption { Value = IMAGE_FORMAT_TIFF, Label = "TIFF"},
|
||||
new ListOption { Value = IMAGE_FORMAT_TGA, Label = "TGA" },
|
||||
new ListOption { Value = IMAGE_FORMAT_WEBP, Label = "WebP"},
|
||||
new () { Value = "", Label = "Same as source"},
|
||||
new () { Value = IMAGE_FORMAT_BMP, Label = "Bitmap"},
|
||||
new () { Value = IMAGE_FORMAT_GIF, Label = "GIF"},
|
||||
new () { Value = IMAGE_FORMAT_JPEG, Label = "JPEG"},
|
||||
new () { Value = IMAGE_FORMAT_PBM, Label = "PBM"},
|
||||
new () { Value = IMAGE_FORMAT_PNG, Label = "PNG"},
|
||||
new () { Value = IMAGE_FORMAT_TIFF, Label = "TIFF"},
|
||||
new () { Value = IMAGE_FORMAT_TGA, Label = "TGA" },
|
||||
new () { Value = IMAGE_FORMAT_WEBP, Label = "WebP"},
|
||||
};
|
||||
}
|
||||
return _FormatOptions;
|
||||
@@ -46,7 +46,7 @@ public abstract class ImageNode : ImageBaseNode
|
||||
{
|
||||
IImageFormat? format = null;
|
||||
|
||||
var newFile = Path.Combine(args.TempPath, Guid.NewGuid().ToString());
|
||||
var newFile = FileHelper.Combine(args.TempPath, Guid.NewGuid().ToString());
|
||||
switch (this.Format)
|
||||
{
|
||||
case IMAGE_FORMAT_BMP:
|
||||
@@ -97,8 +97,19 @@ public abstract class ImageNode : ImageBaseNode
|
||||
|
||||
protected void SaveImage(NodeParameters args, Image img, string file, IImageFormat format, bool updateWorkingFile = true)
|
||||
{
|
||||
using Stream outStream = new FileStream(file, FileMode.Create);
|
||||
string local = args.FileService.FileIsLocal(file)
|
||||
? file
|
||||
: FileHelper.Combine(args.TempPath, Guid.NewGuid() + "." + FileHelper.GetExtension(file));
|
||||
|
||||
using var outStream = new System.IO.FileStream(local, System.IO.FileMode.Create);
|
||||
img.Save(outStream, format);
|
||||
|
||||
if (local != file && args.FileService.FileMove(local, file).Failed(out string error))
|
||||
{
|
||||
args.Logger?.ELog("Failed to move saved file: " + error);
|
||||
return;
|
||||
}
|
||||
|
||||
if (updateWorkingFile)
|
||||
{
|
||||
args.SetWorkingFile(file);
|
||||
@@ -108,9 +119,14 @@ public abstract class ImageNode : ImageBaseNode
|
||||
|
||||
protected void SaveImage(NodeParameters args, ImageMagick.MagickImage img, string file, bool updateWorkingFile = true)
|
||||
{
|
||||
using Stream outStream = new FileStream(file, FileMode.Create);
|
||||
string origExtension = new FileInfo(args.WorkingFile).Extension[1..].ToLowerInvariant();
|
||||
string newExtension = new FileInfo(file).Extension[1..].ToLowerInvariant();
|
||||
string local = args.FileService.FileIsLocal(file)
|
||||
? file
|
||||
: FileHelper.Combine(args.TempPath, Guid.NewGuid() + "." + FileHelper.GetExtension(file));
|
||||
|
||||
using var outStream = new System.IO.FileStream(local, System.IO.FileMode.Create);
|
||||
|
||||
string origExtension = FileHelper.GetExtension(args.WorkingFile).ToLowerInvariant();
|
||||
string newExtension = FileHelper.GetExtension(file).ToLowerInvariant();
|
||||
if (origExtension != newExtension)
|
||||
{
|
||||
switch (newExtension)
|
||||
@@ -145,11 +161,18 @@ public abstract class ImageNode : ImageBaseNode
|
||||
}
|
||||
|
||||
img.Write(outStream);
|
||||
|
||||
|
||||
if (updateWorkingFile)
|
||||
{
|
||||
args.SetWorkingFile(file);
|
||||
using var image = Image.Load(args.WorkingFile, out IImageFormat format);
|
||||
using var image = Image.Load(local, out IImageFormat format);
|
||||
UpdateImageInfo(args, img.Width, img.Height, format.Name, Variables);
|
||||
}
|
||||
|
||||
if (local != file && args.FileService.FileMove(local, file).Failed(out string error))
|
||||
{
|
||||
args.Logger?.ELog("Failed to move saved file: " + error);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,7 +199,7 @@ public class ImageNodesTests
|
||||
[TestMethod]
|
||||
public void ImageNodes_Basic_AutoCrop_01()
|
||||
{
|
||||
Assert.IsTrue(File.Exists(TestCropImage1));
|
||||
Assert.IsTrue(System.IO.File.Exists(TestCropImage1));
|
||||
var logger = new TestLogger();
|
||||
var args = new NodeParameters(TestCropImage1, logger, false, string.Empty)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user