public async void PhotoUpdated(int position, PhotoUploadStatus newStatus = PhotoUploadStatus.Pending)
        {
            CustomerPhotoController controller = new CustomerPhotoController();
            CustomerPhoto           photo      = this.CustomerPhotos[position];

            photo.PhotoUploadStatus = newStatus;

            await controller.SaveAsync(photo);

            CustomerPhoto backUpPhoto = new CustomerPhoto
            {
                CustomerIdentifier = this.CustomerPhotos[position].CustomerIdentifier,
                FilePath           = this.CustomerPhotos[position].FilePath,
                Phone             = this.CustomerPhotos[position].Phone,
                PhotoStatus       = this.CustomerPhotos[position].PhotoStatus,
                PhotoUploadStatus = this.CustomerPhotos[position].PhotoUploadStatus,
                TypeOfPhoto       = this.CustomerPhotos[position].TypeOfPhoto,
                Id       = this.CustomerPhotos[position].Id,
                Created  = this.CustomerPhotos[position].Created,
                Modified = this.CustomerPhotos[position].Modified
            };

            this.CustomerPhotos.RemoveAt(position);
            this.CustomerPhotos.Insert(position, backUpPhoto);

            this.RaisePropertyChanged(() => this.CustomerPhotos);
        }
Beispiel #2
0
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            PhotoUploadStatus status = (PhotoUploadStatus)value;

            switch (status)
            {
            case PhotoUploadStatus.Completing:
                return("../Assets/Images/completing-16.png");

            case PhotoUploadStatus.Succeeded:
                return("../Assets/Images/success-16.png");

            case PhotoUploadStatus.Canceled:
                return("../Assets/Images/cancel-16.png");

            case PhotoUploadStatus.Error:
                return("../Assets/Images/error-16.png");

            default:
                return(DependencyProperty.UnsetValue);
            }
        }
        private async Task <Dictionary <Guid, PhotoUploadStatus> > Upload(List <CustomerPhoto> photos)
        {
            var               photoUploadStatusDict = new Dictionary <Guid, PhotoUploadStatus>();
            CustomerPhoto     currentPhotoForUpload = null;
            PhotoUploadStatus photoUploadStatus     = PhotoUploadStatus.Pending;

            try
            {
                if (_connectivityService.HasConnection() && photos.Any())
                {
                    Logger.Verbose("Total number of Photos available " + photos.Count);

                    // loop through existing customer photos, if any
                    foreach (var photo in photos)
                    {
                        currentPhotoForUpload = photo;
                        photoUploadStatus     = photo.PhotoUploadStatus;

                        var photoName = Path.GetFileName(currentPhotoForUpload.FilePath);
                        Logger.Verbose("About to process Photo : " + photoName);

                        Logger.Verbose("DSR Country Code");
                        Logger.Verbose(Settings.Instance.DsrCountryCode.ToString().ToLower());

                        // build up a blob name that includes the phone and national id number of the customer as well as country prefix
                        var blobName  = currentPhotoForUpload.Phone + "_" + currentPhotoForUpload.CustomerIdentifier + "_" + Settings.Instance.DsrCountryCode.ToString().ToLower() + "_" + Path.GetFileName(photoName);
                        var blockBlob = _photoBlobContainer.GetBlockBlobReference(Path.GetFileName(blobName));

                        // set the metadata of the blob to have customer's national id, type and description of photo
                        if (!string.IsNullOrEmpty(currentPhotoForUpload.CustomerIdentifier))
                        {
                            blockBlob.Metadata["NationalID"] = currentPhotoForUpload.CustomerIdentifier;
                        }

                        if (!string.IsNullOrEmpty(currentPhotoForUpload.Phone))
                        {
                            blockBlob.Metadata["PhoneNumber"] = currentPhotoForUpload.Phone;
                        }

                        if (currentPhotoForUpload.TypeOfPhoto > 0)
                        {
                            blockBlob.Metadata["PhotoType"] = currentPhotoForUpload.TypeOfPhoto.ToString();
                        }

                        // upload each photo to Azure Blob Storage
                        if (File.Exists(currentPhotoForUpload.FilePath))
                        {
                            using (var fileStream = File.OpenRead(currentPhotoForUpload.FilePath))
                            {
                                await blockBlob.UploadFromStreamAsync(fileStream);

                                Logger.Verbose("Photo uploaded to Azure Blob Storage : " + blobName);
                            }
                        }

                        // set the upload status to success
                        photoUploadStatus = PhotoUploadStatus.Successfull;

                        // save the photo's status to the database
                        currentPhotoForUpload.PhotoUploadStatus = photoUploadStatus;
                        await _photoController.SaveAsync(currentPhotoForUpload);

                        // update the photo status dictionary
                        photoUploadStatusDict.Add(currentPhotoForUpload.Id, currentPhotoForUpload.PhotoUploadStatus);

                        Logger.Verbose("Updated Customer Photo Status to Success");
                    }
                }
            }
            catch (StorageException se)
            {
                Logger.Verbose("StorgeException Caught On Photo Upload");

                // set the upload status to Fail
                photoUploadStatus = PhotoUploadStatus.Failed;

                var requestInformation = se.RequestInformation;
                var errorCode          = requestInformation.ExtendedErrorInformation.ErrorCode;
                var statusCode         = (HttpStatusCode)requestInformation.HttpStatusCode;

                Logger.Verbose(errorCode);
                Logger.Verbose(statusCode.ToString());

                // if it is an authentication failure then it means the SAS Token has probably expired and a new one is needed
                if (errorCode == StorageErrorCodeStrings.AuthenticationFailed)
                {
                    // SAS Token probably has expired
                    Logger.Verbose("SAS Token Expired");

                    // TODO Add code to request new SAS Token from OTA Settings or API Call
                }
            }
            catch (Exception e)
            {
                Logger.Verbose("General Exception Caught On Photo Upload");
                Logger.Verbose(e.Message);

                // set the upload status to Fail
                photoUploadStatus = PhotoUploadStatus.Failed;

                Logger.Error(e);
            }

            try
            {
                // if the upload failed, update the database accordingly, but from another try/catch block
                if (photoUploadStatus == PhotoUploadStatus.Failed && currentPhotoForUpload != null)
                {
                    currentPhotoForUpload.PhotoUploadStatus = photoUploadStatus;
                    await _photoController.SaveAsync(currentPhotoForUpload);

                    // update the photo status dictionary
                    photoUploadStatusDict.Add(currentPhotoForUpload.Id, currentPhotoForUpload.PhotoUploadStatus);

                    Logger.Verbose("Updated Customer Photo Status to Failed");
                }
            }
            catch (Exception dbe)
            {
                Logger.Verbose("General Exception Caught In CustomerPhoto Table Updated on Failed Upload");
                Logger.Verbose(dbe);
                Logger.Error(dbe);
            }

            return(photoUploadStatusDict);
        }