Пример #1
0
        private void Process(string path, Options options)
        {
            var files = FileService.FindPictureFiles(path);

            if (!files.Any())
            {
                Logger.Warning("Could not locate any files to upload in the directory: {0}.", path);

                return;
            }


            Logger.Debug("Processing files in the directory: {0}.", path);

            var photosetName = Path.GetFileName(path.TrimEnd('\\'));

            var    photoset        = Uploader.FindPhotosetByName(photosetName);
            var    photosetExists  = photoset != null && photoset.Title == photosetName;
            var    photosetChanged = false;
            string photosetId      = null;

            var photosetPhotos = photosetExists
              ? Uploader.GetPhotosetPictures(photoset.PhotosetId)
              : new List <Photo>();

            if (photosetExists)
            {
                photosetId = photoset.PhotosetId;

                var totalFilesInDirectory = files.Count;
                files.RemoveAll(x => photosetPhotos.Any(p => p.Title == Path.GetFileNameWithoutExtension(x)));
                Logger.Info("{0} out of {1} files are already in the existing photoset.", totalFilesInDirectory - files.Count, totalFilesInDirectory);
            }

            // Check again as the collection might have been modified
            if (!files.Any())
            {
                Logger.Warning("All photos are already in the photoset. Nothing to upload.");
            }
            else
            {
                photosetChanged = true;

                #region Upload photos

                var photoIds = new List <string>();

                Console.WriteLine();
                Logger.Info("Uploading files...");

                //todo: move to FlickrService
                var failures = FlickrService.ParallelExecute(files, fileName =>
                {
                    var title = Path.GetFileNameWithoutExtension(fileName);

                    var photo = photosetPhotos.FirstOrDefault(x => x.Title == title); //?? Uploader.FindPictureByName(title);
                    if (photo == null || photo.Title != title)
                    {
                        var photoId = Uploader.UploadPicture(fileName, title, null, null);
                        if (photoIds.Contains(photoId))
                        {
                            throw new Exception($"{title} is already in the list of uploaded files.");
                        }

                        photoIds.Add(photoId);
                    }
                }, Settings.BatchSizeForParallelUpload);

                if (failures.Any())
                {
                    Logger.Error("Uploaded with errors:");

                    foreach (var failure in failures)
                    {
                        Logger.Error("{0,-20}: {1}", failure.Key, failure.Value);
                    }
                }
                else
                {
                    Logger.Info("All files were successfully uploaded.");
                }

                #endregion

                if (!photoIds.Any())
                {
                    Logger.Warning("No files were uploaded to '{0}'.", photosetName);
                }

                else if (!photosetExists)
                {
                    Console.WriteLine();
                    Logger.Info("Creating photoset '{0}'...", photosetName);

                    var coverPhotoId = photoIds.First();
                    photoIds.Remove(coverPhotoId);

                    photoset   = Uploader.CreatePhotoSet(photosetName, coverPhotoId);
                    photosetId = photoset.PhotosetId;

                    Logger.Info("Photoset created.");

                    photosetExists = true;
                }

                AddPhotosToPhotoset(photosetName, photosetId, photoIds);
            }

            bool updatePermissions = options.ShareWithFamily || options.ShareWithFriends;

            if (photosetExists && (photosetChanged || updatePermissions))
            {
                photosetPhotos = Uploader.GetPhotosetPictures(photosetId);

                FileService.ValidateDirectory(options.Path, photosetPhotos);

                if (photosetPhotos.Count > 1)
                {
                    if (photosetChanged)
                    {
                        FlickrService.SortPhotosInSet(photosetPhotos);
                    }

                    if (updatePermissions)
                    {
                        FlickrService.SetPermissions(photosetPhotos, options.ShareWithFamily, options.ShareWithFriends);
                    }
                }
            }
        }