1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-29 22:48:10 +02:00

All: Make sure resource filesize is set in all cases

This commit is contained in:
Laurent Cozic
2019-05-12 11:41:07 +01:00
parent ed3970be81
commit e1b7b64e1b
10 changed files with 66 additions and 9 deletions

View File

@@ -2,6 +2,7 @@ const Resource = require('lib/models/Resource');
const Setting = require('lib/models/Setting');
const { shim } = require('lib/shim');
const { reg } = require('lib/registry.js');
const { fileExtension } = require('lib/path-utils.js');
const script = {};
@@ -10,7 +11,10 @@ script.exec = async function() {
const queries = [];
for (const stat of stats) {
if (fileExtension(stat.path) === 'crypted') continue;
const resourceId = Resource.pathToId(stat.path);
if (!resourceId) continue;
queries.push({ sql: 'UPDATE resources SET `size` = ? WHERE id = ?', params: [stat.size, resourceId] });
if (queries.length >= 1000) {

View File

@@ -214,6 +214,13 @@ class Resource extends BaseItem {
await ResourceLocalState.save(Object.assign({}, state, { resource_id: id }));
}
// Only set the `size` field and nothing else, not even the update_time
// This is because it's only necessary to do it once after migration 20
// and each client does it so there's no need to sync the resource.
static async setFileSizeOnly(resourceId, fileSize) {
return this.db().exec('UPDATE resources set `size` = ? WHERE id = ?', [fileSize, resourceId]);
}
static async batchDelete(ids, options = null) {
// For resources, there's not really batch deleting since there's the file data to delete
// too, so each is processed one by one with the item being deleted last (since the db

View File

@@ -14,16 +14,19 @@ class MigrationService extends BaseService {
return this.instance_;
}
async runScript(num) {
const script = Migration.script(num);
await script.exec();
}
async run() {
const migrations = await Migration.migrationsToDo();
for (const migration of migrations) {
this.logger().info('Running migration: ' + migration.number);
const script = Migration.script(migration.number);
try {
await script.exec();
await this.runScript(migration.number);
await Migration.delete(migration.id);
} catch (error) {
this.logger().error('Cannot run migration: ' + migration.number, error);

View File

@@ -3,6 +3,7 @@ const BaseService = require('lib/services/BaseService');
const BaseSyncTarget = require('lib/BaseSyncTarget');
const { Logger } = require('lib/logger.js');
const EventEmitter = require('events');
const { shim } = require('lib/shim');
class ResourceFetcher extends BaseService {
@@ -97,7 +98,17 @@ class ResourceFetcher extends BaseService {
if (this.fetchingItems_[resourceId]) return;
this.fetchingItems_[resourceId] = true;
const completeDownload = (emitDownloadComplete = true) => {
const completeDownload = async (emitDownloadComplete = true, localResourceContentPath = '') => {
// 2019-05-12: This is only necessary to set the file size of the resources that come via
// sync. The other ones have been done using migrations/20.js. This code can be removed
// after a few months.
if (resource.size < 0 && localResourceContentPath) {
const itDoes = await shim.fsDriver().waitTillExists(localResourceContentPath);
const fileStat = await shim.fsDriver().stat(localResourceContentPath);
await Resource.setFileSizeOnly(resource.id, fileStat.size);
}
delete this.fetchingItems_[resource.id];
this.scheduleQueueProcess();
if (emitDownloadComplete) this.eventEmitter_.emit('downloadComplete', { id: resource.id });
@@ -110,7 +121,7 @@ class ResourceFetcher extends BaseService {
// Shouldn't happen, but just to be safe don't re-download the
// resource if it's already been downloaded.
if (localState.fetch_status === Resource.FETCH_STATUS_DONE) {
completeDownload(false);
await completeDownload(false);
return;
}
@@ -128,11 +139,11 @@ class ResourceFetcher extends BaseService {
fileApi.get(remoteResourceContentPath, { path: localResourceContentPath, target: "file" }).then(async () => {
await Resource.setLocalState(resource, { fetch_status: Resource.FETCH_STATUS_DONE });
this.logger().debug('ResourceFetcher: Resource downloaded: ' + resource.id);
completeDownload();
await completeDownload(true, localResourceContentPath);
}).catch(async (error) => {
this.logger().error('ResourceFetcher: Could not download resource: ' + resource.id, error);
await Resource.setLocalState(resource, { fetch_status: Resource.FETCH_STATUS_ERROR, fetch_error: error.message });
completeDownload();
await completeDownload();
});
}