Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access to the path 'C:\Windows\TEMP\cwebp.exe' is denied. #5

Open
duongtran1201 opened this issue Sep 7, 2022 · 1 comment
Open
Labels
bug Something isn't working

Comments

@duongtran1201
Copy link

duongtran1201 commented Sep 7, 2022

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?

@webfinesse
Copy link

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();
});

@frankodoom frankodoom added the bug Something isn't working label Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants