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

Apparent Issue with ImageHandler helper #12

Open
kpshubert opened this issue Jul 5, 2023 · 1 comment
Open

Apparent Issue with ImageHandler helper #12

kpshubert opened this issue Jul 5, 2023 · 1 comment

Comments

@kpshubert
Copy link

Porting this project to MVC Core 7 required a major rewrite (not your fault or problem, just life).

Part of the rewrite was understanding the existing code..

It appears to me that there's an issue with the ImageHandler helper class.

Based on what I'm seeing, the LoadImage method is pulling the entire file from the client, then resizing, to create a thumbnail.

This means a couple of things.

  1. The file is being loaded twice (once in the ImageHandler helper class and again in the FilesHelper UploadPartialFile).
  2. The upload occurring in the ImageHandler helper class appears to be a non-chunked upload (meaning the one occurring in the FilesHelper helper class is not only redundant, but that one of the two is also potentially problematic in that it uploads the whole file at one go).

I'm not sure, but it seems like it might be possible to upload the file chunked, then pass the file stream to the ImageHandler class before closing it?

One way or another though, if people are wondering why there's an issue with large files, this may be part of the reason.

I know it may have been some time since you looked at or worked on this. I just thought it might be helpful to others to know this "issue" exists.

@kpshubert
Copy link
Author

I have written a simple fix for this issue that I think will do the trick.

It involves doing what I suggested.

I simply created an overload for ImageHandler.LoadImage that takes a FileStream parameter that looks like this:

public static Bitmap LoadImage(FileStream fsIn) { return (Bitmap)Image.FromStream(fsIn); }

After doing that, I moved the ImageHandler related code between the fs.Flush() and fs.Close calls in FilersHelper.UploadPartialfile():

   private void UploadPartialFile(string fileName, HttpContext requestContext, List<ViewDataUploadFilesResult> statuses)
    {
        var request = requestContext.Request;
        var file = request.Form.Files[0];
        var inputStream = file.OpenReadStream(); // .InputStream;
        string patchOnServer = Path.Combine(StorageRoot);
        var fullName = Path.Combine(patchOnServer, Path.GetFileName(file.FileName));
        var ThumbfullPath = Path.Combine(fullName, Path.GetFileName(file.FileName + "80x80.jpg"));
        using (var fs = new FileStream(fullName, FileMode.Append, FileAccess.Write))
        {
            var buffer = new byte[1024];

            var l = inputStream.Read(buffer, 0, 1024);
            while (l > 0)
            {
                fs.Write(buffer, 0, l);
                l = inputStream.Read(buffer, 0, 1024);
            }
            fs.Flush();

            ImageHandler handler = new ImageHandler();
            var ImageBit = ImageHandler.LoadImage(fs);
            handler.Save(ImageBit, 80, 80, 10, ThumbfullPath);

            fs.Close();
        }
        statuses.Add(UploadResult(file.FileName, (int)file.Length, file.FileName));
    }

I have yet to test this approach, but at present, it looks as though it should do the job.

Any and all comments are welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant