public async Task <IActionResult> Put(int key, [FromBody] Album album) { String userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } var entry = await _context.Albums.FindAsync(key); if (entry == null) { return(NotFound()); } if (String.CompareOrdinal(entry.CreatedBy, userId) != 0) { return(StatusCode(401)); } entry.Desp = album.Desp; entry.IsPublic = album.IsPublic; entry.Title = album.Title; _context.Entry(entry).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Updated(album)); }
public async Task <IActionResult> Delete(int key) { String userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } var entry = await _context.Albums.FindAsync(key); if (entry == null) { return(NotFound()); } if (String.CompareOrdinal(entry.CreatedBy, userId) != 0) { return(StatusCode(401)); } _context.Albums.Remove(entry); await _context.SaveChangesAsync(); return(StatusCode(204)); // HttpStatusCode.NoContent }
public IActionResult Get() { // Be noted: without the NoTracking setting, the query for $select=HomeAddress with throw exception: // A tracking query projects owned entity without corresponding owner in result. Owned entities cannot be tracked without their owner... _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; string userId = ControllerUtility.GetUserID(this._httpContextAccessor); var rst = _context.Photos.Where(p => p.IsPublic == true); return(Ok(rst)); }
public IActionResult Post([FromBody] PhotoTag ptag) { string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } this._context.PhotoTags.Add(ptag); _context.SaveChanges(); return(Created(ptag)); }
public async Task <IActionResult> Delete(string key) { var entry = await _context.Photos.FindAsync(key); if (entry == null) { return(NotFound()); } string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } // Delete the file try { String strFullFile = Startup.UploadFolder + "\\" + entry.FileUrl; var trashFolder = Startup.UploadFolder + "\\Trash"; if (!Directory.Exists(trashFolder)) { Directory.CreateDirectory(trashFolder); } if (System.IO.File.Exists(strFullFile)) { System.IO.File.Move(strFullFile, trashFolder + "\\" + entry.FileUrl); } strFullFile = Startup.UploadFolder + "\\" + entry.ThumbnailFileUrl; if (System.IO.File.Exists(strFullFile)) { System.IO.File.Move(strFullFile, trashFolder + "\\" + entry.ThumbnailFileUrl); } } catch { // DO nothing } // Delete DB entry _context.Photos.Remove(entry); await _context.SaveChangesAsync(); return(StatusCode(204)); // HttpStatusCode.NoContent }
public IActionResult Post([FromBody] Album album) { String userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } // Create new entries album.CreatedAt = DateTime.Now; album.CreatedBy = userId; this._context.Albums.Add(album); _context.SaveChanges(); return(Created(album)); }
public IActionResult GetRelatedPhotos(int key, string AccessCode = null) { Album selalb = null; String usrID = ControllerUtility.GetUserID(this._httpContextAccessor); if (!String.IsNullOrWhiteSpace(usrID)) { selalb = _context.Albums.FirstOrDefault(p => p.Id == key && (p.IsPublic == true || p.CreatedBy == usrID)); } else { selalb = _context.Albums.FirstOrDefault(c => c.Id == key); if (selalb != null) { if (!string.IsNullOrEmpty(selalb.AccessCode)) { if (AccessCode == null) { return(BadRequest("Access code is required")); } if (string.CompareOrdinal(AccessCode, selalb.AccessCode) != 0) { return(BadRequest("Access Code is wrong")); } } } } // Album ID if (selalb == null) { return(NotFound()); } var phts = from ap in _context.AlbumPhotos join photo in _context.Photos on ap.PhotoID equals photo.PhotoId where ap.AlbumID == key select photo; return(Ok(phts)); }
public async Task <IActionResult> Patch([FromODataUri] int key, [FromBody] Delta <Album> patchAlbum) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } String userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } if (patchAlbum != null) { var entry = await _context.Albums.FindAsync(key); if (entry == null) { return(NotFound()); } if (String.CompareOrdinal(entry.CreatedBy, userId) != 0) { return(StatusCode(401)); } patchAlbum.Patch(entry); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.Entry(entry).State = EntityState.Modified; await _context.SaveChangesAsync(); return(new ObjectResult(entry)); } else { return(BadRequest(ModelState)); } }
public async Task <IActionResult> Post([FromBody] UserDetail usrdtl) { if (!ModelState.IsValid) { return(BadRequest("Invalid model state")); } var userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } _context.UserDetails.Add(usrdtl); await _context.SaveChangesAsync(); return(Created(usrdtl)); }
public async Task <IActionResult> Patch([FromODataUri] string key, [FromBody] Delta <Photo> patchPhoto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } if (patchPhoto != null) { var entry = await _context.Photos.FindAsync(key); if (entry == null) { return(NotFound()); } patchPhoto.Patch(entry); // patchPhoto.App(entry, (Microsoft.AspNetCore.JsonPatch.Adapters.IObjectAdapter)ModelState); if (!ModelState.IsValid) { return(BadRequest(ModelState)); } _context.Entry(entry).State = EntityState.Modified; await _context.SaveChangesAsync(); return(new ObjectResult(entry)); } else { return(BadRequest(ModelState)); } }
public async Task <IActionResult> Delete([FromODataUri] string keyPhotoID, [FromODataUri] string keyTagString) { string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } var entity = this._context.PhotoTags.Find(keyPhotoID, keyTagString); if (entity == null) { return(NotFound()); } this._context.PhotoTags.Remove(entity); await _context.SaveChangesAsync(); return(Ok()); }
public async Task <IActionResult> Post([FromBody] AlbumPhoto albpto) { var entry = await _context.AlbumPhotos.FindAsync(albpto.AlbumID, albpto.PhotoID); if (entry != null) { return(BadRequest()); } string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } _context.AlbumPhotos.Add(albpto); await _context.SaveChangesAsync(); return(Created(albpto)); }
public async Task <IActionResult> Put(string key, [FromBody] UserDetail usrdtl) { if (!ModelState.IsValid) { return(BadRequest("Invalid model state")); } if (string.CompareOrdinal(key, usrdtl.UserID) != 0) { return(BadRequest("Key is unmatched")); } var userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } // Can not change other user if (String.CompareOrdinal(userId, key) != 0) { return(StatusCode(401)); } var entry = await _context.UserDetails.FindAsync(key); if (entry == null) { return(NotFound()); } entry.DisplayAs = usrdtl.DisplayAs; entry.AlbumCreate = usrdtl.AlbumCreate; entry.PhotoUpload = usrdtl.PhotoUpload; entry.UploadFileMaxSize = usrdtl.UploadFileMaxSize; entry.UploadFileMinSize = usrdtl.UploadFileMinSize; _context.Entry(entry).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Updated(usrdtl)); }
public IActionResult Delete(int AlbumID, string PhotoID) { // Delete code here var entry = _context.AlbumPhotos.Find(AlbumID, PhotoID); if (entry == null) { return(NotFound()); } string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } _context.AlbumPhotos.Remove(entry); _context.SaveChanges(); return(Ok()); }
public IActionResult ChangeAccessCode(int key, ODataActionParameters paras) { String userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } var album = _context.Albums.FirstOrDefault(c => c.Id == key); if (album == null) { return(NotFound()); } if (String.CompareOrdinal(album.CreatedBy, userId) != 0) { return(StatusCode(401)); } var AccessCode = (string)paras["AccessCode"]; if (string.IsNullOrEmpty(AccessCode)) { album.AccessCodeHint = null; } else { album.AccessCodeHint = AccessCode; } _context.Attach(album); _context.SaveChanges(); return(Ok(true)); }
public async Task <IActionResult> Put(string key, [FromBody] Photo pto) { var entry = await _context.Photos.FindAsync(key); if (entry == null) { return(NotFound()); } string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } entry.Desp = pto.Desp; entry.IsPublic = pto.IsPublic; entry.Title = pto.Title; _context.Entry(entry).State = EntityState.Modified; await _context.SaveChangesAsync(); return(Updated(pto)); }
public IActionResult Get(int key) { String usrID = ControllerUtility.GetUserID(this._httpContextAccessor); if (!String.IsNullOrEmpty(usrID)) { var alb = from almphoto in _context.AlbumPhotos group almphoto by almphoto.AlbumID into almphotos // where almphotos.Key equals usrObj.Value select new { Key = almphotos.Key, PhotoCount = almphotos.Count() } into almphotocnts join alm in _context.Albums on new { Id = almphotocnts.Key, IsAllowed = true } equals new { Id = alm.Id, IsAllowed = alm.IsPublic || alm.CreatedBy == usrID } where alm.Id == key select new Album { Id = alm.Id, Title = alm.Title, Desp = alm.Desp, CreatedBy = alm.CreatedBy, CreatedAt = alm.CreatedAt, IsPublic = alm.IsPublic, AccessCodeHint = alm.AccessCodeHint, PhotoCount = almphotocnts.PhotoCount }; if (alb.Count() != 1) { return(NotFound()); } return(Ok(alb.First())); } var alb2 = from almphoto in _context.AlbumPhotos group almphoto by almphoto.AlbumID into almphotos // where almphotos.Key equals usrObj.Value select new { Key = almphotos.Key, PhotoCount = almphotos.Count() } into almphotocnts join alm in _context.Albums on new { Id = almphotocnts.Key, IsPublic = true } equals new { alm.Id, alm.IsPublic } where alm.Id == key select new Album { Id = alm.Id, Title = alm.Title, Desp = alm.Desp, CreatedBy = alm.CreatedBy, CreatedAt = alm.CreatedAt, IsPublic = alm.IsPublic, AccessCodeHint = alm.AccessCodeHint, PhotoCount = almphotocnts.PhotoCount }; if (alb2.Count() != 1) { return(NotFound()); } return(Ok(alb2.First())); }
public IActionResult SearchPhotoInAlbum(int AlbumID, string AccessCode = null) { Album selalb = null; // Is a logon user? string userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId != null) { selalb = _context.Albums.FirstOrDefault(p => p.Id == AlbumID && (p.IsPublic == true || p.CreatedBy == userId)); } else { selalb = _context.Albums.FirstOrDefault(c => c.Id == AlbumID); if (selalb != null) { if (!string.IsNullOrEmpty(selalb.AccessCode)) { if (AccessCode == null) { return(BadRequest("Access code is required")); } if (string.CompareOrdinal(AccessCode, selalb.AccessCode) != 0) { return(BadRequest("Access Code is wrong")); } } } } // Album ID if (selalb == null) { return(NotFound()); } var phts = from apv in _context.AlbumPhotoViews where apv.AlbumID == AlbumID select new PhotoView { PhotoId = apv.PhotoId, Title = apv.Title, Desp = apv.Desp, Width = apv.Width, Height = apv.Height, ThumbWidth = apv.ThumbWidth, ThumbHeight = apv.ThumbHeight, FileUrl = apv.FileUrl, ThumbnailFileUrl = apv.ThumbnailFileUrl, UploadedTime = apv.UploadedTime, UploadedBy = apv.UploadedBy, OrgFileName = apv.OrgFileName, IsOrgThumbnail = apv.IsOrgThumbnail, IsPublic = apv.IsPublic, CameraMaker = apv.CameraMaker, CameraModel = apv.CameraModel, LensModel = apv.LensModel, AVNumber = apv.AVNumber, ShutterSpeed = apv.ShutterSpeed, ISONumber = apv.ISONumber, Tags = apv.Tags }; return(Ok(phts)); }
public async Task <IActionResult> UploadPhotos(ICollection <IFormFile> files) { if (Request.Form.Files.Count <= 0) { return(BadRequest("No Files")); } var userId = ControllerUtility.GetUserID(this._httpContextAccessor); if (userId == null) { return(StatusCode(401)); } // Only care about the first file var file = Request.Form.Files[0]; var fileSize = file.Length; var filename1 = file.FileName; var idx1 = filename1.LastIndexOf('.'); var fileext = filename1.Substring(idx1); var filerst = new PhotoFileSuccess(); // Copy file to uploads folder filerst.deleteType = "DELETE"; var randomFileName = Guid.NewGuid().ToString("N"); filerst.name = randomFileName; var targetfilename = randomFileName + fileext; filerst.size = (int)fileSize; // To avoid mass change the existing records in db, the URL won't return. // filerst.url = "PhotoFile/" + targetfilename; // filerst.thumbnailUrl = "PhotoFile/" + randomFileName + ".thumb" + fileext; filerst.url = targetfilename; filerst.thumbnailUrl = randomFileName + ".thumb" + fileext; filerst.deleteUrl = filerst.url; PhotoFileErrorResult errrst = null; PhotoFileSuccessResult succrst = new PhotoFileSuccessResult(); try { var filePath = Path.Combine(Startup.UploadFolder, targetfilename); var thmFilePath = Path.Combine(Startup.UploadFolder, randomFileName + ".thumb" + fileext); using (var fileStream = new FileStream(filePath, FileMode.Create)) { await file.CopyToAsync(fileStream); using (IMagickImage image = new MagickImage(filePath)) { filerst.width = image.Width; filerst.height = image.Height; // Add the photo var pht = new Photo(); pht.PhotoId = randomFileName; pht.Title = pht.PhotoId; pht.Desp = pht.PhotoId; pht.FileUrl = filerst.url; var exifprofile = image.GetExifProfile(); if (exifprofile != null) { // AV Number IExifValue value = exifprofile.Values.FirstOrDefault(val => val.Tag == ExifTag.ApertureValue); try { if (value != null) { pht.AVNumber = value.GetValue().ToString(); } } catch { // DO nothing } // Camera Maker value = exifprofile.Values.FirstOrDefault(val => val.Tag == ExifTag.Make); try { if (value != null) { pht.CameraMaker = value.GetValue().ToString(); } } catch { // DO nothing } // Camera Model value = exifprofile.Values.FirstOrDefault(val => val.Tag == ExifTag.Model); try { if (value != null) { pht.CameraModel = value.GetValue().ToString(); } } catch { // DO nothing } // ISO number value = exifprofile.Values.FirstOrDefault(val => val.Tag == ExifTag.ISOSpeed); try { if (value != null) { pht.ISONumber = (int)value.GetValue(); } } catch { // DO nothing } // Lens Model value = exifprofile.Values.FirstOrDefault(val => val.Tag == ExifTag.LensModel); try { if (value != null) { pht.LensModel = value.GetValue().ToString(); } } catch { // DO nothing } // Shutter Speed value = exifprofile.Values.FirstOrDefault(val => val.Tag == ExifTag.ShutterSpeedValue); try { if (value != null) { pht.ShutterSpeed = (string)value.GetValue(); } } catch { // DO nothing } } var bThumbnailCreated = false; // Retrieve the exif information ExifProfile profile = (ExifProfile)image.GetExifProfile(); if (profile != null) { using (IMagickImage thumbnail = profile.CreateThumbnail()) { // Check if exif profile contains thumbnail and save it if (thumbnail != null) { thumbnail.Write(thmFilePath); bThumbnailCreated = true; filerst.thumbwidth = thumbnail.Width; filerst.thumbheight = thumbnail.Height; pht.ThumbnailFileUrl = filerst.thumbnailUrl; pht.ThumbHeight = filerst.thumbheight; pht.ThumbWidth = filerst.thumbwidth; pht.IsOrgThumbnail = true; } } } if (!bThumbnailCreated) { MagickGeometry size = new MagickGeometry(256, 256); // This will resize the image to a fixed size without maintaining the aspect ratio. // Normally an image will be resized to fit inside the specified size. size.IgnoreAspectRatio = false; image.Resize(size); filerst.thumbwidth = image.Width; filerst.thumbheight = image.Height; pht.ThumbnailFileUrl = filerst.thumbnailUrl; pht.ThumbHeight = filerst.thumbheight; pht.ThumbWidth = filerst.thumbwidth; pht.IsOrgThumbnail = false; // Save the result image.Write(thmFilePath); } pht.UploadedBy = userId; pht.UploadedTime = DateTime.Now; this._context.Photos.Add(pht); _context.SaveChanges(); } } succrst.files = new List <PhotoFileSuccess>(); succrst.files.Append(filerst); } catch (Exception exp) { errrst = new PhotoFileErrorResult(); var fileerr = new PhotoFileError(); fileerr.error = exp.Message; fileerr.name = filename1; errrst.files = new List <PhotoFileError>(); errrst.files.Append(fileerr); } if (errrst != null) { return(new JsonResult(errrst)); } return(new JsonResult(filerst)); }
public IActionResult Get() { _context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; String usrID = ControllerUtility.GetUserID(this._httpContextAccessor); if (!String.IsNullOrEmpty(usrID)) { var alb2 = from album in _context.Albums where album.IsPublic == true || album.CreatedBy == usrID select new Album { Id = album.Id, Title = album.Title, Desp = album.Desp, CreatedBy = album.CreatedBy, CreatedAt = album.CreatedAt, IsPublic = album.IsPublic, AccessCodeHint = album.AccessCodeHint, PhotoCount = 0, }; var albnums = from almphoto in _context.AlbumPhotos join selalbum in alb2 on almphoto.AlbumID equals selalbum.Id group almphoto by almphoto.AlbumID into almphotos select new { ID = almphotos.Key, PhotoCount = almphotos.Count() }; foreach (var album in alb2) { var albnum = albnums.FirstOrDefault(p => p.ID == album.Id); if (albnum != null) { album.PhotoCount = albnum.PhotoCount; } } //var albs = from almphoto in _context.AlbumPhotos // group almphoto by almphoto.AlbumID into almphotos // select new // { // ID = almphotos.Key, // PhotoCount = almphotos.Count() // } into almphotocnts // join alm in _context.Albums // on new { Id = almphotocnts.ID, IsAllowed = true } equals new { Id = alm.Id, IsAllowed = alm.IsPublic || alm.CreatedBy == usrID } // select new Album // { // Id = alm.Id, // Title = alm.Title, // Desp = alm.Desp, // CreatedBy = alm.CreatedBy, // CreatedAt = alm.CreatedAt, // IsPublic = alm.IsPublic, // AccessCodeHint = alm.AccessCodeHint, // PhotoCount = almphotocnts.PhotoCount // }; return(Ok(alb2)); } var rst2 = from almphoto in _context.AlbumPhotos group almphoto by almphoto.AlbumID into almphotos select new { ID = almphotos.Key, PhotoCount = almphotos.Count() } into almphotocnts join alm in _context.Albums on new { Id = almphotocnts.ID, IsPublic = true } equals new { alm.Id, alm.IsPublic } select new Album { Id = alm.Id, Title = alm.Title, Desp = alm.Desp, CreatedBy = alm.CreatedBy, CreatedAt = alm.CreatedAt, IsPublic = alm.IsPublic, AccessCodeHint = alm.AccessCodeHint, PhotoCount = almphotocnts.PhotoCount }; return(Ok(rst2)); }