From 8f072ec0df161b79f04aef212438d99f720d5984 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Thu, 27 Jul 2023 16:32:27 +0200 Subject: [PATCH] fix: do not update mtime if not already defined MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- __tests__/files/file.spec.ts | 17 +++++++++++++++++ lib/files/node.ts | 15 ++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/__tests__/files/file.spec.ts b/__tests__/files/file.spec.ts index d260e51e..70902e27 100644 --- a/__tests__/files/file.spec.ts +++ b/__tests__/files/file.spec.ts @@ -223,4 +223,21 @@ describe('Altering attributes updates mtime', () => { expect(file.mtime?.getDate()).toBe(new Date().getDate()) expect(file.attributes.test).toBeUndefined() }) + + test('mtime is NOT updated if not initially defined', () => { + const file = new File({ + source: 'https://cloud.domain.com/remote.php/dav/files/emma', + mime: 'image/jpeg', + owner: 'emma', + attributes: { + test: true, + }, + }) + expect(file.attributes.test).toBe(true) + delete file.attributes.test + + // Check that mtime has been updated + expect(file.mtime).toBeUndefined() + expect(file.attributes.test).toBeUndefined() + }) }) diff --git a/lib/files/node.ts b/lib/files/node.ts index dbc25321..a42629e2 100644 --- a/lib/files/node.ts +++ b/lib/files/node.ts @@ -40,13 +40,13 @@ export abstract class Node { // eslint-disable-next-line @typescript-eslint/no-explicit-any set: (target: Attribute, prop: string, value: any): any => { // Edit modification time - this._data.mtime = new Date() + this.updateMtime() // Apply original changes return Reflect.set(target, prop, value) }, deleteProperty: (target: Attribute, prop: string) => { // Edit modification time - this._data.mtime = new Date() + this.updateMtime() // Apply original changes return Reflect.deleteProperty(target, prop) }, @@ -222,7 +222,7 @@ export abstract class Node { move(destination: string) { validateData({ ...this._data, source: destination }, this._knownDavService) this._data.source = destination - this._data.mtime = new Date() + this.updateMtime() } /** @@ -238,4 +238,13 @@ export abstract class Node { this.move(dirname(this.source) + '/' + basename) } + /** + * Update the mtime if exists. + */ + private updateMtime() { + if (this._data.mtime) { + this._data.mtime = new Date() + } + } + }