コード例 #1
0
 /// <summary>
 /// Edits properties of the specified Photosynth collection. Can be used on newly created Photosynth collections
 /// that are not yet available for viewing, or existing Photosynth collections.
 /// </summary>
 /// <param name="id">The ID of the collection to edit.</param>
 /// <param name="editMediaRequest">The metadata to set.</param>
 /// <returns>A task representing the asynchronous operation.</returns>
 public async Task EditMediaAsync(Guid id, EditMediaRequest editMediaRequest)
 {
     await this.MakeRequestAsync <bool>("/media/" + id, HttpMethod.Post, editMediaRequest);
 }
コード例 #2
0
        /// <summary>
        /// Asynchronously uploads a synth packet to the Photosynth upload service for processing in the cloud.
        /// </summary>
        /// <param name="webApi">A WebApi object for handling HTTP communication with the Photosynth web service</param>
        /// <param name="fileStorage">An IFileStorage implementation for handling file I/O</param>
        /// <param name="synthPacketUpload">Metadata for the synth packet to be uploaded.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <param name="parallelism">The maximum number of chunks that can be uploaded in parallel.</param>
        /// <returns>The task object representing the asynchronous operation</returns>
        public async Task UploadSynthPacketAsync(
            WebApi webApi, IFileStorage fileStorage, SynthPacketUpload synthPacketUpload, CancellationToken cancellationToken, int parallelism = 2)
        {
            if (this.IsUploading)
            {
                throw new InvalidOperationException("Only one upload at a time per Uploader instance is allowed.");
            }

            if (string.IsNullOrWhiteSpace(synthPacketUpload.Title))
            {
                throw new ArgumentException("Must provide a Title for the SynthPacketUpload.");
            }

            try
            {
                this.IsUploading            = true;
                this.Id                     = Guid.Empty;
                this.ProgressPercentage     = 0;
                this.EstimatedTimeRemaining = string.Empty;
                this.FailedChunks.Clear();

                // Create a new Photosynth collection
                this.Status = "Creating collection...";
                var createCollectionRequest = new CreateMediaRequest()
                {
                    UploadType = UploadType.SynthPacketFromRawImages
                };
                var createCollectionResponse = await webApi.CreateMediaAsync(createCollectionRequest);

                this.ProgressPercentage = 1;

                this.Id = createCollectionResponse.Id;

                // Add images to the collection and get upload URLs for each file
                this.Status = "Adding images to collection...";
                var addFilesRequest = new AddFilesRequest();
                for (int i = 0; i < synthPacketUpload.PhotoPaths.Count; i++)
                {
                    addFilesRequest.Files.Add(new AddFileRequest()
                    {
                        Id         = i.ToString(),
                        Extension  = "jpg",
                        Order      = i.ToString("000"),
                        ChunkCount = 1,
                    });
                }

                var addFilesResponse = await webApi.AddFilesAsync(this.Id, addFilesRequest);

                this.ProgressPercentage = 2;

                // Get info about all the files that need uploading
                List <ChunkInfo> chunks = addFilesRequest.Files.Join(
                    addFilesResponse.Files,
                    req => req.Id,
                    resp => resp.ClientId,
                    (req, resp) =>
                {
                    string photoPath = synthPacketUpload.PhotoPaths[Int32.Parse(resp.ClientId)];
                    FileChunk chunk  = resp.Chunks.First();
                    return(new ChunkInfo(chunk.UploadUri, photoPath, fileStorage.GetFileSize(photoPath)));
                }).ToList();

                // Commit the collection, no more files can be added
                this.Status = "Setting properties for the collection...";
                var editMediaRequest = new EditMediaRequest()
                {
                    Name         = synthPacketUpload.Title,
                    Description  = synthPacketUpload.Description,
                    ImageCount   = synthPacketUpload.PhotoPaths.Count,
                    PrivacyLevel = synthPacketUpload.PrivacyLevel,
                    CapturedDate = synthPacketUpload.CapturedDate,
                    Committed    = true,
                    SynthPacket  = new SynthPacket()
                    {
                        License = synthPacketUpload.License,
                    },
                    UploadHints = synthPacketUpload.Topology.ToString()
                                  // TODO geotag
                };
                if (synthPacketUpload.Tags.Any())
                {
                    editMediaRequest.Tags = string.Join(",", synthPacketUpload.Tags);
                }

                await webApi.EditMediaAsync(this.Id, editMediaRequest);

                this.ProgressPercentage = 3;

                var failedChunks = await this.UploadChunksAsync(webApi, fileStorage, chunks, 3, cancellationToken, parallelism);

                foreach (var failedChunk in failedChunks)
                {
                    this.FailedChunks.Add(failedChunk);
                }

                this.ProgressPercentage = 100;
                if (this.FailedChunks.Any())
                {
                    this.Status = string.Format(CultureInfo.CurrentUICulture, "{0} files failed to upload.", this.FailedChunks.Count);
                }
                else
                {
                    this.Status = string.Format(CultureInfo.CurrentUICulture, "{0} files successfully uploaded.", chunks.Count);
                }
            }
            catch (Exception e)
            {
                this.Status = string.Format(CultureInfo.CurrentUICulture, "Error: {0}", e.Message);
            }
            finally
            {
                this.IsUploading = false;
            }
        }