public IActionResult DeleteImage(long userId, long imgid) { try { Domain.Socioboard.Models.ImgLibrary Imglibrary = new ImgLibrary(); DatabaseRepository dbr = new DatabaseRepository(_logger, _env); ImgLibrary lstImages = dbr.Single <ImgLibrary>(t => t.UserId == userId && t.Id == imgid); System.IO.File.Delete(lstImages.LocalImagePath); dbr.Delete <Domain.Socioboard.Models.ImgLibrary>(lstImages); return(Ok("Deleted")); } catch (Exception ex) { return(BadRequest("Something went wrong please try after sometime")); } // return Ok(); }
public IActionResult SaveImageforPrivate(long userId, string imgName, IFormFile files) { try { DatabaseRepository dbr = new DatabaseRepository(_logger, _env); double imgsize = dbr.Find <Domain.Socioboard.Models.ImgLibrary>(t => t.UserId == userId).Sum(t => t.ImageSize); if (imgsize <= 20971520) { long imglength = 0; var filename = ""; var apiimgPath = ""; var uploads = string.Empty; string imgPath = string.Empty; string imagePath = string.Empty; string localpath = string.Empty; string extension = string.Empty; if (files != null) { if (files.Length > 0) { var fileName = Microsoft.Net.Http.Headers.ContentDispositionHeaderValue.Parse(files.ContentDisposition).FileName.Trim('"'); // await file.s(Path.Combine(uploads, fileName)); filename = Microsoft.Net.Http.Headers.ContentDispositionHeaderValue .Parse(files.ContentDisposition) .FileName .Trim('"'); var tempName = Domain.Socioboard.Helpers.SBHelper.RandomString(10) + '.' + fileName.Split('.')[1]; filename = _env.WebRootPath + "\\upload\\UserImages" + $@"\{tempName}"; localpath = filename; imgPath = filename; uploads = _appSettings.ApiDomain + "/api/Media/getUserImages?id=" + $@"{tempName}"; extension = Path.GetExtension(fileName).ToUpper(); if (extension != ".JFIF" && extension != ".JPG" && extension != ".JPEG" && extension != ".Exif" && extension != ".GIF" && extension != ".TIFF" && extension != ".PNG" && extension != ".PPM" && extension != ".BMP" && extension != ".PGM" && extension != ".PBM" && extension != ".PNM") { return(BadRequest("Please select only image file")); } //imglength = new System.IO.FileInfo(filenameforlength).Length; // size += file.Length; try { using (FileStream fs = System.IO.File.Create(filename)) { files.CopyTo(fs); fs.Flush(); } imglength = new System.IO.FileInfo(filename).Length; double totallength = imgsize + imglength; if (totallength > 20971520) { return(BadRequest("You have reached maximum library sapce")); } filename = uploads; // long imglength = new System.IO.FileInfo(imagelocalPath).Length; } catch (System.Exception ex) { if (!string.IsNullOrEmpty(imagePath)) { uploads = imagePath; } } } } else { } Domain.Socioboard.Models.ImgLibrary lstImglibrary = new ImgLibrary(); //long len = (new System.IO.FileInfo(filename)).Length; lstImglibrary.UserId = userId; lstImglibrary.ImageName = imgName; lstImglibrary.ImagePath = filename; lstImglibrary.ImageSize = imglength; lstImglibrary.Imageuploadeddate = DateTime.UtcNow; lstImglibrary.Tags = ""; lstImglibrary.FolderType = "Private"; lstImglibrary.LocalImagePath = localpath; lstImglibrary.Fileextension = extension; int SavedStatus = dbr.Add <Domain.Socioboard.Models.ImgLibrary>(lstImglibrary); if (SavedStatus == 1) { return(Ok("Image saved successfully")); } else { return(BadRequest("Something went wrong")); } } else { return(BadRequest("You have reached maximum library sapce")); } } catch (Exception ex) { return(BadRequest("Something went wrong")); } }