How can I save files inside a temporary folder?

Viewed 61

I am obtaining the directory of the temp or tmp folder, and creating a folder called "TemporaryImages" from it, which as the name says, I am only going to use it to save images with low resolution temporarily.

The problem is that "TemporaryImages" is being created successfully, but the images are not being saved within it, and I don't understand where I'm screwing up.

I'm using C# ASP.NET, on a Linux server.

This code receives a list of images fetched from a database. With "TemporalFolder()" I create or get the directory of the temporary folder, and with the function "SaveImage()" I am sending it the name of the image, the directory of the folder, and the new size of the image.

private void ChangeImageSize(List<OrderViewModel> orderViewModels)
        {
            const int thumbnailWidth = 500;
            string tempFolder;

            try
            {
                tempFolder = TemporaryFolder();

                foreach (var item in orderViewModels)
                {
                    var imageResult = Image.Load("wwwroot/" + item.getImages());

                    this.SaveImage(imageResult, tempFolder, thumbnailWidth);
                }
            }
            catch (Exception ex)
            {
                ViewBag.error = ex.Message.ToString();
                throw new Exception(ex.Message);
            }
        }

With this function I seek to save the image inside the temporary folder:

  private void SaveImage(Image image, string name, int resizeWidth)
{
    var width = image.Width;
    var height = image.Height;

    if (width > resizeWidth)
    {
        height = (int)((double)resizeWidth / width * height);
        width = resizeWidth;
    }

    picture
        .Mutate(i => i.Resize(new Size(width, height)));

    image.Metadata.ExifProfile = null;

    image.SaveAsJpeg(name, new JpegEncoder
    {
        Quality = 100
    });
}

This is the function to create and get the temporary folder:

private string TemporaryFolder()
{
    DirectoryInfo result = Directory.CreateDirectory(Path.GetTempPath() + "TemporaryImages/");

    return result.FullName;
}
1 Answers

First of all, it looks like you are not specifying any file name when saving the file. There are a few options on what filenames to use:

  1. Use numbers, check current files to avoid duplicates.
  2. Use Guids, they are guaranteed to be unique
  3. Use Path.GetTempFileName, this will give a full filename in the root of the temp folder.

Secondly, if you are running asp.net you likely do not have write-permission to the temp folder. You probably do not want to grant your web-process access to your systems temp-folder since that might have security implications.

If the files are truly temporary, as in only valid during a single request, I would recommend keeping them in memory. Images can be saved to a memory stream just fine, and this avoids any issue with cleanup etc. The main reason to write such temporary data to files is either memory constraints, and this should be fairly rare today, or some API that require actual files.

If you are rather want a short lived image storage I would suggest following the standard practices for storing images, i.e. use a database to keep a list of images, a configurable storage folder with correct permissions to store images, and some cleanup process to periodically remove unneeded images, etc.

Related