/// <summary> /// Syncs a local file to an online Picasa Web Album location /// </summary> /// <param name="sourceFile">The image to sync.</param> /// <param name="targetAlbum">The target Picasa Web Album.</param> /// <param name="targetAlbumPhotoFeed">The target Picasa Web Album photo feed listing existing photos.</param> /// <param name="session">The current authenticated Picasa session.</param> private void AddFileToAlbum(FileInfo sourceFile, Album targetAlbum, PicasaFeed targetAlbumPhotoFeed, PicasaService session) { try { PicasaEntry existingPhotoEntry = (PicasaEntry)targetAlbumPhotoFeed.Entries.FirstOrDefault( p => p.Title.Text == sourceFile.Name && p.Summary.Text != ENTRY_DELETED_SUMMARY); if (existingPhotoEntry != null) { WriteOutput(string.Format("Skipping File: {0} (already exists)", sourceFile.Name), true); m_fileSkipCount++; } else { ImageFormat imageFormat; string contentType; bool isImage; bool isVideo; GetFileInfo(sourceFile, out isImage, out isVideo, out imageFormat, out contentType); Stream postResponseStream = null; Stream postFileStream = null; string souceFilePath = sourceFile.FullName; try { if (isVideo && this.ResizeVideos) { WriteOutput(string.Concat("Resizing Video: ", sourceFile.FullName), true); string resizedVideoPath = VideoResizer.ResizeVideo(sourceFile, this.ResizeVideosCommand); //change souceFilePath to resized video file location souceFilePath = resizedVideoPath; } using (Stream sourceFileStream = new FileStream(souceFilePath, FileMode.Open, FileAccess.Read)) { postFileStream = sourceFileStream; if (isImage && this.ResizePhotos) { WriteOutput(string.Concat("Resizing Photo: ", sourceFile.FullName), true); postFileStream = ImageResizer.ResizeImage(postFileStream, imageFormat, this.ResizePhotosMaxSize); } WriteOutput(string.Format("Uploading File: {0}", sourceFile.FullName), true); Uri insertPhotoFeedUri = new Uri(PicasaQuery.CreatePicasaUri(this.PicasaUsername, targetAlbum.Id)); PhotoEntry newFileEntry = new PhotoEntry(); newFileEntry.Title.Text = sourceFile.Name; newFileEntry.Summary.Text = string.Empty; newFileEntry.MediaSource = new MediaFileSource(postFileStream, sourceFile.Name, contentType); //upload file using multipart request postResponseStream = session.EntrySend(insertPhotoFeedUri, newFileEntry, GDataRequestType.Insert); newFileEntry.MediaSource.GetDataStream().Dispose(); } m_fileCreateCount++; } finally { if (postResponseStream != null) { postResponseStream.Dispose(); } if (postFileStream != null) { postFileStream.Dispose(); } } if (isVideo && this.ResizeVideos) { //video was resized and souceFilePath should be temp/resized video path try { File.Delete(souceFilePath); } catch (Exception ex) { WriteOutput(string.Format("Error Deleting Resized Video: {0} (Error - {1})", sourceFile.FullName, ex.Message), true); } } } } catch (GDataRequestException gdex) { WriteOutput(string.Format("Skipping File: {0} (Error - {1})", sourceFile.Name, gdex.ResponseString), true); m_errorsCount++; } catch (Exception ex) { WriteOutput(string.Format("Skipping File: {0} (Error - {1})", sourceFile.Name, ex), true); m_errorsCount++; } }