Ejemplo n.º 1
0
        /// <summary>
        /// <para>
        /// Performs the Photo Method:
        /// Upload a photo.
        /// </para><para>
        /// Serializes the given PhotoUploadRequest into xml and sends the message.
        /// Loads the file and converts it to the appropriate data format for the request - it does not require the "PhotoData" field of the PhotoUploadRequest object to have anything in it.
        ///  </para>
        /// </summary>
        /// <param name="up">The object that will be serialized into xml and then sent in a POST message.</param>
        /// <returns>XDocument.</returns>
        public XDocument UploadPhotoFormat(PhotoUploadRequest up)
        {
            var fileName = up.FileName;

            var fs   = File.OpenRead(fileName);
            var data = new byte[fs.Length];

            // read in the file to a byte array
            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var offset    = 0;
                var remaining = data.Length;
                while (remaining > 0)
                {
                    var read = stream.Read(data, offset, remaining);
                    if (read <= 0)
                    {
                        throw new EndOfStreamException(String.Format(Constants.Culture, "End of stream reached with {0} bytes left to read", remaining));
                    }

                    remaining -= read;
                    offset    += read;
                }
            }

            // The data in the request is a base64 encoded string of the binary data in the photo.
            var endData = Convert.ToBase64String(data);

            // put the data in the object that will be converted to xml and posted
            up.PhotoData = endData;

            // send the post method
            return(_connection.Post(up, "Photos.xml"));
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="photoids">tradelr photo id</param>
        /// <returns>trademe photo id</returns>
        private IEnumerable <int> UploadPhotos(IEnumerable <string> photoids)
        {
            var       ids     = photoids.Select(long.Parse);
            const int timeout = 10000; // 10 seconds
            var       cts     = new CancellationTokenSource();
            var       tasks   = new List <Task>();

            var results = new List <int>();

            var photoservice = new PhotoService(key, secret);

            using (var t = new Timer(_ => cts.Cancel(), null, timeout, -1))
            {
                foreach (var photoid in ids)
                {
                    var task = Task.Factory.StartNew(() =>
                    {
                        using (var repository = new TradelrRepository())
                        {
                            var image = repository.GetProductImage(photoid);
                            if (image != null)
                            {
                                if (image.trademephotoid.HasValue)
                                {
                                    results.Add(image.trademephotoid.Value);
                                }
                                else
                                {
                                    // we need to upload photo
                                    var photoreq = new PhotoUploadRequest
                                    {
                                        FileName        = Path.GetFileName(image.url),
                                        FileType        = Path.GetExtension(image.url).Substring(1),
                                        IsWaterMarked   = true,
                                        IsUsernameAdded = true,
                                        PhotoData       = Img.ConvertToBase64String(GeneralConstants.APP_ROOT_DIR + image.url)
                                    };

                                    photoreq.GenerateSignature();

                                    var resp = photoservice.AddPhoto(new AddPhotoRequest(photoreq));
                                    if (resp.AddPhotoResult.Status == PhotoStatus.Success)
                                    {
                                        results.Add(resp.AddPhotoResult.PhotoId);
                                        image.trademephotoid = resp.AddPhotoResult.PhotoId;
                                        repository.Save();
                                    }
                                }
                            }
                        }
                    }, cts.Token);
                    tasks.Add(task);
                }
            }
            Task.WaitAll(tasks.ToArray());

            return(results.ToArray());
        }
Ejemplo n.º 3
0
        public ActionResult <Response> Upload(PhotoUploadRequest request)
        {
            try
            {
                //Build the photo object
                Photo uploadedPhoto = new Photo(request.latitude, request.longitude, request.imgUrl, request.takenByID, request.photoOfID);

                //Get the player object from the database
                Response <Player> getPlayerResponse = new PlayerDAL().GetPlayerByID(uploadedPhoto.TakenByPlayerID);
                if (!getPlayerResponse.IsSuccessful())
                {
                    return(new Response(getPlayerResponse.ErrorMessage, getPlayerResponse.ErrorCode));
                }

                Response <Photo> response;

                //Call the BR Upload business logic if the player is a BR player
                if (getPlayerResponse.Data.IsBRPlayer())
                {
                    response = new PhotoDAL().SavePhoto(uploadedPhoto, true);
                }

                //Otherwise, call the CORE business logic
                else
                {
                    response = new PhotoDAL().SavePhoto(uploadedPhoto, false);
                }

                //If the response is successful we want to send live updates to clients and
                //email or text message notifications to not connected players
                if (response.IsSuccessful())
                {
                    HubInterface hubInterface = new HubInterface(_hubContext);
                    hubInterface.UpdatePhotoUploaded(response.Data);
                    ScheduledTasks.ScheduleCheckPhotoVotingCompleted(response.Data, hubInterface);
                }
                return(new Response(response.ErrorMessage, response.ErrorCode));
            }
            //Catch any error associated with invalid model data
            catch (InvalidModelException e)
            {
                return(new Response(e.Msg, e.Code));
            }
            //Catch any unhandled / unexpected server errrors
            catch
            {
                return(StatusCode(500));
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// <para>
        /// Performs the Photo Method:
        /// Upload a photo.
        /// </para><para>
        /// Serializes the given PhotoUploadRequest into xml and sends the message.
        /// Loads the file and converts it to the appropriate data format for the request - it does not require the "PhotoData" field of the PhotoUploadRequest object to have anything in it.
        ///  </para>
        /// </summary>
        /// <param name="up">The object that will be serialized into xml and then sent in a POST message.</param>
        /// <returns>XDocument.</returns>
        public XDocument UploadPhotoFormat(PhotoUploadRequest up)
        {
            var fileName = up.FileName;

            var fs = File.OpenRead(fileName);
            var data = new byte[fs.Length];

            // read in the file to a byte array
            using (Stream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                var offset = 0;
                var remaining = data.Length;
                while (remaining > 0)
                {
                    var read = stream.Read(data, offset, remaining);
                    if (read <= 0)
                    {
                        throw new EndOfStreamException(String.Format(Constants.Culture, "End of stream reached with {0} bytes left to read", remaining));
                    }

                    remaining -= read;
                    offset += read;
                }
            }

            // The data in the request is a base64 encoded string of the binary data in the photo.
            var endData = Convert.ToBase64String(data);

            // put the data in the object that will be converted to xml and posted
            up.PhotoData = endData;

            // send the post method
            return _connection.Post(up, "Photos.xml");
        }
Ejemplo n.º 5
0
 /// <summary>
 /// <para>Performs the Photo Method:
 /// Upload a photo.
 /// </para>
 /// Serializes the given PhotoUploadRequest into xml and sends the message.
 /// </summary>
 /// <param name="up">The object that will be serialized into xml and then sent in a POST message.</param>
 /// <returns>XDocument.</returns>
 public XDocument UploadPhoto(PhotoUploadRequest up)
 {
     // send the post method
     return _connection.Post(up, "Photos.xml");
 }
Ejemplo n.º 6
0
        /// <summary>
        /// <para>
        /// Performs the Photo Method:
        /// Upload a photo.
        /// </para><para>
        /// Serializes the given PhotoUploadRequest into xml and sends the message.
        /// Loads the file and converts it to the appropriate data format for the request - it does not require the "PhotoData" field of the PhotoUploadRequest object to have anything in it.
        ///  </para>
        /// </summary>
        /// <param name="up">The object that will be serialized into xml and then sent in a POST message.</param>
        /// <returns>XDocument.</returns>
        public XDocument UploadPhotoFormat(PhotoUploadRequest up)
        {
            if (_photo == null)
            {
                _photo = new PhotoMethods(_connection);
            }

            return _photo.UploadPhotoFormat(up);
        }
Ejemplo n.º 7
0
 public IActionResult Upload(PhotoUploadRequest request)
 {
     return new JsonResult(FileManager.GetInstance(_dbContext).Save(request));
 }
Ejemplo n.º 8
0
 /// <summary>
 /// <para>Performs the Photo Method:
 /// Upload a photo.
 /// </para>
 /// Serializes the given PhotoUploadRequest into xml and sends the message.
 /// </summary>
 /// <param name="up">The object that will be serialized into xml and then sent in a POST message.</param>
 /// <returns>XDocument.</returns>
 public XDocument UploadPhoto(PhotoUploadRequest up)
 {
     // send the post method
     return(_connection.Post(up, "Photos.xml"));
 }