示例#1
0
        /// <summary>
        /// Gets the media object with the specified <paramref name="id" />.
        /// Example: api/mediaitems/4/get
        /// </summary>
        /// <param name="id">The media object ID.</param>
        /// <returns>An instance of <see cref="Entity.MediaItem" />.</returns>
        /// <exception cref="System.Web.Http.HttpResponseException">Thrown when the media object is not found, the user
        /// doesn't have permission to view it, or some other error occurs.</exception>
        public Entity.MediaItem Get(int id)
        {
            try
            {
                IGalleryObject mediaObject = Factory.LoadMediaObjectInstance(id);
                SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.ViewAlbumOrMediaObject, RoleController.GetGalleryServerRolesForUser(), mediaObject.Parent.Id, mediaObject.GalleryId, Utils.IsAuthenticated, mediaObject.Parent.IsPrivate, ((IAlbum)mediaObject.Parent).IsVirtualAlbum);
                var siblings         = mediaObject.Parent.GetChildGalleryObjects(GalleryObjectType.MediaObject, !Utils.IsAuthenticated).ToSortedList();
                int mediaObjectIndex = siblings.IndexOf(mediaObject);

                return(GalleryObjectController.ToMediaItem(mediaObject, mediaObjectIndex + 1, MediaObjectHtmlBuilder.GetMediaObjectHtmlBuilderOptions(mediaObject)));
            }
            catch (InvalidMediaObjectException)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content      = new StringContent(String.Format("Could not find media object with ID = {0}", id)),
                    ReasonPhrase = "Media Object Not Found"
                });
            }
            catch (GallerySecurityException)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Forbidden));
            }
            catch (Exception ex)
            {
                AppEventController.LogError(ex);

                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.InternalServerError)
                {
                    Content      = Utils.GetExStringContent(ex),
                    ReasonPhrase = "Server Error"
                });
            }
        }
        public async Task <IActionResult> Get(int id)
        {
            // GET /api/mediaitems/get/4
            try
            {
                //var settings = new AddMediaObjectSettings { AlbumId = 2, CurrentUserName = "******", DiscardOriginalFile = false, FileName = "Desert.jpg", FileNameOnServer = "Desert.jpg" };

                //var results = await _galleryObjectController.AddMediaObject(settings);

                IGalleryObject mediaObject = Factory.LoadMediaObjectInstance(id);
                SecurityManager.ThrowIfUserNotAuthorized(SecurityActions.ViewAlbumOrMediaObject, await _userController.GetGalleryServerRolesForUser(), mediaObject.Parent.Id, mediaObject.GalleryId, _userController.IsAuthenticated, mediaObject.Parent.IsPrivate, ((IAlbum)mediaObject.Parent).IsVirtualAlbum);
                var siblings         = mediaObject.Parent.GetChildGalleryObjects(GalleryObjectType.MediaObject, !_userController.IsAuthenticated).ToSortedList();
                int mediaObjectIndex = siblings.IndexOf(mediaObject);

                return(new JsonResult(_galleryObjectController.ToMediaItem(mediaObject, mediaObjectIndex + 1, _htmlController.GetMediaObjectHtmlBuilderOptions(mediaObject))));
            }
            catch (InvalidMediaObjectException)
            {
                return(NotFound($"Could not find media object with ID {id}"));
            }
            catch (GallerySecurityException)
            {
                return(Forbid());
            }
            catch (Exception ex)
            {
                AppEventController.LogError(ex);

                return(StatusCode(500, _exController.GetExString(ex)));
            }
        }