1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-23 22:36:32 +02:00

fetchblob

This commit is contained in:
Laurent Cozic
2017-07-06 22:30:45 +01:00
parent 40ed3216a3
commit 8ee0c38f86
12 changed files with 130 additions and 49 deletions

View File

@@ -85,15 +85,21 @@ class FileApiDriverOneDrive {
}
}
async get(path) {
let content = null;
async get(path, options = null) {
if (!options) options = {};
try {
content = await this.api_.execText('GET', this.makePath_(path) + ':/content');
if (options.target == 'file') {
let response = await this.api_.exec('GET', this.makePath_(path) + ':/content', null, null, options);
return response;
} else {
let content = await this.api_.execText('GET', this.makePath_(path) + ':/content');
return content;
}
} catch (error) {
if (error.code == 'itemNotFound') return null;
throw error;
}
return content;
}
async mkdir(path) {

View File

@@ -62,7 +62,8 @@ class FileApi {
});
}
get(path, options = {}) {
get(path, options = null) {
if (!options) options = {};
if (!options.encoding) options.encoding = 'utf8';
this.logger().debug('get ' + this.fullPath_(path));
return this.driver_.get(this.fullPath_(path), options);

View File

@@ -46,6 +46,7 @@ class Setting extends BaseModel {
}
static setConstant(key, value) {
if (!(key in this.constants_)) throw new Error('Unknown constant key: ' + key);
this.constants_[key] = value;
}
@@ -152,6 +153,9 @@ Setting.defaults_ = {
Setting.constants_ = {
'appName': 'joplin',
'appId': 'SET_ME', // Each app should set this identifier
'resourceDir': '',
'profileDir': '',
'tempDir': '',
}
export { Setting };

View File

@@ -84,6 +84,7 @@ class OneDriveApi {
try {
const json = await r.json();
this.setAuth(json);
this.dispatch('authRefreshed', this.auth());
} catch (error) {
const text = await r.text();
error.message += ': ' + text;
@@ -110,6 +111,7 @@ class OneDriveApi {
if (!options) options = {};
if (!options.headers) options.headers = {};
if (!options.target) options.target = 'string';
if (method != 'GET') {
options.method = method;
@@ -137,7 +139,13 @@ class OneDriveApi {
for (let i = 0; i < 5; i++) {
options.headers['Authorization'] = 'bearer ' + this.token();
let response = await shim.fetch(url, options);
let response = null;
if (options.target == 'string') {
response = await shim.fetch(url, options);
} else { // file
response = await shim.fetchBlob(url, options);
}
if (!response.ok) {
let errorResponse = await response.json();
let error = this.oneDriveErrorResponseToError(errorResponse);
@@ -194,7 +202,7 @@ class OneDriveApi {
let body = new shim.FormData();
body.append('client_id', this.clientId());
body.append('client_secret', this.clientSecret());
// body.append('client_secret', this.clientSecret()); // TODO: NEEDED FOR NODE
body.append('refresh_token', this.auth_.refresh_token);
body.append('redirect_uri', 'http://localhost:1917');
body.append('grant_type', 'refresh_token');

View File

@@ -29,6 +29,7 @@ reg.oneDriveApi = () => {
}
reg.oneDriveApi_.on('authRefreshed', (a) => {
reg.logger().info('Saving updated OneDrive auth.');
Setting.setValue('sync.onedrive.auth', JSON.stringify(a));
});
@@ -58,6 +59,7 @@ reg.synchronizer = async () => {
let fileApi = await reg.fileApi();
reg.synchronizer_ = new Synchronizer(reg.db(), fileApi);
reg.synchronizer_.setLogger(reg.logger());
return reg.synchronizer_;
}

View File

@@ -320,6 +320,12 @@ class Synchronizer {
// await Resource.setContent(newContent, remoteResourceContent);
// }
if (newContent.type_ == BaseModel.TYPE_RESOURCE && action == 'createLocal') {
let localResourceContentPath = Resource.fullPath(newContent);
let remoteResourceContentPath = this.resourceDirName_ + '/' + newContent.id;
await this.api().get(remoteResourceContentPath, { path: localResourceContentPath, target: 'file' });
}
await ItemClass.save(newContent, options);
this.logSyncOperation(action, local, content, reason);