<commit_before>﻿namespace ImageGallery.Services.Image
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Web;

    using ImageGallery.Data.Common;
    using ImageGallery.Data.Models;

    using Image = ImageGallery.Data.Models.Image;

    public class ImageService : IImageService
    {
        private IRepository<Album, int> albums;

        private IRepository<Image, int> images;

        public ImageService(IRepository<Image, int> images, IRepository<Album, int> albums)
        {
            this.images = images;
            this.albums = albums;
        }

        public void Add(int albumId, HttpPostedFileBase file, HttpServerUtility server)
        {
            var counter = 0;

            if (file == null)
            {
                return;
            }

            if (!Directory.Exists(server.MapPath(Common.Constants.MainContentFolder)))
            {
                Directory.CreateDirectory(server.MapPath(Common.Constants.MainContentFolder));
            }

            if (!Directory.Exists(server.MapPath(Common.Constants.MainContentFolder + "\\" + albumId)))
            {
                Directory.CreateDirectory(server.MapPath(Common.Constants.MainContentFolder + "\\" + albumId));
            }

            if (
                !Directory.Exists(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\"
                        + Common.Constants.ImageFolderOriginal)))
            {
                Directory.CreateDirectory(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\"
                        + Common.Constants.ImageFolderOriginal));
            }

            if (
                !Directory.Exists(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderMiddle)))
            {
                Directory.CreateDirectory(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderMiddle));
            }

            if (
                !Directory.Exists(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderLow)))
            {
                Directory.CreateDirectory(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderLow));
            }

            var lastImage = this.albums.GetById(albumId).Images.LastOrDefault();

            if (lastImage == null)
            {
                counter = 0;
            }
            else
            {
                counter = lastImage.ImageIdentificator;
            }

            var originalFilename = Path.GetFileName(file.FileName);
            var newFilename = (counter + 1).ToString().PadLeft(4, '0') + ".jpg";
            var originalPath =
                server.MapPath(
                    Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderOriginal
                    + "\\");
            var middlePath =
                server.MapPath(
                    Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderMiddle
                    + "\\");
            var lowPath =
                server.MapPath(
                    Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderLow + "\\");
            file.SaveAs(originalPath + newFilename);

            using (
                var img = this.ScaleImage(
                    new Bitmap(originalPath + newFilename), 
                    Common.Constants.ImageMiddleMaxSize, 
                    Common.Constants.ImageMiddleMaxSize))
            {
                img.Save(middlePath + newFilename, ImageFormat.Jpeg);
            }

            using (
                var img = this.ScaleImage(
                    new Bitmap(originalPath + newFilename), 
                    Common.Constants.ImageLowMaxSize, 
                    Common.Constants.ImageLowMaxSize))
            {
                img.Save(lowPath + newFilename, ImageFormat.Jpeg);
            }

            var newDbImage = new Data.Models.Image()
                                 {
                                     AlbumId = albumId, 
                                     Title = "aaaaaaaaaaaaaaaaaa", 
                                     Description = "aaaaaaaaaaaaaaaa", 
                                     OriginalFileName = originalFilename, 
                                     FileName = newFilename, 
                                     ImageIdentificator = counter + 1
                                 };

            this.images.Add(newDbImage);
        }

        public IQueryable<Image> GetAll()
        {
            return this.images.All();
        }

        public Image GetById(int id)<commit_msg>Added GC cleanup<commit_after>﻿namespace ImageGallery.Services.Image
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Linq;
    using System.Web;

    using ImageGallery.Data.Common;
    using ImageGallery.Data.Models;

    using Image = ImageGallery.Data.Models.Image;

    public class ImageService : IImageService
    {
        private IRepository<Album, int> albums;

        private IRepository<Image, int> images;

        public ImageService(IRepository<Image, int> images, IRepository<Album, int> albums)
        {
            this.images = images;
            this.albums = albums;
        }

        public void Add(int albumId, HttpPostedFileBase file, HttpServerUtility server)
        {
            var counter = 0;

            if (file == null)
            {
                return;
            }

            if (!Directory.Exists(server.MapPath(Common.Constants.MainContentFolder)))
            {
                Directory.CreateDirectory(server.MapPath(Common.Constants.MainContentFolder));
            }

            if (!Directory.Exists(server.MapPath(Common.Constants.MainContentFolder + "\\" + albumId)))
            {
                Directory.CreateDirectory(server.MapPath(Common.Constants.MainContentFolder + "\\" + albumId));
            }

            if (
                !Directory.Exists(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\"
                        + Common.Constants.ImageFolderOriginal)))
            {
                Directory.CreateDirectory(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\"
                        + Common.Constants.ImageFolderOriginal));
            }

            if (
                !Directory.Exists(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderMiddle)))
            {
                Directory.CreateDirectory(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderMiddle));
            }

            if (
                !Directory.Exists(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderLow)))
            {
                Directory.CreateDirectory(
                    server.MapPath(
                        Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderLow));
            }

            var lastImage = this.albums.GetById(albumId).Images.LastOrDefault();

            if (lastImage == null)
            {
                counter = 0;
            }
            else
            {
                counter = lastImage.ImageIdentificator;
            }

            var originalFilename = Path.GetFileName(file.FileName);
            var newFilename = (counter + 1).ToString().PadLeft(4, '0') + ".jpg";
            var originalPath =
                server.MapPath(
                    Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderOriginal
                    + "\\");
            var middlePath =
                server.MapPath(
                    Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderMiddle
                    + "\\");
            var lowPath =
                server.MapPath(
                    Common.Constants.MainContentFolder + "\\" + albumId + "\\" + Common.Constants.ImageFolderLow + "\\");
            file.SaveAs(originalPath + newFilename);

            using (
                var img = this.ScaleImage(
                    new Bitmap(originalPath + newFilename), 
                    Common.Constants.ImageMiddleMaxSize, 
                    Common.Constants.ImageMiddleMaxSize))
            {
                img.Save(middlePath + newFilename, ImageFormat.Jpeg);
                img.Dispose();
            }

            using (
                var img = this.ScaleImage(
                    new Bitmap(originalPath + newFilename), 
                    Common.Constants.ImageLowMaxSize, 
                    Common.Constants.ImageLowMaxSize))
            {
                img.Save(lowPath + newFilename, ImageFormat.Jpeg);
                img.Dispose();
            }

            GC.Collect();

            var newDbImage = new Data.Models.Image()
                                 {
                                     AlbumId = albumId, 
                                     Title = "aaaaaaaaaaaaaaaaaa", 
                                     Description = "aaaaaaaaaaaaaaaa", 
                                     OriginalFileName = originalFilename, 
                                     FileName = newFilename, 
                                     ImageIdentificator = counter + 1
                                 };

            this.images.Add(newDbImage);
        }

        public IQueryable<Image> GetAll()
        {
            return this.images.All();
        }

        public Image GetById(int id)
{'fraction_non_alphanumeric': 0.09664429530201342, 'fraction_numerical': 0.0011505273250239693, 'mean_word_length': 1.2626898047722344, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5518874', 'n_tokens_mistral': 2548, 'n_tokens_neox': 2303, 'n_words': 386}