コード例 #1
0
        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));
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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()));
        }
コード例 #4
0
        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));
        }
コード例 #5
0
        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));
        }