Example #1
0
        private async Task <UploadFileResult> UploadFileAsync(File4Flickr file4Flickr)
        {
            var result = await Task.Run(() =>
            {
                var uploadFileResult = UploadFile(file4Flickr);
                return(uploadFileResult);
            });

            return(result);
        }
Example #2
0
        /// <summary>
        /// zakladni upload Photo na flickr s podporou retry pokusu (flickr je zrejme pretizeny a obcas haze service unavailable atd)
        /// </summary>
        public UploadFileResult UploadFile(File4Flickr file4Flickr, int maxRetryAttemps = 10)
        {
            var    startTime   = DateTime.Now;
            string photoId     = null;
            var    retryAttemp = 0;

            while (string.IsNullOrEmpty(photoId))
            {
                try
                {
                    photoId = UploadPicture(file4Flickr);

                    // nechapu co to ma znamenat, ale jednou se ted stalo, ze upload probehl v poradku, avsak photoId bylo prazdne?? a na flickru fotky skutecne nejsou
                    // vyvolam tedy vyjimku, aby probehlo uploadovani znovu
                    if (string.IsNullOrEmpty(photoId))
                    {
                        throw new FlickrExtenderException("PhotoId is empty!");
                    }
                }
                catch (Exception ex) when(ex is WebException || ex is IOException || ex is FlickrExtenderException)
                {
                    // v pripade nekterych http protocol erroru zkusim retry - mozna by bylo vhodne to zkouset pri kazde chybe?
                    if (retryAttemp++ < maxRetryAttemps)
                    {
                        if (ex is WebException)
                        {
                            Logger.Write(string.Format("WebException message:{0} status:{1}", ex.Message, (ex as WebException).Status), VtLogState.Error);
                        }
                        else
                        {
                            Logger.Write(string.Format("{0} error:{1}", ex.GetType().Name, ex.Message), VtLogState.Error);
                        }
                        Logger.Write(string.Format("Retry #:{0}", retryAttemp), VtLogState.Info);
                    }
                    else
                    {
                        throw;
                    }
                }
                catch (FlickrApiException flickrApiException)
                {
                    // doslo k nejake flickr API chybe (treba 5: Filetype was not recognised), takze nezkousim retry a skoncim upload s chybou
                    var result = new UploadFileResult(file4Flickr, startTime, retryAttemp, flickrApiException);
                    return(result);
                }
            }
            return(new UploadFileResult(file4Flickr, startTime, retryAttemp, photoId));
        }
Example #3
0
 private string UploadPicture(File4Flickr file4Flickr)
 {
     using (var filestream = new FileStream(file4Flickr.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
     {
         //Arguments https://www.flickr.com/services/api/upload.api.html
         //photo
         //    The file to upload.
         //title(optional)
         //    The title of the photo.
         //description(optional)
         //    A description of the photo.May contain some limited HTML.
         //tags(optional)
         //    A space-seperated list of tags to apply to the photo.
         //is_public, is_friend, is_family(optional)
         //    Set to 0 for no, 1 for yes.Specifies who can view the photo.
         //safety_level(optional)
         //    Set to 1 for Safe, 2 for Moderate, or 3 for Restricted.
         //content_type(optional)
         //    Set to 1 for Photo, 2 for Screenshot, or 3 for Other.
         //hidden(optional)
         //    Set to 1 to keep the photo in global search results, 2 to hide from public searches.
         var photoId = UploadPicture(filestream,
                                     Path.GetFileName(file4Flickr.FullName),
                                     null,
                                     null,
                                     file4Flickr.FlupTags,
                                     false,
                                     false,
                                     false,
                                     ContentType.None,
                                     SafetyLevel.Restricted,
                                     HiddenFromSearch.Hidden);
         filestream.Close();
         return(photoId);
     }
 }
Example #4
0
 public UploadFileResult(File4Flickr file4Flickr, DateTime startTime, int retryAttemp, FlickrApiException flickrApiException) : this(file4Flickr, startTime, retryAttemp)
 {
     PhotoId            = null;
     FlickrApiException = flickrApiException;
 }
Example #5
0
 public UploadFileResult(File4Flickr file4Flickr, DateTime startTime, int retryAttemp, string photoId) : this(file4Flickr, startTime, retryAttemp)
 {
     PhotoId = photoId;
 }
Example #6
0
 public UploadFileResult(File4Flickr file4Flickr, DateTime startTime, int retryAttemp) : base(startTime)
 {
     File4Flickr = file4Flickr;
     RetryAttemp = retryAttemp;
 }