Skip to content

Commit edb80ff

Browse files
committed
feat: allow changing Node mime type
Signed-off-by: John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
1 parent 065a1fc commit edb80ff

2 files changed

Lines changed: 63 additions & 3 deletions

File tree

__tests__/files/node.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,49 @@ describe('FileId attribute', () => {
8181
})
8282
})
8383

84+
describe('Mime attribute', () => {
85+
test('Mime definition', () => {
86+
const file = new File({
87+
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
88+
mime: 'image/jpeg',
89+
owner: 'emma',
90+
})
91+
expect(file.mime).toBe('image/jpeg')
92+
})
93+
94+
test('Default mime', () => {
95+
const file = new File({
96+
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
97+
owner: 'emma',
98+
})
99+
expect(file.mime).toBe('application/octet-stream')
100+
})
101+
102+
test('Changing mime', () => {
103+
const file = new File({
104+
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
105+
mime: 'image/jpeg',
106+
owner: 'emma',
107+
})
108+
expect(file.mime).toBe('image/jpeg')
109+
110+
file.mime = 'image/png'
111+
expect(file.mime).toBe('image/png')
112+
})
113+
114+
test('Removing mime', () => {
115+
const file = new File({
116+
source: 'https://cloud.domain.com/remote.php/dav/picture.jpg',
117+
mime: 'image/jpeg',
118+
owner: 'emma',
119+
})
120+
expect(file.mime).toBe('image/jpeg')
121+
122+
file.mime = undefined
123+
expect(file.mime).toBe('application/octet-stream')
124+
})
125+
})
126+
84127
describe('Mtime attribute', () => {
85128
test('Mtime definition', () => {
86129
const mtime = new Date()

lib/files/node.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ export abstract class Node {
5959
} as ProxyHandler<Attribute>
6060

6161
constructor(data: NodeData, davService?: RegExp) {
62+
if (!data.mime) {
63+
data.mime = 'application/octet-stream'
64+
}
65+
6266
// Validate data
6367
validateData(data, davService || this._knownDavService)
6468

@@ -167,10 +171,23 @@ export abstract class Node {
167171

168172
/**
169173
* Get the file mime
170-
* There is no setter as the mime is not meant to be changed
171174
*/
172-
get mime(): string|undefined {
173-
return this._data.mime
175+
get mime(): string {
176+
return this._data.mime || 'application/octet-stream'
177+
}
178+
179+
/**
180+
* Set the file mime
181+
* Removing the mime type will set it to `application/octet-stream`
182+
*/
183+
set mime(mime: string|undefined) {
184+
if (mime === undefined) {
185+
mime = 'application/octet-stream'
186+
}
187+
188+
validateData({ ...this._data, mime }, this._knownDavService)
189+
this.updateMtime()
190+
this._data.mime = mime
174191
}
175192

176193
/**

0 commit comments

Comments
 (0)