avm99963 | 04def3e | 2016-11-27 22:53:05 +0100 | [diff] [blame^] | 1 | (function() {
|
| 2 | const ALBUM_NAME = 'Screen Capture';
|
| 3 | const CLIENT_ID = '368358534491.apps.googleusercontent.com';
|
| 4 | const AUTH_URL = 'https://accounts.google.com/o/oauth2/auth';
|
| 5 | const REDIRECT_URI = 'https://picasaweb.google.com';
|
| 6 | const SCOPE = 'https://picasaweb.google.com/data/';
|
| 7 | const RESPONSE_TYPE = 'token';
|
| 8 | const ALBUM_URL = 'https://picasaweb.google.com/data/feed/api/user/' +
|
| 9 | 'default';
|
| 10 | const CREATE_ALBUM_URL = 'https://picasaweb.google.com/data/feed/api/user/' +
|
| 11 | 'default';
|
| 12 | const UPLOAD_BASE_URL = 'https://picasaweb.google.com/data/feed/api/user/' +
|
| 13 | 'default/albumid/';
|
| 14 | const LOGOUT_URL = 'https://picasaweb.google.com/bye?continue=' +
|
| 15 | 'https://www.google.com/accounts/Logout?continue=' +
|
| 16 | 'https://picasaweb.google.com';
|
| 17 |
|
| 18 | // var picasa = window.Picasa = new Site('picasa');
|
| 19 | var picasa = window.Picasa = {
|
| 20 | siteId: 'picasa',
|
| 21 | currentUserId: null,
|
| 22 | redirectUrl: REDIRECT_URI,
|
| 23 | accessTokenCallback: null,
|
| 24 |
|
| 25 | getAccessToken: function(callback) {
|
| 26 | picasa.accessTokenCallback = callback;
|
| 27 | var url = AUTH_URL + '?client_id=' + CLIENT_ID + '&redirect_uri=' +
|
| 28 | REDIRECT_URI + '&scope=' + SCOPE + '&response_type=' + RESPONSE_TYPE;
|
| 29 | chrome.tabs.create({ url: url});
|
| 30 | },
|
| 31 |
|
| 32 | parseRedirectUrl: function(url) {
|
| 33 | var result = false;
|
| 34 | if (url.indexOf(REDIRECT_URI) == 0) {
|
| 35 | var hash = url.split('#')[1];
|
| 36 | if (hash) {
|
| 37 | var params = hash.split('&');
|
| 38 | var paramMap = {};
|
| 39 | params.forEach(function(param) {
|
| 40 | paramMap[param.split('=')[0]] = param.split('=')[1];
|
| 41 | });
|
| 42 |
|
| 43 | var accessToken = paramMap['access_token'];
|
| 44 | var expires = paramMap['expires_in'];
|
| 45 | if (accessToken && expires) {
|
| 46 | result = {
|
| 47 | accessToken: accessToken,
|
| 48 | expires: expires
|
| 49 | };
|
| 50 | } else {
|
| 51 | result = 'bad_redirect_url'; // Should never happened.
|
| 52 | }
|
| 53 | } else {
|
| 54 | var search = url.split('?')[1];
|
| 55 | if (search == 'error=access_denied')
|
| 56 | result = 'access_denied';
|
| 57 | }
|
| 58 | }
|
| 59 | return result;
|
| 60 | },
|
| 61 |
|
| 62 | isRedirectUrl: function(url) {
|
| 63 | return picasa.parseRedirectUrl(url) != false;
|
| 64 | },
|
| 65 |
|
| 66 | parseAccessToken: function(redirectUrl) {
|
| 67 | var parsedResult = picasa.parseRedirectUrl(redirectUrl);
|
| 68 | if (parsedResult && typeof parsedResult == 'object') {
|
| 69 | var user = new User({
|
| 70 | accessToken: parsedResult.accessToken,
|
| 71 | expires: new Date().getTime() + parsedResult.expires * 1000
|
| 72 | });
|
| 73 | picasa.accessTokenCallback('success', user);
|
| 74 | } else {
|
| 75 | picasa.accessTokenCallback('failure', 'user_denied');
|
| 76 | }
|
| 77 | picasa.accessTokenCallback = null;
|
| 78 | },
|
| 79 |
|
| 80 | getUserInfo: function(user, callback) {
|
| 81 | ajax({
|
| 82 | url: ALBUM_URL,
|
| 83 | parameters: {
|
| 84 | fields: 'title,gphoto:nickname,entry/title,entry/gphoto:id',
|
| 85 | alt: 'json',
|
| 86 | access_token: user.accessToken
|
| 87 | },
|
| 88 | success: function(res) {
|
| 89 | var userId = res.feed.title.$t;
|
| 90 | var userName = res.feed.gphoto$nickname.$t;
|
| 91 | user.id = userId;
|
| 92 | user.name = userName;
|
| 93 |
|
| 94 | var albums = res.feed.entry;
|
| 95 | if (albums) {
|
| 96 | var length = albums.length;
|
| 97 |
|
| 98 | // Check if user has created album "Screen Capture".
|
| 99 | for (var i = 0; i < length; i++) {
|
| 100 | var albumName = albums[i].title.$t;
|
| 101 | if (albumName == ALBUM_NAME) {
|
| 102 | user.albumId = albums[i].gphoto$id.$t;
|
| 103 | break;
|
| 104 | }
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | // Create album "Screen Capture" and retrieve album id.
|
| 109 | if (!user.albumId) {
|
| 110 | picasa.createAlbum(user.accessToken, function(result,
|
| 111 | albumIdOrMessage) {
|
| 112 | if (result == 'success') {
|
| 113 | user.albumId = albumIdOrMessage;
|
| 114 | callback('success', user);
|
| 115 | } else {
|
| 116 | callback('failure', albumIdOrMessage);
|
| 117 | }
|
| 118 | });
|
| 119 | } else {
|
| 120 | callback('success', user);
|
| 121 | }
|
| 122 | },
|
| 123 | status: {
|
| 124 | 404: function() {
|
| 125 | callback('failure', 'failed_to_get_user_info');
|
| 126 | }
|
| 127 | }
|
| 128 | });
|
| 129 | },
|
| 130 |
|
| 131 | createAlbum: function(accessToken, callback) {
|
| 132 | var data = '<entry xmlns="http://www.w3.org/2005/Atom" ' +
|
| 133 | 'xmlns:media="http://search.yahoo.com/mrss/" ' +
|
| 134 | 'xmlns:gphoto="http://schemas.google.com/photos/2007">' +
|
| 135 | '<title type="text">' + ALBUM_NAME +
|
| 136 | '</title><category scheme="http://schemas.google.com/g/2005#kind" ' +
|
| 137 | 'term="http://schemas.google.com/photos/2007#album"></category>' +
|
| 138 | '</entry>';
|
| 139 |
|
| 140 | ajax({
|
| 141 | method: 'POST',
|
| 142 | url: CREATE_ALBUM_URL,
|
| 143 | parameters: {
|
| 144 | alt: 'json'
|
| 145 | },
|
| 146 | data: data,
|
| 147 | headers: {
|
| 148 | 'GData-Version': 2,
|
| 149 | 'Content-Type': 'application/atom+xml',
|
| 150 | 'Authorization': 'OAuth ' + accessToken
|
| 151 | },
|
| 152 | complete: function(statusCode, album) {
|
| 153 | if (statusCode == 201) {
|
| 154 | var albumId = album.entry.gphoto$id.$t;
|
| 155 | callback('success', albumId);
|
| 156 | } else {
|
| 157 | callback('failure', 'failure_to_create_album')
|
| 158 | }
|
| 159 | }
|
| 160 | });
|
| 161 | },
|
| 162 |
|
| 163 | upload: function(user, caption, imageData, callback) {
|
| 164 | caption = ajax.convertEntityString(caption);
|
| 165 | caption = ajax.encodeForBinary(caption);
|
| 166 |
|
| 167 | var imageFile = new Date().getTime() + '.png';
|
| 168 | var headers = {
|
| 169 | 'GData-Version': 2,
|
| 170 | 'Content-Type': 'multipart/related; boundary=' +
|
| 171 | MULTIPART_FORMDATA_BOUNDARY,
|
| 172 | 'Authorization': 'OAuth ' + user.accessToken
|
| 173 | };
|
| 174 |
|
| 175 | var captionData = '<entry xmlns="http://www.w3.org/2005/Atom">' +
|
| 176 | '<title>' + imageFile + '</title>' +
|
| 177 | '<summary>' + caption + '</summary>' +
|
| 178 | '<category scheme="http://schemas.google.com/g/2005#kind" ' +
|
| 179 | 'term="http://schemas.google.com/photos/2007#photo"/></entry>';
|
| 180 |
|
| 181 | var dataPart1 = {
|
| 182 | contentType: 'application/atom+xml',
|
| 183 | data: captionData
|
| 184 | };
|
| 185 | var dataPart2 = {
|
| 186 | contentType: 'image/png',
|
| 187 | data: imageData
|
| 188 | };
|
| 189 | var multipartData = {
|
| 190 | boundary: MULTIPART_FORMDATA_BOUNDARY,
|
| 191 | dataList: [dataPart1, dataPart2]
|
| 192 | };
|
| 193 |
|
| 194 | ajax({
|
| 195 | url: UPLOAD_BASE_URL + user.albumId + '?alt=json',
|
| 196 | headers: headers,
|
| 197 | multipartData: multipartData,
|
| 198 | complete: function(statusCode, res) {
|
| 199 | if (statusCode == 201) {
|
| 200 | var link = res.entry.link;
|
| 201 | callback('success', link);
|
| 202 | } else {
|
| 203 | var message = 'failed_to_upload_image';
|
| 204 | if (statusCode == 403) {
|
| 205 | // bad access token
|
| 206 | message = 'bad_access_token';
|
| 207 | } else if (statusCode == 404 && res == 'No album found.') {
|
| 208 | // Invalid album id.
|
| 209 | message = 'invalid_album_id'
|
| 210 | }
|
| 211 | callback('failure', message);
|
| 212 | }
|
| 213 | }
|
| 214 | });
|
| 215 | },
|
| 216 |
|
| 217 | getPhotoLink: function(user, photolinks, callback) {
|
| 218 | for (var i = 0; i < photolinks.length; i++) {
|
| 219 | var link = photolinks[i];
|
| 220 | if (link.type == 'text/html' &&
|
| 221 | link.rel == 'http://schemas.google.com/photos/2007#canonical') {
|
| 222 | callback('success', link.href);
|
| 223 | break;
|
| 224 | }
|
| 225 | }
|
| 226 | },
|
| 227 |
|
| 228 | logout: function(callback) {
|
| 229 | ajax({
|
| 230 | url: LOGOUT_URL,
|
| 231 | success: function() {
|
| 232 | callback();
|
| 233 | }
|
| 234 | });
|
| 235 | }
|
| 236 | };
|
| 237 | })(); |