Exemple #1
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());
        }