diff --git a/example/photos/jake.jpg b/example/photos/jake.jpg new file mode 100644 index 0000000..d20f3ce Binary files /dev/null and b/example/photos/jake.jpg differ diff --git a/example/postPhoto.js b/example/postPhoto.js new file mode 100644 index 0000000..e1a4732 --- /dev/null +++ b/example/postPhoto.js @@ -0,0 +1,23 @@ +'use strict' + +const fs = require('fs') + +const Picasa = require('../.') + +const accessToken = 'ya29.ZQLb0DRxf7PzBvkok6DGoSime3lym2ZNCzFPhyU1HGzkzwXaSwffivulMXcGFcgM4x2w' +const albumId = '6236607441185654065' + +const picasa = new Picasa() + +fs.readFile(__dirname + '/photos/jake.jpg', (err, binary) => { + const photoData = { + title : 'Jake the dog', + summary : 'Corgis ftw!', + contentType : 'image/jpeg', + binary : binary + } + + picasa.postPhoto(accessToken, albumId, photoData, (error, response) => { + console.log(error, response) + }) +}) diff --git a/src/picasa.js b/src/picasa.js index 90d2790..f48a24c 100644 --- a/src/picasa.js +++ b/src/picasa.js @@ -12,54 +12,89 @@ const PICASA_HOST = 'https://picasaweb.google.com' const PICASA_SCOPE = '/data' const PICASA_API_PATH = '/feed/api/user/default' +const FETCH_AS_JSON = 'json' + function Picasa () { this.executeRequest = executeRequest } Picasa.prototype.getPhotos = getPhotos +Picasa.prototype.postPhoto = postPhoto +// Auth utilities Picasa.prototype.getAuthURL = getAuthURL Picasa.prototype.getAccessToken = getAccessToken +function postPhoto (accessToken, albumId, photoData, callback) { + const accessTokenQuery = querystring.stringify({ + alt : FETCH_AS_JSON, + access_token : accessToken + }) + + const photoInfoXML = ` + ${photoData.title} + ${photoData.summary} + + ` + + const requestOptions = { + url : `${PICASA_HOST}${PICASA_SCOPE}${PICASA_API_PATH}/albumid/${albumId}?${accessTokenQuery}`, + multipart : [ + {'Content-Type' : 'application/atom+xml', body : photoInfoXML}, + {'Content-Type' : photoData.contentType, body : photoData.binary} + ] + } + + this.executeRequest('post', requestOptions, (error, body) => { + if (error) return callback(error) + + const photo = getPhotoByEntry(body.entry) + + callback(error, photo) + }) +} + function getPhotos (accessToken, options, callback) { const requestOptions = buildPicasaRequestOptions(accessToken, 'photo', options) this.executeRequest('get', requestOptions, (error, body) => { if (error) return callback(error) - const photoSchema = { - 'gphoto$id' : 'id', - 'gphoto$albumid' : 'album_id', - 'gphoto$access' : 'access', - 'gphoto$width' : 'width', - 'gphoto$height' : 'height', - 'gphoto$size' : 'size' , - 'gphoto$checksum' : 'checksum', - 'gphoto$timestamp' : 'timestamp', - 'gphoto$imageVersion' : 'image_version', - 'gphoto$commentingEnabled' : 'commenting_enabled', - 'gphoto$commentCount' : 'comment_count', - 'content' : 'content', - 'title' : 'title', - 'summary' : 'summary' - } - - const photos = body.feed.entry.map(entry => { - let photo = {} - - Object.keys(photoSchema).forEach(schemaKey => { - const key = photoSchema[schemaKey] - - if (key) photo[key] = checkParam(entry[schemaKey]) - }) - - return photo - }) + const photos = body.feed.entry.map(getPhotoByEntry) callback(null, photos) }) } +const photoSchema = { + 'gphoto$id' : 'id', + 'gphoto$albumid' : 'album_id', + 'gphoto$access' : 'access', + 'gphoto$width' : 'width', + 'gphoto$height' : 'height', + 'gphoto$size' : 'size' , + 'gphoto$checksum' : 'checksum', + 'gphoto$timestamp' : 'timestamp', + 'gphoto$imageVersion' : 'image_version', + 'gphoto$commentingEnabled' : 'commenting_enabled', + 'gphoto$commentCount' : 'comment_count', + 'content' : 'content', + 'title' : 'title', + 'summary' : 'summary' +} + +function getPhotoByEntry (entry) { + let photo = {} + + Object.keys(photoSchema).forEach(schemaKey => { + const key = photoSchema[schemaKey] + + if (key) photo[key] = checkParam(entry[schemaKey]) + }) + + return photo +} + function getAuthURL (config) { const authenticationParams = { access_type : 'offline', @@ -96,10 +131,8 @@ function getAccessToken (config, code, callback) { } function buildPicasaRequestOptions (accessToken, kind, options) { - const fetchKind = 'json' - const accessTokenParams = { - alt : fetchKind, + alt : FETCH_AS_JSON, kind : kind, access_token : accessToken }