libwebp.net
libwebp.net copied to clipboard
Access to the path 'C:\Windows\TEMP\cwebp.exe' is denied.
I got this error when deploy code to hosting service (it works well on Azure)
In some cases, path0 has value "'C:\Windows\TEMP" and the user account don't have permission to read/write to this folder
// Get user temp directory
var path0 = Path.GetTempPath();
Is there anyway to config the temp folder?
I think the library may not handle concurrency correctly. I also get this error in a parallel foreach.
// See https://aka.ms/new-console-template for more information
using Libwebp.Net;
using Libwebp.Standard;
if (args.Length <= 0 || string.IsNullOrWhiteSpace(args[0]))
{
Console.WriteLine("Please provide a file path.");
return;
}
var path = args[0];
ICollection<string> files = new string[] {};
string rootPath = Environment.CurrentDirectory;
if(Directory.Exists(path))
{
files = Directory.GetFiles(path, "*.png");
if(!path.EndsWith('\\'))
{
path += '\\';
}
var directoryName = Path.GetDirectoryName(path);
if(string.IsNullOrWhiteSpace(directoryName))
{
Console.WriteLine("Unable to determine directory name");
return;
}
rootPath = Path.Combine(rootPath, $"{directoryName} - WebP");
if(!Directory.Exists(rootPath))
{
Directory.CreateDirectory(rootPath);
}
}
else if(File.Exists(path) && Path.GetExtension(path) == ".png")
{
files = new[] { path };
}
await Parallel.ForEachAsync(files, async (currentFilePath, token) =>
{
using var currentFile = new FileStream(currentFilePath, FileMode.Open);
using var ms = new MemoryStream((int)currentFile.Length);
await currentFile.CopyToAsync(ms);
var fileName = $"{Path.GetFileNameWithoutExtension(currentFilePath)}.webp";
var outputFileName = Path.Combine(rootPath, fileName);
var encoderConfig = new WebpConfigurationBuilder()
.Lossless()
.Output(fileName)
.Build();
var encoder = new WebpEncoder(encoderConfig);
var tempFile = await encoder.EncodeAsync(ms, fileName);
using var newFile = new FileStream(outputFileName, FileMode.OpenOrCreate);
await tempFile.CopyToAsync(newFile);
await newFile.FlushAsync();
newFile.Close();
});