示例#1
0
        public async Task <ActionResult> ResizeImage()
        {
            //Get infos about the image and social networks to present

            try
            {
                Guid savedGuid = new Guid(TempData["guid"].ToString());

                if (savedGuid != Guid.Empty)
                {
                    //Retrieve the file operation
                    Operation operation = Task.Run(async() => await _context.GetOperation(savedGuid)).GetAwaiter().GetResult();

                    if (operation != null)
                    {
                        //var directory = new DirectoryInfo(Server.MapPath("~/Files_Repo/"));
                        //var myFile = directory.GetFiles()
                        //    .OrderByDescending(f => f.LastWriteTime)
                        //    .First();
                        UploadedIMGViewModel model = new UploadedIMGViewModel();
                        model.IMG_Name        = operation.FileName;
                        model.IMG_ContentType = operation.ContentType;
                        model.IMG_Guid        = savedGuid.ToString();


                        model.CompatibleNetworks = await ConvertToVM(await _context.GetSocialNetworksCompatibleWithFile(operation));

                        return(View(model));
                    }
                }
                return(View("Error"));
            }
            catch (Exception e)
            {
                TempData["Error"] = e.Message;
                ViewBag.Error     = e.Message;
                throw e;
            }
        }
示例#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"));
        }