Пример #1
0
        /// <summary>
        /// Converts the given contract into a data model.
        /// </summary>
        /// <param name="photoContract">The data contract.</param>
        /// <returns>The data model.</returns>
        public static Photo ToDataModel(this PhotoContract photoContract)
        {
            var photo = new Photo
            {
                Id                  = photoContract.Id,
                CategoryId          = photoContract.CategoryId,
                ThumbnailUrl        = photoContract.ThumbnailUrl,
                StandardUrl         = photoContract.StandardUrl,
                HighResolutionUrl   = photoContract.HighResolutionUrl,
                CroppedUrl          = photoContract.CroppedUrl,
                User                = photoContract.User.ToDataModel(),
                Caption             = photoContract.Description,
                CreatedAt           = photoContract.CreatedAt,
                NumberOfAnnotations = photoContract.NumberOfAnnotations,
                GoldCount           = photoContract.NumberOfGoldVotes,
                CategoryName        = photoContract.CategoryName,
                Reports             = photoContract.Reports,
                Status              = photoContract.Status,
                Keywords            = photoContract.Keywords
            };

            if (photoContract.Annotations != null)
            {
                photo.Annotations = new ObservableCollection <Annotation>(
                    photoContract.Annotations.Select(a => a.ToDataModel()));
            }

            return(photo);
        }
Пример #2
0
 /// <summary>
 /// Creates a <see cref="PhotoDocument" /> from a <see cref="PhotoContract" />.
 /// </summary>
 /// <returns>The <see cref="PhotoDocument" />.</returns>
 public static PhotoDocument CreateFromContract(PhotoContract contract)
 {
     return(new PhotoDocument
     {
         Id = contract.Id,
         CategoryId = contract.CategoryId,
         CategoryName = contract.CategoryName,
         UserId = contract.User.UserId,
         ThumbnailUrl = contract.ThumbnailUrl,
         StandardUrl = contract.StandardUrl,
         HighResolutionUrl = contract.HighResolutionUrl,
         Description = contract.Description,
         CreatedDateTime = new DateDocument
         {
             Date = contract.CreatedAt
         },
         ModifiedAt = new DateDocument
         {
             Date = contract.CreatedAt
         },
         Annotations =
             contract.Annotations?.Select(AnnotationDocument.CreateFromContract).ToList() ??
             new List <AnnotationDocument>(),
         GoldCount = contract.NumberOfGoldVotes,
         OSPlatform = contract.OSPlatform,
         Status = contract.Status
     });
 }
Пример #3
0
        public async Task <PhotoContract> PutAsync(PhotoContract photo)
        {
            try
            {
                _telemetryClient.TrackEvent("Auth: PhotoController PutAsync invoked");

                var registrationReference = await ValidateAndReturnCurrentUserId();

                if (!await _photoValidation.IsUserPhotoOwner(registrationReference, photo.Id))
                {
                    throw ServiceExceptions.NotAllowed();
                }

                return(await _repository.UpdatePhoto(photo));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #4
0
        /// <summary>
        /// Uploads the photo.
        /// </summary>
        /// <param name="stream">The memory stream.</param>
        /// <param name="localPath">The local path.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="categoryId">The id of the assocaited category.</param>
        /// <returns>The uploaded photo.</returns>
        public async Task <Photo> UploadPhoto(Stream stream, string localPath, string caption, string categoryId)
        {
            try
            {
                var sasContracts = await GetSasUrls();

                byte[] myBinary = new byte[stream.Length];
                stream.Read(myBinary, 0, (int)stream.Length);

                // Upload photos into azure blob storage
                foreach (var sasContract in sasContracts)
                {
                    var blob         = new CloudBlockBlob(new Uri($"{sasContract.FullBlobUri}{sasContract.SasToken}"));
                    var sideLength   = (int)sasContract.SasPhotoType.ToSideLength();
                    var resizedPhoto = _photoCroppingService.ResizeImage(myBinary, sideLength, sideLength);
                    await blob.UploadFromByteArrayAsync(resizedPhoto, 0, resizedPhoto.Length);
                }

                var photoContract = new PhotoContract
                {
                    CategoryId  = categoryId,
                    Description = caption,
                    //OSPlatform = AnalyticsInfo.VersionInfo.DeviceFamily,
                    OSPlatform   = "Xamarin",
                    User         = AppEnvironment.Instance.CurrentUser.ToDataContract(),
                    ThumbnailUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Thumbnail)?
                        .FullBlobUri.ToString(),
                    StandardUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Standard)?
                        .FullBlobUri.ToString(),
                    HighResolutionUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.HighRes)?
                        .FullBlobUri.ToString()
                };

                var responsePhotoContract =
                    await _mobileServiceClient.InvokeApiAsync <PhotoContract, PhotoContract>("Photo",
                                                                                             photoContract,
                                                                                             HttpMethod.Post,
                                                                                             null);

                // Call the Cognitive Vision API to generate tags for the image
                // TODO: Shift this call to the backend and let it gets triggered automatically with functions
                var httpClient = new HttpClient();
                var response   = await httpClient.GetAsync($"https://snapgolddemofunctions.azurewebsites.net/api/generatekeywords/{responsePhotoContract.Id}");

                if (!response.IsSuccessStatusCode)
                {
                    // Something went wrong...
                }

                return(responsePhotoContract.ToDataModel());
            }
            catch (Exception e)
            {
                throw new ServiceException("UploadPhoto error", e);
            }
        }
        /// <summary>
        /// Uploads the photo.
        /// </summary>
        /// <param name="stream">The memory stream.</param>
        /// <param name="localPath">The local path.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="categoryId">The id of the assocaited category.</param>
        /// <returns>The uploaded photo.</returns>
        public async Task <Photo> UploadPhoto(Stream stream, string localPath, string caption, string categoryId)
        {
            try
            {
                var sasContracts = await GetSasUrls();

                // Upload photos into azure
                foreach (var sasContract in sasContracts)
                {
                    var blob =
                        new CloudBlockBlob(
                            new Uri($"{sasContract.FullBlobUri}{sasContract.SasToken}"));

                    var sideLength = sasContract.SasPhotoType.ToSideLength();

                    var resizedStream = await BitmapTools.Resize(
                        stream.AsRandomAccessStream(), sideLength,
                        sideLength);

                    await blob.UploadFromStreamAsync(resizedStream);
                }

                var photoContract = new PhotoContract
                {
                    CategoryId   = categoryId,
                    Description  = caption,
                    OSPlatform   = AnalyticsInfo.VersionInfo.DeviceFamily,
                    User         = AppEnvironment.Instance.CurrentUser.ToDataContract(),
                    ThumbnailUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Thumbnail)?
                        .FullBlobUri.ToString(),
                    StandardUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.Standard)?
                        .FullBlobUri.ToString(),
                    HighResolutionUrl =
                        sasContracts.FirstOrDefault(c => c.SasPhotoType == PhotoTypeContract.HighRes)?
                        .FullBlobUri.ToString()
                };

                var responsePhotoContract =
                    await _mobileServiceClient.InvokeApiAsync <PhotoContract, PhotoContract>("Photo",
                                                                                             photoContract,
                                                                                             HttpMethod.Post,
                                                                                             null);

                return(responsePhotoContract.ToDataModel());
            }
            catch (Exception e)
            {
                _telemetryClient.TrackException(e);
                throw new ServiceException("UploadPhoto error", e);
            }
        }
Пример #6
0
        /// <summary>
        /// Converts the given data model into a photo data contract.
        /// </summary>
        /// <param name="photo">The data model.</param>
        /// <returns>The photo data contract.</returns>
        public static PhotoContract ToDataContract(this Photo photo)
        {
            var photoContract = new PhotoContract
            {
                Id                  = photo.Id,
                CategoryId          = photo.CategoryId,
                CategoryName        = photo.CategoryName,
                ThumbnailUrl        = photo.ThumbnailUrl,
                StandardUrl         = photo.StandardUrl,
                HighResolutionUrl   = photo.HighResolutionUrl,
                User                = photo.User.ToDataContract(),
                Description         = photo.Caption,
                CreatedAt           = photo.CreatedAt,
                NumberOfAnnotations = photo.NumberOfAnnotations,
                NumberOfGoldVotes   = photo.GoldCount,
                Status              = photo.Status
            };

            return(photoContract);
        }
Пример #7
0
        public async Task <PhotoContract> PostAsync(PhotoContract photo)
        {
            try
            {
                _telemetryClient.TrackEvent("PhotoController PostAsync invoked");
                await ValidateAndReturnCurrentUserId();

                int goldIncrement;
                int.TryParse(WebConfigurationManager.AppSettings["NewPhotoAward"], out goldIncrement);

                return(await _repository.InsertPhoto(photo, goldIncrement));
            }
            catch (DataLayerException ex)
            {
                _telemetryClient.TrackException(ex);

                if (ex.Error == DataLayerError.Unknown)
                {
                    throw ServiceExceptions.UnknownInternalFailureException(ServiceExceptions.Source);
                }

                throw ServiceExceptions.DataLayerException(ex.Message);
            }
        }
Пример #8
0
 public Task <PhotoContract> UpdatePhotoStatus(PhotoContract photoContract)
 {
     throw new NotImplementedException();
 }
Пример #9
0
 public Task <PhotoContract> InsertPhoto(PhotoContract photo, int goldIncrement)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Update stored photo object.
 /// </summary>
 /// <param name="photo">Photo Object.</param>
 /// <returns>New PhotoContract containing updated data.</returns>
 public Task <PhotoContract> UpdatePhoto(PhotoContract photo)
 {
     return(_repository.UpdatePhoto(photo));
 }
 /// <summary>
 /// Insert the photo object into storage.
 /// </summary>
 /// <param name="photo">Photo Object.</param>
 /// <param name="goldIncrement">Gold to award for new photo.</param>
 /// <returns>New PhotoContract with updated user balance.</returns>
 public Task <PhotoContract> InsertPhoto(PhotoContract photo, int goldIncrement)
 {
     return(_repository.InsertPhoto(photo, goldIncrement));
 }
Пример #12
0
 /// <summary>
 /// Updates the status of the stored photo.
 /// </summary>
 /// <param name="photoContract">Photo object.</param>
 /// <returns>PhotoContract containing updated data.</returns>
 public Task <PhotoContract> UpdatePhotoStatus(PhotoContract photoContract)
 {
     return(_repository.UpdatePhotoStatus(photoContract));
 }