예제 #1
0
        public HttpResponseMessage Put(int id, [FromBody] Models.Photo updatedPhoto)
        {
            updatedPhoto.Id = id;

            ServiceData.Models.Photo found = _photoRepository.GetById(id);
            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            if (!IsSameUser(found))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            ServiceData.Models.Photo final = _photoRepository.Update(Models.Photo.ToServiceModel(updatedPhoto, true));

            if (final == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            UpdateShares(final);

            ServerUtils.LogTelemetryEvent(User.Identity.Name, "UpdatePhoto");
            PostLog("Photos_Update");
            return(Request.CreateResponse(HttpStatusCode.OK, Models.Photo.ToAppModel(final, true)));
        }
예제 #2
0
        public async Task <HttpResponseMessage> Get(string imageId, bool thumb = false)
        {
            int id;

            if (string.IsNullOrEmpty(imageId) || !Int32.TryParse(imageId, out id))
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            ServiceData.Models.Photo found = _photoRepository.GetById(id);
            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            ServiceData.Models.UserCondition foundCond = _conditionRepository.GetById(found.UserCondition.Id);
            if (foundCond.Owner.Email != User.Identity.Name)
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            string target = (thumb) ? found.ThumbUrl : found.Url;

            CloudBlobContainer container = await GetBlobContainer();

            Stream    blobStream = new MemoryStream();
            CloudBlob photoBlob  = container.GetBlobReference(target.Replace(ConfidentialData.BlobStorageUrl, ""));

            KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(ServerUtils.GetToken);
            IKey rsa = await cloudResolver.ResolveKeyAsync(ConfidentialData.KeyLocation, CancellationToken.None);

            BlobEncryptionPolicy policy  = new BlobEncryptionPolicy(null, cloudResolver);
            BlobRequestOptions   options = new BlobRequestOptions()
            {
                EncryptionPolicy = policy
            };

            await photoBlob.DownloadToStreamAsync(blobStream, null, options, null);

            blobStream.Position = 0;

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StreamContent(blobStream);
            response.Content.Headers.ContentDisposition          = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = imageId + Path.GetExtension(target);

            string eventName = thumb ? "DownloadThumb" : "DownloadImage";

            ServerUtils.LogTelemetryEvent(User.Identity.Name, eventName);

            return(response);
        }
예제 #3
0
        private void UpdateShares(ServiceData.Models.Photo photo)
        {
            List <ServiceData.Models.Share> shares = _shareRepository.Search(sh =>
                                                                             sh.UserCondition.Id == photo.UserCondition.Id).ToList();

            foreach (var sh in shares)
            {
                if (!sh.Updated)
                {
                    sh.Updated = true;
                    _shareRepository.Update(sh);
                }
            }
        }
예제 #4
0
        public async Task <ActionResult> Download(string imageId, bool thumb = false)
        {
            int id;

            if (string.IsNullOrEmpty(imageId) || !Int32.TryParse(imageId, out id))
            {
                return(new HttpUnauthorizedResult());
            }

            IReadWriteRepository <ServiceData.Models.Photo>         _photoRepository = new PhotoRepository();
            IReadWriteRepository <ServiceData.Models.UserCondition> _condRepository  = new UserConditionsRepository();

            ServiceData.Models.Photo found = _photoRepository.GetById(id);
            if (found == null)
            {
                return(new HttpNotFoundResult());
            }

            ServiceData.Models.UserCondition foundCond = _condRepository.GetById(found.UserCondition.Id);
            if (!IsSharedOrOwned(foundCond))
            {
                return(new HttpUnauthorizedResult());
            }

            string target = (thumb) ? found.ThumbUrl : found.Url;

            CloudBlobContainer container = await UploadController.GetBlobContainer();

            Stream    blobStream = new MemoryStream();
            CloudBlob photoBlob  = container.GetBlobReference(target.Replace(ConfidentialData.BlobStorageUrl, ""));

            KeyVaultKeyResolver cloudResolver = new KeyVaultKeyResolver(ServerUtils.GetToken);
            IKey rsa = await cloudResolver.ResolveKeyAsync(ConfidentialData.KeyLocation, CancellationToken.None);

            BlobEncryptionPolicy policy  = new BlobEncryptionPolicy(null, cloudResolver);
            BlobRequestOptions   options = new BlobRequestOptions()
            {
                EncryptionPolicy = policy
            };

            await photoBlob.DownloadToStreamAsync(blobStream, null, options, null);

            blobStream.Position = 0;

            return(File(blobStream, "image/jpeg"));
        }
예제 #5
0
        public async Task <HttpResponseMessage> Post([FromBody] Models.Photo newPhoto)
        {
            try
            {
                ServiceData.Models.Photo returned = _photoRepository.Insert(Models.Photo.ToServiceModel(newPhoto, true));
                UpdateShares(returned);

                ServerUtils.LogTelemetryEvent(User.Identity.Name, "AddPhoto");

                PostLog("Photos_Create");

                return(Request.CreateResponse(HttpStatusCode.OK, Models.Photo.ToAppModel(returned, false)));
            }
            catch (Exception e)
            {
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, e));
            }
        }
예제 #6
0
        public async Task <HttpResponseMessage> Delete(int id)
        {
            ServiceData.Models.Photo found = _photoRepository.GetById(id);

            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            if (!IsSameUser(found))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            await Delete(_photoRepository, id);

            ServerUtils.LogTelemetryEvent(User.Identity.Name, "DeletePhoto");
            PostLog("Photos_Delete");
            return(Request.CreateResponse(HttpStatusCode.OK));
        }
예제 #7
0
        public HttpResponseMessage Get(int id)
        {
            ServiceData.Models.Photo found = _photoRepository.GetById(id);

            if (found == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            if (!IsSameUser(found))
            {
                return(Request.CreateResponse(HttpStatusCode.Forbidden));
            }

            Models.Photo toRet = Models.Photo.ToAppModel(found, false);

            ServerUtils.LogTelemetryEvent(User.Identity.Name, "GetPhoto");
            PostLog("Photos_GetSingle");
            return(Request.CreateResponse(HttpStatusCode.OK, toRet));
        }
예제 #8
0
        public static Photo ToAppModel(ServiceData.Models.Photo given, bool includeCondition)
        {
            Photo cond = new Photo
            {
                Id               = given.Id,
                Url              = given.Url,
                ThumbUrl         = given.ThumbUrl,
                CreatedAt        = given.CreatedAt,
                Treatment        = given.Treatment,
                Notes            = given.Notes,
                PhotoDescription = given.PhotoDescription,
                Rating           = given.Rating
            };

            if (includeCondition && given.UserCondition != null)
            {
                cond.UserCondition = UserCondition.ToAppModel(given.UserCondition, true);
            }

            return(cond);
        }
예제 #9
0
        public static async Task Delete(IReadWriteRepository <ServiceData.Models.Photo> photoRep, int id)
        {
            ServiceData.Models.Photo found = photoRep.GetById(id);

            CloudBlobContainer container = await UploadController.GetBlobContainer();

            try
            {
                string url      = UploadController.GetFilePathFromUrl(found.Url);
                var    mainBlob = container.GetBlockBlobReference(url);
                mainBlob.Delete();
            }
            catch { }

            try
            {
                string thumbUrl  = UploadController.GetFilePathFromUrl(found.ThumbUrl);
                var    thumbBlob = container.GetBlockBlobReference(thumbUrl);
                thumbBlob.Delete();
            }
            catch { }

            await photoRep.Delete(id);
        }
예제 #10
0
        private bool IsSameUser(ServiceData.Models.Photo foundPhoto)
        {
            ServiceData.Models.UserCondition foundCond = _conditionRepository.GetById(foundPhoto.UserCondition.Id);

            return(foundCond.Owner.Email == User.Identity.Name);
        }