Skip to content

Commit 03791b0

Browse files
committed
fix: accept altText and mimetype as storeOptions
1 parent 8171fdb commit 03791b0

File tree

8 files changed

+65
-24
lines changed

8 files changed

+65
-24
lines changed

src/lib/api/upload/file_tools.browser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ const readPart = (start: number, end: number, file): Promise<any> => {
154154
* @param {*} fileOrString
155155
* @returns {Promise<File>}
156156
*/
157-
export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Promise<FsFile> => {
157+
export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions, mimetype?: string): Promise<FsFile> => {
158158
let filename;
159159
let file: Blob;
160160

@@ -186,7 +186,7 @@ export const getFile = (input: InputFile, sanitizeOptions?: SanitizeOptions): Pr
186186
{
187187
name: filename,
188188
size: file.size,
189-
type: mime,
189+
type: mimetype || mime,
190190
slice: res.slice,
191191
release: res.release,
192192
},

src/lib/api/upload/file_tools.node.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ const isFileBase = (input: InputFile): input is string => {
6363
* @param {*} inputFile
6464
* @returns {Promise<File>}
6565
*/
66-
export const getFile = async(input: InputFile, sanitizeOptions?: SanitizeOptions): Promise<FsFile> => {
66+
export const getFile = async(input: InputFile, sanitizeOptions?: SanitizeOptions, mimetype?: string): Promise<FsFile> => {
6767
let filename;
6868

6969
if (isFileNamed(input)) {
@@ -88,7 +88,7 @@ export const getFile = async(input: InputFile, sanitizeOptions?: SanitizeOptions
8888
{
8989
name: filename,
9090
size: buffer.byteLength,
91-
type: mime,
91+
type: mimetype || mime,
9292
slice: (start, end) => Promise.resolve(buffer.slice(start, end)),
9393
},
9494
sanitizeOptions
@@ -114,7 +114,7 @@ export const getFile = async(input: InputFile, sanitizeOptions?: SanitizeOptions
114114
{
115115
name: filename,
116116
size: input.byteLength,
117-
type: mime,
117+
type: mimetype || mime,
118118
// @ts-ignore
119119
slice: (start, end) => Promise.resolve(input.slice(start, end)),
120120
},

src/lib/api/upload/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ export interface UploadOptions {
8181
* @memberof UploadOptions
8282
*/
8383
tags?: UploadTags;
84-
85-
altText?: string;
8684
}
8785

8886
export type StoreUploadOptions = StoreBaseParams & {
@@ -91,6 +89,16 @@ export type StoreUploadOptions = StoreBaseParams & {
9189
*/
9290
filename?: ((file: File) => string) | string;
9391

92+
/**
93+
* Mimetype of the stored file
94+
*/
95+
mimetype?: string;
96+
97+
/**
98+
* Alt text of the stored file
99+
*/
100+
altText?: string;
101+
94102
/**
95103
* Workflows ids to run after upload
96104
*/

src/lib/api/upload/upload.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ export class Upload extends EventEmitter {
6767
*/
6868
private overrideFileName;
6969

70+
/**
71+
* Mimetype of the file
72+
*
73+
* @private
74+
* @memberof Upload
75+
*/
76+
private mimetype;
77+
78+
/**
79+
* Alt Text of the file
80+
*
81+
* @private
82+
* @memberof Upload
83+
*/
84+
private altText;
85+
7086
private lastProgress: ProgressEvent = {
7187
totalBytes: 0,
7288
totalPercent: 0,
@@ -102,6 +118,16 @@ export class Upload extends EventEmitter {
102118
delete this.storeOptions.sanitizer;
103119
}
104120

121+
if (storeOptions.altText) {
122+
this.altText = storeOptions.altText;
123+
delete this.storeOptions.altText;
124+
}
125+
126+
if (storeOptions.mimetype) {
127+
this.mimetype = storeOptions.mimetype;
128+
delete this.storeOptions.mimetype;
129+
}
130+
105131
this.uploader = new S3Uploader(this.storeOptions, options.concurrency);
106132

107133
this.uploader.setRetryConfig({
@@ -199,16 +225,16 @@ export class Upload extends EventEmitter {
199225
* Upload single file
200226
*
201227
* @param {(InputFile)} file
202-
* @param {(string)} altText
203228
* @returns {Promise<any>}
204229
* @memberof Upload
205230
*/
206-
async upload(input: InputFile, altText?: string): Promise<any> {
231+
async upload(input: InputFile): Promise<any> {
207232

208-
const f = await getFile(input, this.sanitizerOptions);
233+
const f = await getFile(input, this.sanitizerOptions, this.mimetype);
209234
f.customName = this.overrideFileName;
210-
if (altText) {
211-
f.alt = altText;
235+
236+
if (this.overrideFileName) {
237+
f.alt = this.altText
212238
}
213239

214240
this.uploader.addFile(f);
@@ -240,8 +266,13 @@ export class Upload extends EventEmitter {
240266
continue;
241267
}
242268

243-
const f = await getFile(input[i], this.sanitizerOptions);
269+
const f = await getFile(input[i], this.sanitizerOptions, this.mimetype);
244270
f.customName = this.overrideFileName;
271+
272+
if (this.overrideFileName) {
273+
f.alt = this.altText
274+
}
275+
245276
this.uploader.addFile(f);
246277
}
247278

src/lib/client.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,15 +210,13 @@ describe('client', () => {
210210

211211
expect(Upload.prototype.setToken).toHaveBeenCalledWith(token);
212212
expect(Upload.prototype.setSecurity).toHaveBeenCalledWith(defaultSecurity);
213-
expect(Upload.prototype.upload).toHaveBeenCalledWith(file, undefined);
213+
expect(Upload.prototype.upload).toHaveBeenCalledWith(file);
214214
});
215215

216216
it('should be able to upload file with alt text', async () => {
217217
const client = new Client(defaultApikey);
218218
const file = 'anyFile';
219-
const uploadOptions = {
220-
altText: 'alt',
221-
};
219+
const uploadOptions = {};
222220
const storeOptions = {};
223221
const token = {};
224222

@@ -233,7 +231,7 @@ describe('client', () => {
233231

234232
expect(Upload.prototype.setToken).toHaveBeenCalledWith(token);
235233
expect(Upload.prototype.setSecurity).toHaveBeenCalledWith(defaultSecurity);
236-
expect(Upload.prototype.upload).toHaveBeenCalledWith(file, uploadOptions.altText);
234+
expect(Upload.prototype.upload).toHaveBeenCalledWith(file);
237235
});
238236

239237
it('should be able to upload file without token and security', async () => {
@@ -251,7 +249,7 @@ describe('client', () => {
251249
urls: sessionURls,
252250
});
253251

254-
expect(Upload.prototype.upload).toHaveBeenCalledWith(file, undefined);
252+
expect(Upload.prototype.upload).toHaveBeenCalledWith(file);
255253
});
256254

257255
it('should emit error', async () => {

src/lib/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ export class Client extends EventEmitter {
474474
this.emit('upload.error', e);
475475
});
476476

477-
return upload.upload(file, options && options.altText);
477+
return upload.upload(file);
478478
}
479479

480480
/**

src/schema/store.schema.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ export const StoreParamsSchema = {
2828
format: 'callback',
2929
}],
3030
},
31+
mimetype: {
32+
type: ['string', 'null'],
33+
maxLength: 60,
34+
},
35+
altText: {
36+
type: ['string', 'null'],
37+
maxLength: 125,
38+
},
3139
location: {
3240
'$ref': 'locationsDef',
3341
},

src/schema/upload.schema.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,5 @@ export const UploadParamsSchema = {
8181
maxlength: 256,
8282
},
8383
},
84-
altText: {
85-
type: ['string', 'null'],
86-
maxLength: 60,
87-
},
8884
},
8985
};

0 commit comments

Comments
 (0)