<commit_before>﻿using System.IO;
using System.Web;
using N2.Configuration;
using N2.Edit;
using N2.Edit.FileSystem;
using N2.Engine;
using N2.Plugin;
using N2.Web;
using N2.Web.Drawing;

namespace N2.Management.Files
{
	[Service]
	public class UploadedFilesResizer : IAutoStart
	{
		IFileSystem files;
		ImageResizer resizer;
		ImagesElement images;

		public UploadedFilesResizer(IFileSystem files, ImageResizer resizer, EditSection config)
		{
			this.files = files;
			this.resizer = resizer;
			this.images = config.Images;
		}

		void files_FileWritten(object sender, FileEventArgs e)
		{
			if (!IsResizableImagePath(e.VirtualPath))
				return;

			if(images.Sizes.Count == 0)
				return;

			byte[] image;
			using (var s = files.OpenFile(e.VirtualPath))
			{
				image = new byte[s.Length];
				s.Read(image, 0, image.Length);
			}

			foreach (ImageSizeElement size in images.Sizes.AllElements)
			{
				if (!size.ResizeOnUpload)
					continue;

				Url url = e.VirtualPath;
				string resizedPath = ImagesUtility.GetResizedPath(url, size.Name);

				using (var sourceStream = new MemoryStream(image))
				{
					if (size.Width <= 0 && size.Height <= 0)
					{
						using (var destinationStream = files.OpenFile(resizedPath))
						{
							int b;
							while((b = sourceStream.ReadByte()) != -1) 
							{
								destinationStream.WriteByte((byte)b);
							}
						}
					}
					else
					{
						if (!files.FileExists(resizedPath) || size.Replace)
						{
							using (var destinationStream = files.OpenFile(resizedPath))
							{<commit_msg>Delete image before replace to fix file size bug<commit_after>﻿using System.IO;
using System.Web;
using N2.Configuration;
using N2.Edit;
using N2.Edit.FileSystem;
using N2.Engine;
using N2.Plugin;
using N2.Web;
using N2.Web.Drawing;

namespace N2.Management.Files
{
	[Service]
	public class UploadedFilesResizer : IAutoStart
	{
		IFileSystem files;
		ImageResizer resizer;
		ImagesElement images;

		public UploadedFilesResizer(IFileSystem files, ImageResizer resizer, EditSection config)
		{
			this.files = files;
			this.resizer = resizer;
			this.images = config.Images;
		}

		void files_FileWritten(object sender, FileEventArgs e)
		{
			if (!IsResizableImagePath(e.VirtualPath))
				return;

			if(images.Sizes.Count == 0)
				return;

			byte[] image;
			using (var s = files.OpenFile(e.VirtualPath))
			{
				image = new byte[s.Length];
				s.Read(image, 0, image.Length);
			}

			foreach (ImageSizeElement size in images.Sizes.AllElements)
			{
				if (!size.ResizeOnUpload)
					continue;

				Url url = e.VirtualPath;
				string resizedPath = ImagesUtility.GetResizedPath(url, size.Name);

				using (var sourceStream = new MemoryStream(image))
				{
					if (size.Width <= 0 && size.Height <= 0)
					{
						using (var destinationStream = files.OpenFile(resizedPath))
						{
							int b;
							while((b = sourceStream.ReadByte()) != -1) 
							{
								destinationStream.WriteByte((byte)b);
							}
						}
					}
					else
					{
						if (!files.FileExists(resizedPath) || size.Replace)
						{
                            // Delete the image before writing.
                            // Fixes a weird bug where overwriting the original file while it still exists
                            //  leaves the resized image the with the exact same file size as the original even 
                            //  though it should be smaller.
                            if (files.FileExists(resizedPath))
                            {
                                files.DeleteFile(resizedPath);
                            }

							using (var destinationStream = files.OpenFile(resizedPath))
							{
{'fraction_non_alphanumeric': 0.10127613358674993, 'fraction_numerical': 0.0070594623947868584, 'mean_word_length': 2.3985239852398523, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1687927', 'n_tokens_mistral': 1443, 'n_tokens_neox': 1127, 'n_words': 269}