1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Improved logic to set resource file size

This commit is contained in:
Laurent Cozic 2019-05-12 15:53:42 +01:00
parent e57bfad9b1
commit 3e808f05fd
6 changed files with 61 additions and 6 deletions

View File

@ -1034,6 +1034,33 @@ describe('Synchronizer', function() {
expect(allEncrypted).toBe(false); expect(allEncrypted).toBe(false);
})); }));
it('should set the resource file size after decryption', asyncTest(async () => {
Setting.setValue('encryption.enabled', true);
const masterKey = await loadEncryptionMasterKey();
let folder1 = await Folder.save({ title: "folder1" });
let note1 = await Note.save({ title: 'ma note', parent_id: folder1.id });
await shim.attachFileToNote(note1, __dirname + '/../tests/support/photo.jpg');
let resource1 = (await Resource.all())[0];
await Resource.setFileSizeOnly(resource1.id, -1);
let resourcePath1 = Resource.fullPath(resource1);
await synchronizer().start();
await switchClient(2);
await synchronizer().start();
Setting.setObjectKey('encryption.passwordCache', masterKey.id, '123456');
await encryptionService().loadMasterKeysFromSettings();
const fetcher = new ResourceFetcher(() => { return synchronizer().api() });
fetcher.queueDownload(resource1.id);
await fetcher.waitForAllFinished();
await decryptionWorker().start();
const resource1_2 = await Resource.load(resource1.id);
expect(resource1_2.size).toBe(2720);
}));
it('should encrypt remote resources after encryption has been enabled', asyncTest(async () => { it('should encrypt remote resources after encryption has been enabled', asyncTest(async () => {
while (insideBeforeEach) await time.msleep(100); while (insideBeforeEach) await time.msleep(100);

View File

@ -17,11 +17,11 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>10.0.31</string> <string>10.0.32</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>31</string> <string>32</string>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>
<true/> <true/>
<key>NSAppTransportSecurity</key> <key>NSAppTransportSecurity</key>

View File

@ -6,6 +6,7 @@ const Setting = require('lib/models/Setting.js');
const ArrayUtils = require('lib/ArrayUtils.js'); const ArrayUtils = require('lib/ArrayUtils.js');
const pathUtils = require('lib/path-utils.js'); const pathUtils = require('lib/path-utils.js');
const { mime } = require('lib/mime-utils.js'); const { mime } = require('lib/mime-utils.js');
const { shim } = require('lib/shim');
const { filename, safeFilename } = require('lib/path-utils.js'); const { filename, safeFilename } = require('lib/path-utils.js');
const { FsDriverDummy } = require('lib/fs-driver-dummy.js'); const { FsDriverDummy } = require('lib/fs-driver-dummy.js');
const markdownUtils = require('lib/markdownUtils'); const markdownUtils = require('lib/markdownUtils');
@ -214,6 +215,10 @@ class Resource extends BaseItem {
await ResourceLocalState.save(Object.assign({}, state, { resource_id: id })); await ResourceLocalState.save(Object.assign({}, state, { resource_id: id }));
} }
static async needFileSizeSet() {
return this.modelSelectAll('SELECT * FROM resources WHERE `size` < 0 AND encryption_blob_encrypted = 0');
}
// Only set the `size` field and nothing else, not even the update_time // 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 // 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. // and each client does it so there's no need to sync the resource.

View File

@ -1,5 +1,6 @@
const BaseItem = require('lib/models/BaseItem'); const BaseItem = require('lib/models/BaseItem');
const Resource = require('lib/models/Resource'); const Resource = require('lib/models/Resource');
const ResourceService = require('lib/services/ResourceService');
const { Logger } = require('lib/logger.js'); const { Logger } = require('lib/logger.js');
class DecryptionWorker { class DecryptionWorker {
@ -132,6 +133,10 @@ class DecryptionWorker {
throw error; throw error;
} }
// 2019-05-12: Temporary to set the file size of the resources
// that weren't set in migration/20.js due to being on the sync target
await ResourceService.autoSetFileSizes();
this.logger().info('DecryptionWorker: completed decryption.'); this.logger().info('DecryptionWorker: completed decryption.');
this.dispatchReport({ state: 'idle' }); this.dispatchReport({ state: 'idle' });

View File

@ -1,5 +1,6 @@
const Resource = require('lib/models/Resource'); const Resource = require('lib/models/Resource');
const BaseService = require('lib/services/BaseService'); const BaseService = require('lib/services/BaseService');
const ResourceService = require('lib/services/ResourceService');
const BaseSyncTarget = require('lib/BaseSyncTarget'); const BaseSyncTarget = require('lib/BaseSyncTarget');
const { Logger } = require('lib/logger.js'); const { Logger } = require('lib/logger.js');
const EventEmitter = require('events'); const EventEmitter = require('events');
@ -103,14 +104,17 @@ class ResourceFetcher extends BaseService {
// 2019-05-12: This is only necessary to set the file size of the resources that come via // 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 // sync. The other ones have been done using migrations/20.js. This code can be removed
// after a few months. // after a few months.
if (resource.size < 0 && localResourceContentPath) { if (resource.size < 0 && localResourceContentPath && !resource.encryption_blob_encrypted) {
const itDoes = await shim.fsDriver().waitTillExists(localResourceContentPath); await ResourceService.autoSetFileSizes();
const fileStat = await shim.fsDriver().stat(localResourceContentPath);
await Resource.setFileSizeOnly(resource.id, fileStat.size);
} }
delete this.fetchingItems_[resource.id]; delete this.fetchingItems_[resource.id];
this.scheduleQueueProcess(); this.scheduleQueueProcess();
// Note: This downloadComplete event is not really right or useful because the resource
// might still be encrypted and the caller usually can't do much with this. In particular
// the note being displayed will refresh the resource images but since they are still
// encrypted it's not useful. Probably, the views should listen to DecryptionWorker events instead.
if (emitDownloadComplete) this.eventEmitter_.emit('downloadComplete', { id: resource.id }); if (emitDownloadComplete) this.eventEmitter_.emit('downloadComplete', { id: resource.id });
this.updateReport(); this.updateReport();
} }

View File

@ -111,6 +111,20 @@ class ResourceService extends BaseService {
} }
} }
static async autoSetFileSize(resourceId, filePath) {
const itDoes = await shim.fsDriver().waitTillExists(filePath);
const fileStat = await shim.fsDriver().stat(filePath);
await Resource.setFileSizeOnly(resourceId, fileStat.size);
}
static async autoSetFileSizes() {
const resources = await Resource.needFileSizeSet();
for (const r of resources) {
await this.autoSetFileSize(r.id, Resource.fullPath(r));
}
}
async maintenance() { async maintenance() {
await this.indexNoteResources(); await this.indexNoteResources();
await this.deleteOrphanResources(); await this.deleteOrphanResources();