Esempio n. 1
0
        public async Task <Guid> SaveFileInDB(HttpPostedFileBase file)
        {
            if (file != null)
            {
                //create new operation
                Operation operation = new Operation();

                var task = Task.Run(
                    () =>
                {
                    operation.Id           = Guid.NewGuid();
                    operation.ContentType  = file.ContentType;
                    operation.FileName     = file.FileName;
                    operation.FileUploaded = FileConversionMethods.ConvertFileToByteArray(file.InputStream).Result;
                    operation.Date         = DateTime.Now;
                    Image image            = Image.FromStream(file.InputStream);
                    operation.Width        = image.PhysicalDimension.Width;
                    operation.Height       = image.PhysicalDimension.Height;
                }
                    );


                await task;
                //save operation in DB
                this.Operations.Add(operation);
                this.SaveChanges();

                return(operation.Id);
            }
            return(Guid.Empty);
        }
Esempio n. 2
0
        public async Task <ActionResult> ResizeImage(UploadedIMGViewModel model)
        {
            if (ModelState.IsValid)
            {
                //get original image on server
                Operation operation = Task.Run(async() => await _context.GetOperation(new Guid(model.IMG_Guid))).GetAwaiter().GetResult();

                if (operation != null)
                {
                    Image img = Task.Run(async() => await FileConversionMethods.ConvertByteArrayToImage(operation.FileUploaded)).GetAwaiter().GetResult();


                    if (img != null)
                    {
                        MemoryStream zipStream = new MemoryStream();
                        Dictionary <string, MemoryStream> memoryStreams = new Dictionary <string, MemoryStream>();

                        //resize the images by selected profiles and compile in a zip file and send as download
                        foreach (var network in model.CompatibleNetworks)
                        {
                            if (network.IsSelected)
                            {
                                foreach (var profile in network.IMG_Profile_Specs)
                                {
                                    if (profile.IsSelected)
                                    {
                                        var imageResized = await ResizingMethods.ResizeImage(img, profile.Width, profile.Height);

                                        //save img in folder Files_ToZip
                                        //imageResized.Save(Path.Combine(Server.MapPath("~/Files_ToZip/"), network.Name + "-" + profile.ProfileName.Replace(" ", "_") + "__" + model.IMG_Name));
                                        MemoryStream ms          = new MemoryStream();
                                        ImageFormat  imageFormat = Utility.GetImageFormat(operation.ContentType.Split('/')[1]);
                                        imageResized.Save(ms, imageFormat);
                                        memoryStreams.Add(network.Name + "/" + network.Name + "-" + profile.ProfileName.Replace(" ", "_") + "__" + model.IMG_Name, ms);
                                    }
                                }
                            }
                        }


                        //create the zip file to be downloaded
                        using (var archive = ZipArchive.Create())
                        {
                            //archive.AddAllFromDirectory(Server.MapPath("~/Files_ToZip/"));
                            foreach (var ms in memoryStreams)
                            {
                                if (!string.IsNullOrEmpty(ms.Key) && ms.Value != null)
                                {
                                    archive.AddEntry(ms.Key, ms.Value, ms.Value.Length, DateTime.Now);
                                }
                            }



                            //archive.SaveTo(Path.Combine(Server.MapPath("~/File_Zip/") + "ResizedImages.zip"), CompressionType.Deflate);
                            archive.SaveTo(zipStream);
                        }

                        //free img ressource
                        img.Dispose();

                        // delete files and download result
                        //memoryStreams = null;
                        //List<string> filesToDelete = new List<string>(Directory.GetFiles(Server.MapPath("~/Files_ToZip/")));
                        //filesToDelete.AddRange(Directory.GetFiles(Server.MapPath("~/Files_Repo/")));

                        //foreach (string file in filesToDelete)
                        //{
                        //    //if (file.Contains(fileUniqueName.ToString()))
                        //    //{
                        //    FileInfo fi = new FileInfo(file);
                        //    if (fi != null && fi.Exists && fi.FullName != model.IMG_Path)
                        //        fi.Delete();
                        //    //}
                        //}

                        Task.Run(async() => await _context.EmptyByteArray(operation)).GetAwaiter().GetResult();

                        //Response.ClearContent();
                        //Response.ClearHeaders();
                        //Set zip file name

                        //Response.AppendHeader("statusCode", "200");
                        string fileName = "ResizedImages_" + operation.Id.ToString().Substring(5, 5) + "_" + DateTime.Now.ToShortDateString() + "_resizeit.zip";
                        //Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
                        var cd = new System.Net.Mime.ContentDisposition
                        {
                            // for example foo.bak
                            FileName = fileName,

                            // always prompt the user for downloading, set to true if you want
                            // the browser to try to show the file inline
                            Inline = true,
                        };
                        Response.AppendHeader("Content-Disposition", cd.ToString());



                        return(File(zipStream.ToArray(), "application/rar"));//File(Path.Combine(Server.MapPath("~/File_Zip/") + "ResizedImages.zip"), "application/zip");//RedirectToAction("Contact");
                    }
                }
                return(View("Error"));
            }

            return(RedirectToAction("Index"));
        }