keeweb/app/scripts/storage/impl/storage-gdrive.js

259 lines
9.8 KiB
JavaScript
Raw Normal View History

2019-09-15 14:16:32 +02:00
import { StorageBase } from 'storage/storage-base';
import { Locale } from 'util/locale';
2016-03-13 17:08:25 +01:00
const GDriveClientId = {
Local: '783608538594-36tkdh8iscrq8t8dq87gghubnhivhjp5.apps.googleusercontent.com',
Production: '847548101761-koqkji474gp3i2gn3k5omipbfju7pbt1.apps.googleusercontent.com'
};
2017-01-31 07:50:28 +01:00
const NewFileIdPrefix = 'NewFile:';
2016-03-27 11:08:54 +02:00
2017-01-31 07:50:28 +01:00
const StorageGDrive = StorageBase.extend({
2016-03-13 17:08:25 +01:00
name: 'gdrive',
2016-03-26 23:04:34 +01:00
enabled: true,
2016-03-13 17:08:25 +01:00
uipos: 30,
2019-08-16 23:05:39 +02:00
iconSvg:
'<svg xmlns="http://www.w3.org/2000/svg" width="128" height="128" viewBox="0 0 128 128"><path d="M86.657536,76.246208 L47.768064,9 L89.111168,' +
2017-03-06 20:57:42 +01:00
'9 L128,76.246208 L86.657536,76.246208 Z M25.010048,119.08 L102.690048,119.08 L123.36256,83.24 L45.68064,83.24 L25.010048,119.08 L25.010048,' +
'119.08 Z M38.793088,9.003712 L0,76.30496 L20.671872,112.110016 L59.464704,44.808128 L38.793088,9.003712 Z"></path></svg>',
2016-03-13 17:08:25 +01:00
2016-03-27 14:57:22 +02:00
_baseUrl: 'https://www.googleapis.com/drive/v3',
2017-11-26 20:34:14 +01:00
_baseUrlUpload: 'https://www.googleapis.com/upload/drive/v3',
2016-03-27 11:08:54 +02:00
2019-08-18 10:17:09 +02:00
getPathForName(fileName) {
2016-03-27 20:14:31 +02:00
return NewFileIdPrefix + fileName;
},
2019-08-18 10:17:09 +02:00
load(path, opts, callback) {
this.stat(path, opts, (err, stat) => {
2019-08-16 23:05:39 +02:00
if (err) {
return callback && callback(err);
}
this.logger.debug('Load', path);
2017-01-31 07:50:28 +01:00
const ts = this.logger.ts();
2019-08-16 23:05:39 +02:00
const url =
this._baseUrl +
2019-08-18 08:05:38 +02:00
'/files/{id}/revisions/{rev}?alt=media'
.replace('{id}', path)
.replace('{rev}', stat.rev);
this._xhr({
2019-08-18 10:17:09 +02:00
url,
2016-03-27 09:42:48 +02:00
responseType: 'arraybuffer',
2019-08-16 23:05:39 +02:00
success: response => {
this.logger.debug('Loaded', path, stat.rev, this.logger.ts(ts));
2016-03-27 09:42:48 +02:00
return callback && callback(null, response, { rev: stat.rev });
},
2019-08-16 23:05:39 +02:00
error: err => {
this.logger.error('Load error', path, err, this.logger.ts(ts));
2016-03-27 09:42:48 +02:00
return callback && callback(err);
2016-03-26 21:12:56 +01:00
}
});
});
2016-03-13 17:08:25 +01:00
},
2019-08-18 10:17:09 +02:00
stat(path, opts, callback) {
2016-03-27 20:14:31 +02:00
if (path.lastIndexOf(NewFileIdPrefix, 0) === 0) {
return callback && callback({ notFound: true });
}
2016-07-17 13:30:38 +02:00
this._oauthAuthorize(err => {
2016-03-26 21:12:56 +01:00
if (err) {
return callback && callback(err);
}
this.logger.debug('Stat', path);
2017-01-31 07:50:28 +01:00
const ts = this.logger.ts();
2019-08-16 23:05:39 +02:00
const url = this._baseUrl + '/files/{id}?fields=headRevisionId'.replace('{id}', path);
this._xhr({
2019-08-18 10:17:09 +02:00
url,
2016-03-27 09:42:48 +02:00
responseType: 'json',
2019-08-16 23:05:39 +02:00
success: response => {
2017-01-31 07:50:28 +01:00
const rev = response.headRevisionId;
this.logger.debug('Stated', path, rev, this.logger.ts(ts));
2019-08-18 10:17:09 +02:00
return callback && callback(null, { rev });
2016-03-27 09:42:48 +02:00
},
2019-08-16 23:05:39 +02:00
error: err => {
this.logger.error('Stat error', this.logger.ts(ts), err);
2016-03-27 09:42:48 +02:00
return callback && callback(err);
}
2016-03-26 21:12:56 +01:00
});
});
},
2019-08-18 10:17:09 +02:00
save(path, opts, data, callback, rev) {
2016-08-21 17:43:59 +02:00
this._oauthAuthorize(err => {
if (err) {
return callback && callback(err);
2016-03-27 20:14:31 +02:00
}
2016-08-21 17:43:59 +02:00
this.stat(path, opts, (err, stat) => {
if (rev) {
if (err) {
return callback && callback(err);
}
if (stat.rev !== rev) {
return callback && callback({ revConflict: true }, stat);
2016-03-27 09:42:48 +02:00
}
2016-03-26 21:12:56 +01:00
}
2016-08-21 17:43:59 +02:00
this.logger.debug('Save', path);
2017-01-31 07:50:28 +01:00
const ts = this.logger.ts();
const isNew = path.lastIndexOf(NewFileIdPrefix, 0) === 0;
let url;
2016-08-21 17:43:59 +02:00
if (isNew) {
2019-08-18 08:05:38 +02:00
url =
this._baseUrlUpload +
'/files?uploadType=multipart&fields=id,headRevisionId';
2017-01-31 07:50:28 +01:00
const fileName = path.replace(NewFileIdPrefix, '') + '.kdbx';
const boundry = 'b' + Date.now() + 'x' + Math.round(Math.random() * 1000000);
2019-08-16 23:05:39 +02:00
data = new Blob(
[
'--',
boundry,
'\r\n',
'Content-Type: application/json; charset=UTF-8',
'\r\n\r\n',
JSON.stringify({ name: fileName }),
'\r\n',
'--',
boundry,
'\r\n',
'Content-Type: application/octet-stream',
'\r\n\r\n',
data,
'\r\n',
'--',
boundry,
'--',
'\r\n'
],
{ type: 'multipart/related; boundary="' + boundry + '"' }
);
2016-08-21 17:43:59 +02:00
} else {
2019-08-16 23:05:39 +02:00
url =
this._baseUrlUpload +
'/files/{id}?uploadType=media&fields=headRevisionId'.replace('{id}', path);
2016-08-21 17:43:59 +02:00
data = new Blob([data], { type: 'application/octet-stream' });
}
this._xhr({
2019-08-18 10:17:09 +02:00
url,
2016-08-21 17:43:59 +02:00
method: isNew ? 'POST' : 'PATCH',
responseType: 'json',
2019-08-18 10:17:09 +02:00
data,
2019-08-16 23:05:39 +02:00
success: response => {
2016-08-21 17:43:59 +02:00
this.logger.debug('Saved', path, this.logger.ts(ts));
2017-01-31 07:50:28 +01:00
const newRev = response.headRevisionId;
2016-08-21 17:43:59 +02:00
if (!newRev) {
return callback && callback('save error: no rev');
}
2019-08-18 08:05:38 +02:00
return (
callback &&
callback(null, { rev: newRev, path: isNew ? response.id : null })
);
2016-08-21 17:43:59 +02:00
},
2019-08-16 23:05:39 +02:00
error: err => {
2016-08-21 17:43:59 +02:00
this.logger.error('Save error', path, err, this.logger.ts(ts));
return callback && callback(err);
}
});
2016-03-26 21:12:56 +01:00
});
});
},
2019-08-18 10:17:09 +02:00
list(dir, callback) {
2019-08-16 23:05:39 +02:00
this._oauthAuthorize(err => {
if (err) {
return callback && callback(err);
}
this.logger.debug('List');
2019-08-18 08:05:38 +02:00
let query =
dir === 'shared'
? 'sharedWithMe=true'
: dir
? `"${dir}" in parents`
: '"root" in parents';
query += ' and trashed=false';
2019-08-16 23:05:39 +02:00
const url =
this._baseUrl +
'/files?fields={fields}&q={q}&pageSize=1000'
2019-08-18 08:05:38 +02:00
.replace(
'{fields}',
encodeURIComponent('files(id,name,mimeType,headRevisionId)')
)
2019-08-16 23:05:39 +02:00
.replace('{q}', encodeURIComponent(query));
2017-01-31 07:50:28 +01:00
const ts = this.logger.ts();
this._xhr({
2019-08-18 10:17:09 +02:00
url,
2016-03-27 09:42:48 +02:00
responseType: 'json',
2019-08-16 23:05:39 +02:00
success: response => {
2016-03-27 09:42:48 +02:00
if (!response) {
this.logger.error('List error', this.logger.ts(ts));
2016-03-27 09:42:48 +02:00
return callback && callback('list error');
}
this.logger.debug('Listed', this.logger.ts(ts));
2017-01-31 07:50:28 +01:00
const fileList = response.files.map(f => ({
2016-07-17 13:30:38 +02:00
name: f.name,
path: f.id,
2017-11-26 20:34:14 +01:00
rev: f.headRevisionId,
dir: f.mimeType === 'application/vnd.google-apps.folder'
2016-07-17 13:30:38 +02:00
}));
if (!dir) {
fileList.unshift({
name: Locale.gdriveSharedWithMe,
path: 'shared',
rev: undefined,
dir: true
});
}
2016-03-27 09:42:48 +02:00
return callback && callback(null, fileList);
},
2019-08-16 23:05:39 +02:00
error: err => {
this.logger.error('List error', this.logger.ts(ts), err);
2016-03-27 09:42:48 +02:00
return callback && callback(err);
2016-03-26 21:12:56 +01:00
}
});
});
},
2019-08-18 10:17:09 +02:00
remove(path, callback) {
this.logger.debug('Remove', path);
2017-01-31 07:50:28 +01:00
const ts = this.logger.ts();
const url = this._baseUrl + '/files/{id}'.replace('{id}', path);
this._xhr({
2019-08-18 10:17:09 +02:00
url,
2016-03-27 20:14:31 +02:00
method: 'DELETE',
responseType: 'json',
statuses: [200, 204],
success: () => {
this.logger.debug('Removed', path, this.logger.ts(ts));
2016-03-27 20:14:31 +02:00
return callback && callback();
},
2019-08-16 23:05:39 +02:00
error: err => {
this.logger.error('Remove error', path, err, this.logger.ts(ts));
2016-03-27 20:14:31 +02:00
return callback && callback(err);
}
});
},
2019-08-18 10:17:09 +02:00
setEnabled(enabled) {
if (!enabled) {
this._oauthRevokeToken('https://accounts.google.com/o/oauth2/revoke?token={token}');
}
StorageBase.prototype.setEnabled.call(this, enabled);
},
2019-08-18 10:17:09 +02:00
_getOAuthConfig() {
let clientId = this.appSettings.get('gdriveClientId');
if (!clientId) {
2019-08-18 08:05:38 +02:00
clientId =
location.origin.indexOf('localhost') >= 0
? GDriveClientId.Local
: GDriveClientId.Production;
}
2016-03-27 15:18:05 +02:00
return {
2016-03-27 16:47:29 +02:00
scope: 'https://www.googleapis.com/auth/drive',
url: 'https://accounts.google.com/o/oauth2/v2/auth',
2019-08-18 10:17:09 +02:00
clientId,
2016-03-27 14:57:22 +02:00
width: 600,
height: 400
2016-03-27 15:18:05 +02:00
};
2016-03-13 17:08:25 +01:00
}
2016-03-27 09:06:23 +02:00
});
2016-03-13 17:08:25 +01:00
2019-09-15 14:16:32 +02:00
export { StorageGDrive };