fng-jq-upload 0.12.0-beta.331 → 0.12.0-beta.333
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +9 -0
- package/.github/workflows/main.workflow.yaml +11 -8
- package/app/fng-jq-upload.js +108 -4
- package/dist/fng-jq-upload.js +109 -4
- package/dist/fng-jq-upload.min.js +1 -1
- package/lib/fng-jq-upload.js +40 -3
- package/lib/fng-jq-upload.ts +47 -0
- package/package.json +2 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
name: Run build and test
|
|
2
2
|
|
|
3
|
-
on:
|
|
3
|
+
"on":
|
|
4
4
|
push:
|
|
5
5
|
branches-ignore:
|
|
6
6
|
- 'next'
|
|
@@ -11,28 +11,31 @@ jobs:
|
|
|
11
11
|
runs-on: ubuntu-latest
|
|
12
12
|
|
|
13
13
|
steps:
|
|
14
|
-
- uses: actions/checkout@
|
|
14
|
+
- uses: actions/checkout@v6
|
|
15
15
|
|
|
16
16
|
- name: Set up Node.js
|
|
17
|
-
uses: actions/setup-node@
|
|
17
|
+
uses: actions/setup-node@v6
|
|
18
18
|
with:
|
|
19
19
|
node-version: 24.x
|
|
20
20
|
|
|
21
|
+
- name: Install ImageMagick
|
|
22
|
+
run: sudo apt-get install -y imagemagick
|
|
23
|
+
|
|
21
24
|
- name: Install Chrome
|
|
22
|
-
uses: browser-actions/setup-chrome@
|
|
25
|
+
uses: browser-actions/setup-chrome@v2
|
|
23
26
|
with:
|
|
24
27
|
chrome-version: 'latest'
|
|
25
28
|
|
|
26
29
|
- name: Start MongoDB
|
|
27
|
-
uses: supercharge/mongodb-github-action@1.
|
|
30
|
+
uses: supercharge/mongodb-github-action@1.12.1
|
|
28
31
|
with:
|
|
29
32
|
mongodb-version: 6.0
|
|
30
33
|
|
|
31
34
|
- name: Setup mongodb-tools
|
|
32
35
|
run: |
|
|
33
|
-
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2204-x86_64-100.
|
|
34
|
-
sudo apt install ./mongodb-database-tools-*-100.
|
|
35
|
-
mongorestore --version
|
|
36
|
+
wget https://fastdl.mongodb.org/tools/db/mongodb-database-tools-ubuntu2204-x86_64-100.16.1.deb
|
|
37
|
+
sudo apt install ./mongodb-database-tools-*-100.16.1.deb
|
|
38
|
+
mongorestore --version
|
|
36
39
|
|
|
37
40
|
- name: Install dependencies
|
|
38
41
|
run: npm install
|
package/app/fng-jq-upload.js
CHANGED
|
@@ -13,6 +13,11 @@
|
|
|
13
13
|
return breakdown[2];
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
// Module-scope accumulator so every fng-jq-upload directive on the same page contributes to a single
|
|
17
|
+
// batched presign request, regardless of where the directive is mounted in the scope tree.
|
|
18
|
+
// Keyed by modelName; flushed on the next tick via $timeout(0).
|
|
19
|
+
var presignBuckets = {};
|
|
20
|
+
|
|
16
21
|
app
|
|
17
22
|
.controller('FngUploadAdditFieldsCtrl', [
|
|
18
23
|
'$scope',
|
|
@@ -76,7 +81,8 @@
|
|
|
76
81
|
.controller('FngJqUploadCtrl', [
|
|
77
82
|
'$scope',
|
|
78
83
|
'$http',
|
|
79
|
-
|
|
84
|
+
'$timeout',
|
|
85
|
+
function ($scope, $http, $timeout) {
|
|
80
86
|
$scope.loadingFiles = false;
|
|
81
87
|
$scope.formScope = $scope.$parent;
|
|
82
88
|
|
|
@@ -208,6 +214,78 @@
|
|
|
208
214
|
$scope.formScope.fngJqUploadFileQueue = $scope.$$childHead.queue;
|
|
209
215
|
}
|
|
210
216
|
|
|
217
|
+
function isImageFilename(filename) {
|
|
218
|
+
if (!filename) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
const lc = filename.toLowerCase();
|
|
222
|
+
for (const ext of ['.gif', '.png', '.jpg', '.jpeg']) {
|
|
223
|
+
if (lc.endsWith(ext)) {
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Collect the queue entries that need a pre-signed URL, defer briefly so that sibling
|
|
231
|
+
// fng-jq-upload directives on the same form get to register their attachments too, then ask
|
|
232
|
+
// the server for them all in one round-trip per location. Each form has many file fields
|
|
233
|
+
// (referral file, photo, ID document, ...) and typically each holds 1 file, so without this
|
|
234
|
+
// batching the page-load cost is dominated by N small requests. $timeout(0) coalesces every
|
|
235
|
+
// register-call made during the current digest into a single $http.post per model+location.
|
|
236
|
+
function batchPresignAttachments(pending) {
|
|
237
|
+
const modelName = $scope.formScope.modelName;
|
|
238
|
+
if (!presignBuckets[modelName]) {
|
|
239
|
+
presignBuckets[modelName] = { items: [], flushScheduled: false };
|
|
240
|
+
}
|
|
241
|
+
const bucket = presignBuckets[modelName];
|
|
242
|
+
for (const item of pending) {
|
|
243
|
+
bucket.items.push(item);
|
|
244
|
+
}
|
|
245
|
+
if (bucket.flushScheduled) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
bucket.flushScheduled = true;
|
|
249
|
+
$timeout(function () {
|
|
250
|
+
const items = bucket.items;
|
|
251
|
+
bucket.items = [];
|
|
252
|
+
bucket.flushScheduled = false;
|
|
253
|
+
const byLocation = new Map();
|
|
254
|
+
for (const item of items) {
|
|
255
|
+
const group = byLocation.get(item.location) || [];
|
|
256
|
+
group.push(item);
|
|
257
|
+
byLocation.set(item.location, group);
|
|
258
|
+
}
|
|
259
|
+
byLocation.forEach((groupItems, location) => {
|
|
260
|
+
const body = {
|
|
261
|
+
files: groupItems.map((item) => ({
|
|
262
|
+
id: item.id,
|
|
263
|
+
location,
|
|
264
|
+
fn: item.filename,
|
|
265
|
+
thumbnailId: item.thumbnailId,
|
|
266
|
+
})),
|
|
267
|
+
};
|
|
268
|
+
$http.post('/api/file/presign/' + modelName, body).then((response) => {
|
|
269
|
+
const results = (response.data && response.data.files) || [];
|
|
270
|
+
const byId = {};
|
|
271
|
+
for (const r of results) {
|
|
272
|
+
byId[r.id] = r;
|
|
273
|
+
}
|
|
274
|
+
for (const item of groupItems) {
|
|
275
|
+
const result = byId[item.id];
|
|
276
|
+
if (!result) {
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
item.queueElement.url = result.url;
|
|
280
|
+
if (result.thumbnailUrl) {
|
|
281
|
+
item.queueElement.thumbnailUrl = result.thumbnailUrl;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
}, 0);
|
|
287
|
+
}
|
|
288
|
+
|
|
211
289
|
function setUpAttachments() {
|
|
212
290
|
if (!$scope.$$childHead) {
|
|
213
291
|
return;
|
|
@@ -217,24 +295,50 @@
|
|
|
217
295
|
if (!storedData) {
|
|
218
296
|
return;
|
|
219
297
|
}
|
|
298
|
+
const modelName = $scope.formScope.modelName;
|
|
299
|
+
const pendingPresign = [];
|
|
220
300
|
for (const storedElement of storedData) {
|
|
221
301
|
// location did not exist in the original schema. where no value has been saved, the file is assumed to have been
|
|
222
302
|
// saved to MongoDB - which is represeted by a value of 1
|
|
223
303
|
const location = storedElement.location === undefined ? 1 : storedElement.location;
|
|
224
304
|
const storedName = storedElement.filename;
|
|
305
|
+
const id = storedElement._id;
|
|
306
|
+
const thumbnailId = storedElement.thumbnailId;
|
|
307
|
+
const modelAndLocation = modelName + '/' + location;
|
|
308
|
+
const baseUrl = '/api/file/' + modelAndLocation + '/' + id + '?fn=' + storedName;
|
|
309
|
+
let deleteUrl = baseUrl;
|
|
310
|
+
if (thumbnailId) {
|
|
311
|
+
deleteUrl += '&thumbnailId=' + thumbnailId;
|
|
312
|
+
}
|
|
225
313
|
const queueElement = {
|
|
226
314
|
name: storedName,
|
|
227
315
|
size: storedElement.size,
|
|
228
316
|
location,
|
|
317
|
+
deleteUrl,
|
|
318
|
+
deleteType: 'DELETE',
|
|
319
|
+
thumbnailUrl: 'https://upload.wikimedia.org/wikipedia/commons/6/6c/Iconoir_journal-page.svg',
|
|
229
320
|
};
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
321
|
+
if (location > 1) {
|
|
322
|
+
// remote storage - defer URL generation to the batched presign call below
|
|
323
|
+
pendingPresign.push({ queueElement, location, id, filename: storedName, thumbnailId });
|
|
324
|
+
} else {
|
|
325
|
+
// file is stored in MongoDB - the URL is just the route, no presigning required
|
|
326
|
+
queueElement.url = baseUrl;
|
|
327
|
+
if (!$scope.options.defaultThumbnail && isImageFilename(storedName)) {
|
|
328
|
+
// for MongoDB-stored images we have a dedicated route that streams the thumbnail
|
|
329
|
+
queueElement.thumbnailUrl = thumbnailId
|
|
330
|
+
? '/api/file/' + modelAndLocation + '/' + thumbnailId
|
|
331
|
+
: '/api/file/' + modelAndLocation + '/thumbnail/' + id;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
233
334
|
if (!$scope.$$childHead.queue) {
|
|
234
335
|
$scope.$$childHead.queue = []; // shouldn't happen, but have seen in Sentry
|
|
235
336
|
}
|
|
236
337
|
$scope.$$childHead.queue.push(queueElement);
|
|
237
338
|
}
|
|
339
|
+
if (pendingPresign.length > 0) {
|
|
340
|
+
batchPresignAttachments(pendingPresign);
|
|
341
|
+
}
|
|
238
342
|
assignQueueToFormScope();
|
|
239
343
|
}
|
|
240
344
|
|
package/dist/fng-jq-upload.js
CHANGED
|
@@ -13,6 +13,11 @@
|
|
|
13
13
|
return breakdown[2];
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
+
// Module-scope accumulator so every fng-jq-upload directive on the same page contributes to a single
|
|
17
|
+
// batched presign request, regardless of where the directive is mounted in the scope tree.
|
|
18
|
+
// Keyed by modelName; flushed on the next tick via $timeout(0).
|
|
19
|
+
var presignBuckets = {};
|
|
20
|
+
|
|
16
21
|
app
|
|
17
22
|
.controller('FngUploadAdditFieldsCtrl', [
|
|
18
23
|
'$scope',
|
|
@@ -76,7 +81,8 @@
|
|
|
76
81
|
.controller('FngJqUploadCtrl', [
|
|
77
82
|
'$scope',
|
|
78
83
|
'$http',
|
|
79
|
-
|
|
84
|
+
'$timeout',
|
|
85
|
+
function ($scope, $http, $timeout) {
|
|
80
86
|
$scope.loadingFiles = false;
|
|
81
87
|
$scope.formScope = $scope.$parent;
|
|
82
88
|
|
|
@@ -127,6 +133,7 @@
|
|
|
127
133
|
var arrayIndex =
|
|
128
134
|
$scope.formScope['$_arrayOffset_' + root.replace(/\./g, '_') + '_' + $scope.options.subkeyno];
|
|
129
135
|
if (arrayIndex != null && arrayIndex !== undefined && arrayIndex !== -1) {
|
|
136
|
+
retVal[arrayIndex][lastPart] = retVal[arrayIndex][lastPart] || [];
|
|
130
137
|
retVal = retVal[arrayIndex][lastPart];
|
|
131
138
|
} else {
|
|
132
139
|
retVal = undefined;
|
|
@@ -207,6 +214,78 @@
|
|
|
207
214
|
$scope.formScope.fngJqUploadFileQueue = $scope.$$childHead.queue;
|
|
208
215
|
}
|
|
209
216
|
|
|
217
|
+
function isImageFilename(filename) {
|
|
218
|
+
if (!filename) {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
const lc = filename.toLowerCase();
|
|
222
|
+
for (const ext of ['.gif', '.png', '.jpg', '.jpeg']) {
|
|
223
|
+
if (lc.endsWith(ext)) {
|
|
224
|
+
return true;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return false;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// Collect the queue entries that need a pre-signed URL, defer briefly so that sibling
|
|
231
|
+
// fng-jq-upload directives on the same form get to register their attachments too, then ask
|
|
232
|
+
// the server for them all in one round-trip per location. Each form has many file fields
|
|
233
|
+
// (referral file, photo, ID document, ...) and typically each holds 1 file, so without this
|
|
234
|
+
// batching the page-load cost is dominated by N small requests. $timeout(0) coalesces every
|
|
235
|
+
// register-call made during the current digest into a single $http.post per model+location.
|
|
236
|
+
function batchPresignAttachments(pending) {
|
|
237
|
+
const modelName = $scope.formScope.modelName;
|
|
238
|
+
if (!presignBuckets[modelName]) {
|
|
239
|
+
presignBuckets[modelName] = { items: [], flushScheduled: false };
|
|
240
|
+
}
|
|
241
|
+
const bucket = presignBuckets[modelName];
|
|
242
|
+
for (const item of pending) {
|
|
243
|
+
bucket.items.push(item);
|
|
244
|
+
}
|
|
245
|
+
if (bucket.flushScheduled) {
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
bucket.flushScheduled = true;
|
|
249
|
+
$timeout(function () {
|
|
250
|
+
const items = bucket.items;
|
|
251
|
+
bucket.items = [];
|
|
252
|
+
bucket.flushScheduled = false;
|
|
253
|
+
const byLocation = new Map();
|
|
254
|
+
for (const item of items) {
|
|
255
|
+
const group = byLocation.get(item.location) || [];
|
|
256
|
+
group.push(item);
|
|
257
|
+
byLocation.set(item.location, group);
|
|
258
|
+
}
|
|
259
|
+
byLocation.forEach((groupItems, location) => {
|
|
260
|
+
const body = {
|
|
261
|
+
files: groupItems.map((item) => ({
|
|
262
|
+
id: item.id,
|
|
263
|
+
location,
|
|
264
|
+
fn: item.filename,
|
|
265
|
+
thumbnailId: item.thumbnailId,
|
|
266
|
+
})),
|
|
267
|
+
};
|
|
268
|
+
$http.post('/api/file/presign/' + modelName, body).then((response) => {
|
|
269
|
+
const results = (response.data && response.data.files) || [];
|
|
270
|
+
const byId = {};
|
|
271
|
+
for (const r of results) {
|
|
272
|
+
byId[r.id] = r;
|
|
273
|
+
}
|
|
274
|
+
for (const item of groupItems) {
|
|
275
|
+
const result = byId[item.id];
|
|
276
|
+
if (!result) {
|
|
277
|
+
continue;
|
|
278
|
+
}
|
|
279
|
+
item.queueElement.url = result.url;
|
|
280
|
+
if (result.thumbnailUrl) {
|
|
281
|
+
item.queueElement.thumbnailUrl = result.thumbnailUrl;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
});
|
|
286
|
+
}, 0);
|
|
287
|
+
}
|
|
288
|
+
|
|
210
289
|
function setUpAttachments() {
|
|
211
290
|
if (!$scope.$$childHead) {
|
|
212
291
|
return;
|
|
@@ -216,24 +295,50 @@
|
|
|
216
295
|
if (!storedData) {
|
|
217
296
|
return;
|
|
218
297
|
}
|
|
298
|
+
const modelName = $scope.formScope.modelName;
|
|
299
|
+
const pendingPresign = [];
|
|
219
300
|
for (const storedElement of storedData) {
|
|
220
301
|
// location did not exist in the original schema. where no value has been saved, the file is assumed to have been
|
|
221
302
|
// saved to MongoDB - which is represeted by a value of 1
|
|
222
303
|
const location = storedElement.location === undefined ? 1 : storedElement.location;
|
|
223
304
|
const storedName = storedElement.filename;
|
|
305
|
+
const id = storedElement._id;
|
|
306
|
+
const thumbnailId = storedElement.thumbnailId;
|
|
307
|
+
const modelAndLocation = modelName + '/' + location;
|
|
308
|
+
const baseUrl = '/api/file/' + modelAndLocation + '/' + id + '?fn=' + storedName;
|
|
309
|
+
let deleteUrl = baseUrl;
|
|
310
|
+
if (thumbnailId) {
|
|
311
|
+
deleteUrl += '&thumbnailId=' + thumbnailId;
|
|
312
|
+
}
|
|
224
313
|
const queueElement = {
|
|
225
314
|
name: storedName,
|
|
226
315
|
size: storedElement.size,
|
|
227
316
|
location,
|
|
317
|
+
deleteUrl,
|
|
318
|
+
deleteType: 'DELETE',
|
|
319
|
+
thumbnailUrl: 'https://upload.wikimedia.org/wikipedia/commons/6/6c/Iconoir_journal-page.svg',
|
|
228
320
|
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
321
|
+
if (location > 1) {
|
|
322
|
+
// remote storage - defer URL generation to the batched presign call below
|
|
323
|
+
pendingPresign.push({ queueElement, location, id, filename: storedName, thumbnailId });
|
|
324
|
+
} else {
|
|
325
|
+
// file is stored in MongoDB - the URL is just the route, no presigning required
|
|
326
|
+
queueElement.url = baseUrl;
|
|
327
|
+
if (!$scope.options.defaultThumbnail && isImageFilename(storedName)) {
|
|
328
|
+
// for MongoDB-stored images we have a dedicated route that streams the thumbnail
|
|
329
|
+
queueElement.thumbnailUrl = thumbnailId
|
|
330
|
+
? '/api/file/' + modelAndLocation + '/' + thumbnailId
|
|
331
|
+
: '/api/file/' + modelAndLocation + '/thumbnail/' + id;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
232
334
|
if (!$scope.$$childHead.queue) {
|
|
233
335
|
$scope.$$childHead.queue = []; // shouldn't happen, but have seen in Sentry
|
|
234
336
|
}
|
|
235
337
|
$scope.$$childHead.queue.push(queueElement);
|
|
236
338
|
}
|
|
339
|
+
if (pendingPresign.length > 0) {
|
|
340
|
+
batchPresignAttachments(pendingPresign);
|
|
341
|
+
}
|
|
237
342
|
assignQueueToFormScope();
|
|
238
343
|
}
|
|
239
344
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(){"use strict";var e=angular.module("uploadModule",["blueimp.fileupload"]);e.controller("FngUploadAdditFieldsCtrl",["$scope","$timeout",function(i,e){function t(){i.schema&&i.schema.forEach(function(e){var a=i.uploadForm.dataField()[i.file];i.record[e.name]=a?a[e.name]:void 0})}i.record={},i.$watch("record",function(e,a){e!==a&&(i.uploadForm.formScope[i.uploadForm.formScope.topLevelFormName].$setDirty(),angular.extend(i.uploadForm.dataField()[i.file],e))},!0),e(function(){var a;i.formScope.newRecord?i.record={}:a=i.formScope.$watch("phase",function(e){"ready"===e&&(t(),i.$on("fngCancel",function(){t()}),a())})})}]).directive("fngUploadAdditFields",function(){return{link:function(e,a,i){e.uploadForm=e.$parent.$parent.$parent,e.formScope=e.uploadForm.formScope,e.schema=e.uploadForm.directiveOptions.additFields,e.file=parseInt(i.file)},scope:{},controller:"FngUploadAdditFieldsCtrl",template:'<form-input formstyle="inline" schema="schema" model="record" forceform="true"></form-input>'}}).controller("FngJqUploadCtrl",["$scope","$http",function(r,i){function c(e,a){return 1<e?i.get(a).then(e=>{if(201===e.status)return e.data.clientUrl;throw e="Unexpected response to getPresignedUrl (status"+e.status+")",new Error(e)}):Promise.resolve(a)}function s(a,e,i,t,n){var l=r.formScope.modelName+"/"+e;let o="/api/file/"+l+"/"+i+"?fn="+t;if(c(e,o).then(e=>{a.url=e}),a.deleteUrl=o,n&&(a.deleteUrl+="&thumbnailId="+n),a.deleteType="DELETE",a.thumbnailUrl="https://upload.wikimedia.org/wikipedia/commons/6/6c/Iconoir_journal-page.svg",t&&!r.options.defaultThumbnail){var s=t.toLowerCase();for(const d of[".gif",".png",".jpg",".jpeg"])if(s.endsWith(d)){if(n){const o="/api/file/"+l+"/"+n;c(e,o).then(e=>{a.thumbnailUrl=e})}else a.thumbnailUrl="/api/file/"+l+"/thumbnail/"+i;break}}}function d(){r.formScope.fngJqUploadFileQueue=r.$$childHead.queue}function e(){if(r.$$childHead){r.$$childHead.queue=[];var e=r.dataField();if(e){for(const n of e){var a=void 0===n.location?1:n.location,i=n.filename,t={name:i,size:n.size,location:a};s(t,a,n._id,i,n.thumbnailId),r.$$childHead.queue||(r.$$childHead.queue=[]),r.$$childHead.queue.push(t)}d()}}}var a;r.loadingFiles=!1,r.formScope=r.$parent,r.dataField=function(e){if(-1===r.info.name.indexOf(".")){var a=r.options.model||"record",a=r.formScope[a];if(!(o=a?a[r.info.name]:o)&&e){if(!a)throw new Error(`Cannot initialise record. ${r.info.name} - record is not defined`);o=a[r.info.name]=[]}}else if(r.options.subschema){for(var e=r.formScope.record,a=r.info.name,i=r.options.subschemaroot,a=a.slice(i.length+1),t=(o=e,i.split(".")),n=0,l=t.length;n<l;n++)o=o[t[n]];var o,e=r.options.index;if(void 0!==(e=void 0===e?r.$parent.$index:e)){if(!o[e])return;o[e][a]||(o[e][a]=[]),o=o[e][a]}else r.options.subkey?o=null!=(e=r.formScope["$_arrayOffset_"+i.replace(/\./g,"_")+"_"+r.options.subkeyno])&&void 0!==e&&-1!==e?o[e][a]:void 0:console.log("No support for this yet")}else console.log("No support for this yet either");return o},r.initialiseJqUpload=function(){e(),r.$on("fngCancel",function(){e()})},r.formScope.newRecord||(a=r.formScope.$watch("phase",function(e){"ready"===e&&(r.initialiseJqUpload(),a())})),r.$on("jqUpload:reinitialise",function(){r.initialiseJqUpload()}),r.$on("fileuploadstart",function(){delete r.uploadError}),r.$on("fngCancel",()=>{var e=r.$$childHead;e&&"function"==typeof e.active&&e.active()&&"function"==typeof e.cancel&&e.cancel()}),r.$on("fileuploadfail",function(e,a){r.$$childHead.queue.pop();let i;if(a.xhr){var t,n=a.xhr();try{t=JSON.parse(n.responseText),i=t.error||t.message||t}catch(e){i=n.responseText}}"abort"!==(i=i||a.errorThrown||"an unexpected error occurred")&&(r.uploadError=i),i.includes("ENOTFOUND")&&(i="Failed to connect to storage service to upload file. Have you gone offline?")}),r.$on("fileuploaddone",function(e,a){var i=r.dataField(!0),a=a.result.files[0],t=a.id,n=a.name,l=a.location,o=a.thumbnailId,i=(i.push({_id:t,filename:n,size:a.size,location:l,thumbnailId:o}),r.$$childHead.queue);s(i[i.length-1],l,t,n,o),r.ngModel.$setDirty(),d()})}]).directive("fngJqUploadForm",["PluginHelperService","$rootScope",function(n,l){return{link:function(e,a,i,t){angular.extend(e,n.extractFromAttr(i,"fngJqUploadForm",e.formScope,{setUpDynamicLabelFunc:!0})),e.label=e.formScope.label,e.passedParams=e.formScope[i.schema],angular.extend(e.options,e.passedParams.fngJqUploadForm),e.options.additFields&&(e.directiveOptions.additFields=JSON.parse(e.options.additFields)),e.directiveOptions.url="/api/file/upload/"+e.formScope.modelName+"/"+encodeURIComponent(e.info.name),e.directiveOptions.autoUpload=e.directiveOptions.autoupload,e.passedParams.readonly?e.isDisabled=!0:e.isDisabled="function"==typeof l.isSecurelyDisabled&&l.isSecurelyDisabled(e.info.id),e.name=e.passedParams.name,e.ngModel=t},restrict:"E",require:"?ngModel",templateUrl:"templates/fileform.html",scope:{},controller:"FngJqUploadCtrl"}}]).controller("FileDestroyController",["$scope","$http",function(l,a){function t(e){a=e.deleteUrl;for(var a,i=/\/file\/(.+?)\/([0-9a-f]{24})/.exec(a)[2],t=l.uploadScope.dataField(),n=t.length-1;0<=n;n--)t[n]._id===i&&t.splice(n,1);l.clear(e),l.ngModel.$setDirty()}var n,o=l.file;l.uploadScope=l.$parent.$parent.$parent;o.deleteUrl?(o.$state=function(){return n},o.$destroy=function(e){if(!e||!e.target.className.includes("ng-hide")){const i=l.$parent.$parent;return i.mouseIn=0,n="pending",a({url:o.deleteUrl,method:o.deleteType}).then(()=>{n="resolved",t(o)}).catch(e=>{n="rejected";var a=e.data||e,a="string"==typeof a?a:a.message||JSON.stringify(a);403===e.status?o.error=a:(t(o),i.$parent.uploadError=a)})}}):o.$cancel||o._index||(o.$cancel=function(){t(o)})}])}(),angular.module("uploadModule").run(["$templateCache",function(e){"use strict";e.put("templates/fileform.html",'<div class="form-group control-group" id=cg_f_{{info.name}} {{info.add}}><label for=f_{{info.name}} id={{info.name}}-label class="col-sm-3 control-label">{{label ? label() : (info.label || (info.name | titleCase))}}</label><div class="bs3-input col-sm-9 row-fluid"><div class="col-xs-12 span12 fileupload-form"><div class=fileupload method=POST enctype=multipart/form-data data-ng-app=demo data-file-upload=directiveOptions data-ng-class="{\'fileupload-processing\': processing() || loadingFiles}"><div class=controls><div class="row fileupload-buttonbar"><div class="col-md-12 span7" style="min-height: 60px;"><div data-ng-show="directiveOptions.single && queue.length === 0" class=pull-left><span class="btn btn-success fileinput-button" ng-class="{disabled: isDisabled}"><i class="glyphicon glyphicon-plus icon icon-plus"></i> <span>Upload {{label ? label() : (info.label || (info.name | titleCase))}}</span> <input type=file name=files ng-disabled=isDisabled placeholder="Upload {{ label ? label() : (info.label || (info.name | titleCase))}}"></span></div><div data-ng-show=directiveOptions.single class=pull-left><div class=preview ng-show=!!queue[0].thumbnailUrl><a data-ng-href={{queue[0].url}} title={{queue[0].name}} download={{queue[0].name}} target=_blank data-gallery ng-mouseover="mouseIn=1" ng-mouseout="mouseIn=0"><img data-ng-src={{queue[0].thumbnailUrl}} alt="" width="{{ options.width || 80 }}" height="{{ options.height || 60 }}"></a></div></div><div data-ng-show="directiveOptions.single && !active()" class="pull-left file-delete-div"><button ng-show="mouseIn && !isDisabled" class="file-delete overlay-btn" aria-label=Delete ng-mouseover="mouseIn=1" data-ng-click=queue[0].$destroy($event) title="Delete File"><i class="glyphicon glyphicon-trash icon icon-trash"></i></button> <span class="error text-danger single-delete-err" data-ng-show=queue[0].error>{{ queue[0].error }}</span></div><span data-ng-hide=directiveOptions.single><span class="btn btn-success fileinput-button" ng-class="{disabled: isDisabled}"><i class="glyphicon glyphicon-plus icon icon-plus"></i> <span>Add files...</span> <input type=file name=files multiple ng-disabled=isDisabled placeholder="Upload {{info.label || (info.name | titleCase)}}"> </span></span><button type=button data-ng-hide=directiveOptions.autoupload class="btn btn-primary start" data-ng-click=submit()><i class="glyphicon glyphicon-upload icon icon-upload"></i> <span>Start upload</span></button> <button data-ng-show=active() type=button class="btn btn-warning cancel" data-ng-click=cancel()><i class="glyphicon glyphicon-ban icon-circle icon-ban-circle"></i> <span>Cancel upload</span></button><div data-ng-show=uploadError style="padding-top: 5px;"><span class="error text-danger">{{ uploadError }}</span></div><span class=fileupload-process></span></div><div class="col-md-5 span5 fade" data-ng-class="{in: active()}"><div class="progress progress-striped active" data-file-upload-progress=progress()><div class="progress-bar progress-bar-success" data-ng-style="{width: num + \'%\'}"></div></div><div class=progress-extended> </div></div></div><table data-ng-hide=directiveOptions.single class="table table-striped files ng-cloak"><tr data-ng-repeat="file in queue"><td data-ng-switch data-on=!!file.thumbnailUrl><div class=preview data-ng-switch-when=true><a data-ng-href={{file.url}} title={{file.name}} download={{file.name}} target=_blank data-gallery><img data-ng-src={{file.thumbnailUrl}} alt="" width=80 height=60></a></div><div class=preview data-ng-switch-default data-file-upload-preview=file></div></td><td><p class=name data-ng-switch data-on=!!file.url><span data-ng-switch-when=true data-ng-switch data-on=!!file.thumbnailUrl><a data-ng-switch-when=true data-ng-href={{file.url}} title={{file.name}} download={{file.name}} target=_blank data-gallery>{{file.name}}</a> <a data-ng-switch-default data-ng-href={{file.url}} title={{file.name}} download={{file.name}}>{{file.name}}</a> </span><span data-ng-switch-default>{{file.name}}</span></p><span class="error text-danger" data-ng-show=file.error>{{ file.error }}</span></td><td><p class=size>{{file.size | formatFileSize}}</p><div class="progress progress-striped active fade" data-ng-class="{pending: \'in\'}[file.$state()]" data-file-upload-progress=file.$progress()><div class="progress-bar progress-bar-success" data-ng-style="{width: num + \'%\'}"></div></div></td><td ng-show=directiveOptions.additFields><fng-upload-addit-fields file={{$index}}></fng-upload-addit-fields></td><td><button type=button class="btn btn-primary start" data-ng-click=file.$submit() data-ng-hide="!file.$submit || directiveOptions.autoupload" data-ng-disabled="file.$state() == \'pending\' || file.$state() == \'rejected\'"><i class="glyphicon glyphicon-upload icon icon-upload"></i> <span>Start</span></button> <button type=button class="btn btn-warning cancel" data-ng-click=file.$cancel() data-ng-hide=!file.$cancel><i class="glyphicon glyphicon-ban-circle icon icon-ban-circle"></i> <span>Cancel</span></button> <button type=button class="btn btn-danger destroy" data-ng-click=file.$destroy() data-ng-hide="!file.$destroy || isDisabled" data-ng-controller=FileDestroyController><i class="glyphicon glyphicon-trash icon icon-trash"></i> <span>Delete</span></button></td></tr></table></div></div></div></div></div>')}]);
|
|
1
|
+
(()=>{var e=angular.module("uploadModule",["blueimp.fileupload"]),b={};e.controller("FngUploadAdditFieldsCtrl",["$scope","$timeout",function(i,e){function l(){i.schema&&i.schema.forEach(function(e){var a=i.uploadForm.dataField()[i.file];i.record[e.name]=a?a[e.name]:void 0})}i.record={},i.$watch("record",function(e,a){e!==a&&(i.uploadForm.formScope[i.uploadForm.formScope.topLevelFormName].$setDirty(),angular.extend(i.uploadForm.dataField()[i.file],e))},!0),e(function(){var a;i.formScope.newRecord?i.record={}:a=i.formScope.$watch("phase",function(e){"ready"===e&&(l(),i.$on("fngCancel",function(){l()}),a())})})}]).directive("fngUploadAdditFields",function(){return{link:function(e,a,i){e.uploadForm=e.$parent.$parent.$parent,e.formScope=e.uploadForm.formScope,e.schema=e.uploadForm.directiveOptions.additFields,e.file=parseInt(i.file)},scope:{},controller:"FngUploadAdditFieldsCtrl",template:'<form-input formstyle="inline" schema="schema" model="record" forceform="true"></form-input>'}}).controller("FngJqUploadCtrl",["$scope","$http","$timeout",function(m,u,f){function g(e,a){return 1<e?u.get(a).then(e=>{if(201===e.status)return e.data.clientUrl;throw e="Unexpected response to getPresignedUrl (status"+e.status+")",new Error(e)}):Promise.resolve(a)}function h(){m.formScope.fngJqUploadFileQueue=m.$$childHead.queue}function e(){if(m.$$childHead){m.$$childHead.queue=[];var e=m.dataField();if(e){var a,i=m.formScope.modelName,l=[];for(a of e){var t=void 0===a.location?1:a.location,n=a.filename,o=a._id,s=a.thumbnailId,r=i+"/"+t,d="/api/file/"+r+"/"+o+"?fn="+n;let e=d;s&&(e+="&thumbnailId="+s);var c={name:n,size:a.size,location:t,deleteUrl:e,deleteType:"DELETE",thumbnailUrl:"https://upload.wikimedia.org/wikipedia/commons/6/6c/Iconoir_journal-page.svg"};1<t?l.push({queueElement:c,location:t,id:o,filename:n,thumbnailId:s}):(c.url=d,!m.options.defaultThumbnail&&(e=>{if(e){var a,i=e.toLowerCase();for(a of[".gif",".png",".jpg",".jpeg"])if(i.endsWith(a))return 1}})(n)&&(c.thumbnailUrl=s?"/api/file/"+r+"/"+s:"/api/file/"+r+"/thumbnail/"+o)),m.$$childHead.queue||(m.$$childHead.queue=[]),m.$$childHead.queue.push(c)}if(0<l.length){e=l;let t=m.formScope.modelName,n=(b[t]||(b[t]={items:[],flushScheduled:!1}),b[t]);for(var p of e)n.items.push(p);n.flushScheduled||(n.flushScheduled=!0,f(function(){var e,a=n.items,i=(n.items=[],n.flushScheduled=!1,new Map);for(e of a){var l=i.get(e.location)||[];l.push(e),i.set(e.location,l)}i.forEach((n,a)=>{var e={files:n.map(e=>({id:e.id,location:a,fn:e.filename,thumbnailId:e.thumbnailId}))};u.post("/api/file/presign/"+t,e).then(e=>{var a,i,l={};for(a of e.data&&e.data.files||[])l[a.id]=a;for(i of n){var t=l[i.id];t&&(i.queueElement.url=t.url,t.thumbnailUrl)&&(i.queueElement.thumbnailUrl=t.thumbnailUrl)}})})},0))}h()}}}var a;m.loadingFiles=!1,m.formScope=m.$parent,m.dataField=function(e){if(-1===m.info.name.indexOf(".")){var a=m.options.model||"record",a=m.formScope[a];if(!(o=a?a[m.info.name]:o)&&e){if(!a)throw new Error(`Cannot initialise record. ${m.info.name} - record is not defined`);o=a[m.info.name]=[]}}else if(m.options.subschema){for(var e=m.formScope.record,a=m.info.name,i=m.options.subschemaroot,a=a.slice(i.length+1),l=(o=e,i.split(".")),t=0,n=l.length;t<n;t++)o=o[l[t]];var o,e=m.options.index;if(void 0!==(e=void 0===e?m.$parent.$index:e)){if(!o[e])return;o[e][a]||(o[e][a]=[]),o=o[e][a]}else m.options.subkey?o=null!=(e=m.formScope["$_arrayOffset_"+i.replace(/\./g,"_")+"_"+m.options.subkeyno])&&void 0!==e&&-1!==e?(o[e][a]=o[e][a]||[],o[e][a]):void 0:console.log("No support for this yet")}else console.log("No support for this yet either");return o},m.initialiseJqUpload=function(){e(),m.$on("fngCancel",function(){e()})},m.formScope.newRecord||(a=m.formScope.$watch("phase",function(e){"ready"===e&&(m.initialiseJqUpload(),a())})),m.$on("jqUpload:reinitialise",function(){m.initialiseJqUpload()}),m.$on("fileuploadstart",function(){delete m.uploadError}),m.$on("fngCancel",()=>{var e=m.$$childHead;e&&"function"==typeof e.active&&e.active()&&"function"==typeof e.cancel&&e.cancel()}),m.$on("fileuploadfail",function(e,a){m.$$childHead.queue.pop();let i;if(a.xhr){var l,t=a.xhr();try{l=JSON.parse(t.responseText),i=l.error||l.message||l}catch(e){i=t.responseText}}"abort"!==(i=i||a.errorThrown||"an unexpected error occurred")&&(m.uploadError=i),i.includes("ENOTFOUND")&&(i="Failed to connect to storage service to upload file. Have you gone offline?")}),m.$on("fileuploaddone",function(e,a){var i=m.dataField(!0),a=a.result.files[0],l=a.id,t=a.name,n=a.location,o=a.thumbnailId,i=(i.push({_id:l,filename:t,size:a.size,location:n,thumbnailId:o}),m.$$childHead.queue);{var s=i[i.length-1],r=n,d=l,c=(a=t,o),p=m.formScope.modelName+"/"+r;let e="/api/file/"+p+"/"+d+"?fn="+a;if(g(r,e).then(e=>{s.url=e}),s.deleteUrl=e,c&&(s.deleteUrl+="&thumbnailId="+c),s.deleteType="DELETE",s.thumbnailUrl="https://upload.wikimedia.org/wikipedia/commons/6/6c/Iconoir_journal-page.svg",a&&!m.options.defaultThumbnail){var u,f=a.toLowerCase();for(u of[".gif",".png",".jpg",".jpeg"])if(f.endsWith(u)){if(c){let e="/api/file/"+p+"/"+c;g(r,e).then(e=>{s.thumbnailUrl=e})}else s.thumbnailUrl="/api/file/"+p+"/thumbnail/"+d;break}}}m.ngModel.$setDirty(),h()})}]).directive("fngJqUploadForm",["PluginHelperService","$rootScope",function(t,n){return{link:function(e,a,i,l){angular.extend(e,t.extractFromAttr(i,"fngJqUploadForm",e.formScope,{setUpDynamicLabelFunc:!0})),e.label=e.formScope.label,e.passedParams=e.formScope[i.schema],angular.extend(e.options,e.passedParams.fngJqUploadForm),e.options.additFields&&(e.directiveOptions.additFields=JSON.parse(e.options.additFields)),e.directiveOptions.url="/api/file/upload/"+e.formScope.modelName+"/"+encodeURIComponent(e.info.name),e.directiveOptions.autoUpload=e.directiveOptions.autoupload,e.passedParams.readonly?e.isDisabled=!0:e.isDisabled="function"==typeof n.isSecurelyDisabled&&n.isSecurelyDisabled(e.info.id),e.name=e.passedParams.name,e.ngModel=l},restrict:"E",require:"?ngModel",templateUrl:"templates/fileform.html",scope:{},controller:"FngJqUploadCtrl"}}]).controller("FileDestroyController",["$scope","$http",function(n,a){function l(e){a=e.deleteUrl;for(var a,i=/\/file\/(.+?)\/([0-9a-f]{24})/.exec(a)[2],l=n.uploadScope.dataField(),t=l.length-1;0<=t;t--)l[t]._id===i&&l.splice(t,1);n.clear(e),n.ngModel.$setDirty()}var t,o=n.file;n.uploadScope=n.$parent.$parent.$parent;o.deleteUrl?(o.$state=function(){return t},o.$destroy=function(e){if(!e||!e.target.className.includes("ng-hide")){let i=n.$parent.$parent;return i.mouseIn=0,t="pending",a({url:o.deleteUrl,method:o.deleteType}).then(()=>{t="resolved",l(o)}).catch(e=>{t="rejected";var a=e.data||e,a="string"==typeof a?a:a.message||JSON.stringify(a);403===e.status?o.error=a:(l(o),i.$parent.uploadError=a)})}}):o.$cancel||o._index||(o.$cancel=function(){l(o)})}])})(),angular.module("uploadModule").run(["$templateCache",function(e){e.put("templates/fileform.html",'<div class="form-group control-group" id=cg_f_{{info.name}} {{info.add}}><label for=f_{{info.name}} id={{info.name}}-label class="col-sm-3 control-label">{{label ? label() : (info.label || (info.name | titleCase))}}</label><div class="bs3-input col-sm-9 row-fluid"><div class="col-xs-12 span12 fileupload-form"><div class=fileupload method=POST enctype=multipart/form-data data-ng-app=demo data-file-upload=directiveOptions data-ng-class="{\'fileupload-processing\': processing() || loadingFiles}"><div class=controls><div class="row fileupload-buttonbar"><div class="col-md-12 span7" style="min-height: 60px;"><div data-ng-show="directiveOptions.single && queue.length === 0" class=pull-left><span class="btn btn-success fileinput-button" ng-class="{disabled: isDisabled}"><i class="glyphicon glyphicon-plus icon icon-plus"></i> <span>Upload {{label ? label() : (info.label || (info.name | titleCase))}}</span> <input type=file name=files ng-disabled=isDisabled placeholder="Upload {{ label ? label() : (info.label || (info.name | titleCase))}}"></span></div><div data-ng-show=directiveOptions.single class=pull-left><div class=preview ng-show=!!queue[0].thumbnailUrl><a data-ng-href={{queue[0].url}} title={{queue[0].name}} download={{queue[0].name}} target=_blank data-gallery ng-mouseover="mouseIn=1" ng-mouseout="mouseIn=0"><img data-ng-src={{queue[0].thumbnailUrl}} alt="" width="{{ options.width || 80 }}" height="{{ options.height || 60 }}"></a></div></div><div data-ng-show="directiveOptions.single && !active()" class="pull-left file-delete-div"><button ng-show="mouseIn && !isDisabled" class="file-delete overlay-btn" aria-label=Delete ng-mouseover="mouseIn=1" data-ng-click=queue[0].$destroy($event) title="Delete File"><i class="glyphicon glyphicon-trash icon icon-trash"></i></button> <span class="error text-danger single-delete-err" data-ng-show=queue[0].error>{{ queue[0].error }}</span></div><span data-ng-hide=directiveOptions.single><span class="btn btn-success fileinput-button" ng-class="{disabled: isDisabled}"><i class="glyphicon glyphicon-plus icon icon-plus"></i> <span>Add files...</span> <input type=file name=files multiple ng-disabled=isDisabled placeholder="Upload {{info.label || (info.name | titleCase)}}"> </span></span><button type=button data-ng-hide=directiveOptions.autoupload class="btn btn-primary start" data-ng-click=submit()><i class="glyphicon glyphicon-upload icon icon-upload"></i> <span>Start upload</span></button> <button data-ng-show=active() type=button class="btn btn-warning cancel" data-ng-click=cancel()><i class="glyphicon glyphicon-ban icon-circle icon-ban-circle"></i> <span>Cancel upload</span></button><div data-ng-show=uploadError style="padding-top: 5px;"><span class="error text-danger">{{ uploadError }}</span></div><span class=fileupload-process></span></div><div class="col-md-5 span5 fade" data-ng-class="{in: active()}"><div class="progress progress-striped active" data-file-upload-progress=progress()><div class="progress-bar progress-bar-success" data-ng-style="{width: num + \'%\'}"></div></div><div class=progress-extended> </div></div></div><table data-ng-hide=directiveOptions.single class="table table-striped files ng-cloak"><tr data-ng-repeat="file in queue"><td data-ng-switch data-on=!!file.thumbnailUrl><div class=preview data-ng-switch-when=true><a data-ng-href={{file.url}} title={{file.name}} download={{file.name}} target=_blank data-gallery><img data-ng-src={{file.thumbnailUrl}} alt="" width=80 height=60></a></div><div class=preview data-ng-switch-default data-file-upload-preview=file></div></td><td><p class=name data-ng-switch data-on=!!file.url><span data-ng-switch-when=true data-ng-switch data-on=!!file.thumbnailUrl><a data-ng-switch-when=true data-ng-href={{file.url}} title={{file.name}} download={{file.name}} target=_blank data-gallery>{{file.name}}</a> <a data-ng-switch-default data-ng-href={{file.url}} title={{file.name}} download={{file.name}}>{{file.name}}</a> </span><span data-ng-switch-default>{{file.name}}</span></p><span class="error text-danger" data-ng-show=file.error>{{ file.error }}</span></td><td><p class=size>{{file.size | formatFileSize}}</p><div class="progress progress-striped active fade" data-ng-class="{pending: \'in\'}[file.$state()]" data-file-upload-progress=file.$progress()><div class="progress-bar progress-bar-success" data-ng-style="{width: num + \'%\'}"></div></div></td><td ng-show=directiveOptions.additFields><fng-upload-addit-fields file={{$index}}></fng-upload-addit-fields></td><td><button type=button class="btn btn-primary start" data-ng-click=file.$submit() data-ng-hide="!file.$submit || directiveOptions.autoupload" data-ng-disabled="file.$state() == \'pending\' || file.$state() == \'rejected\'"><i class="glyphicon glyphicon-upload icon icon-upload"></i> <span>Start</span></button> <button type=button class="btn btn-warning cancel" data-ng-click=file.$cancel() data-ng-hide=!file.$cancel><i class="glyphicon glyphicon-ban-circle icon icon-ban-circle"></i> <span>Cancel</span></button> <button type=button class="btn btn-danger destroy" data-ng-click=file.$destroy() data-ng-hide="!file.$destroy || isDisabled" data-ng-controller=FileDestroyController><i class="glyphicon glyphicon-trash icon icon-trash"></i> <span>Delete</span></button></td></tr></table></div></div></div></div></div>')}]);
|
package/lib/fng-jq-upload.js
CHANGED
|
@@ -312,6 +312,43 @@ export function controller(fng, processArgs, options) {
|
|
|
312
312
|
});
|
|
313
313
|
},
|
|
314
314
|
]));
|
|
315
|
+
// batch route for retrieving signed URLs for many previously-uploaded files in a single request.
|
|
316
|
+
// the body is { files: [{ id, location, fn?, thumbnailId? }, ...] } and the response is
|
|
317
|
+
// { files: [{ id, url, thumbnailUrl? }, ...] }. this exists so that the page loading a record with
|
|
318
|
+
// lots of attachments doesn't have to make a separate round-trip per file.
|
|
319
|
+
fng.app.post.apply(fng.app, processArgs(modifiedFngOpts, [
|
|
320
|
+
"file/presign/:model",
|
|
321
|
+
function (req, res) {
|
|
322
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
323
|
+
var items, result, e_2;
|
|
324
|
+
var _b;
|
|
325
|
+
return __generator(this, function (_c) {
|
|
326
|
+
switch (_c.label) {
|
|
327
|
+
case 0:
|
|
328
|
+
_c.trys.push([0, 2, , 3]);
|
|
329
|
+
if (!options.presignBatchDelegate) {
|
|
330
|
+
throw new Error("A presignBatchDelegate is required to use the batch presign route");
|
|
331
|
+
}
|
|
332
|
+
items = (_b = req.body) === null || _b === void 0 ? void 0 : _b.files;
|
|
333
|
+
if (!Array.isArray(items)) {
|
|
334
|
+
res.status(400).send({ error: "Request body must include a 'files' array" });
|
|
335
|
+
return [2 /*return*/];
|
|
336
|
+
}
|
|
337
|
+
return [4 /*yield*/, options.presignBatchDelegate(req, req.params.model, items)];
|
|
338
|
+
case 1:
|
|
339
|
+
result = _c.sent();
|
|
340
|
+
res.send({ files: result });
|
|
341
|
+
return [3 /*break*/, 3];
|
|
342
|
+
case 2:
|
|
343
|
+
e_2 = _c.sent();
|
|
344
|
+
res.status(500).send({ error: e_2.message || "An unexpected error occurred" });
|
|
345
|
+
return [3 /*break*/, 3];
|
|
346
|
+
case 3: return [2 /*return*/];
|
|
347
|
+
}
|
|
348
|
+
});
|
|
349
|
+
});
|
|
350
|
+
},
|
|
351
|
+
]));
|
|
315
352
|
// the route used when retrieving a previously-uploaded file
|
|
316
353
|
fng.app.get.apply(fng.app, processArgs(modifiedFngOpts, [
|
|
317
354
|
"file/:model/:location/:fileId",
|
|
@@ -353,7 +390,7 @@ export function controller(fng, processArgs, options) {
|
|
|
353
390
|
"file/:model/:location/:id",
|
|
354
391
|
function (req, res) {
|
|
355
392
|
return __awaiter(this, void 0, void 0, function () {
|
|
356
|
-
var location_3, fileId, mongo, model, resource, gridFSBucket, thumbnailId, collection, obj, _b,
|
|
393
|
+
var location_3, fileId, mongo, model, resource, gridFSBucket, thumbnailId, collection, obj, _b, e_3;
|
|
357
394
|
return __generator(this, function (_c) {
|
|
358
395
|
switch (_c.label) {
|
|
359
396
|
case 0:
|
|
@@ -405,8 +442,8 @@ export function controller(fng, processArgs, options) {
|
|
|
405
442
|
res.sendStatus(200);
|
|
406
443
|
return [3 /*break*/, 11];
|
|
407
444
|
case 10:
|
|
408
|
-
|
|
409
|
-
res.status(500).send(
|
|
445
|
+
e_3 = _c.sent();
|
|
446
|
+
res.status(500).send(e_3.message);
|
|
410
447
|
return [3 /*break*/, 11];
|
|
411
448
|
case 11: return [2 /*return*/];
|
|
412
449
|
}
|
package/lib/fng-jq-upload.ts
CHANGED
|
@@ -43,6 +43,25 @@ export type StorageDelegate = (
|
|
|
43
43
|
|
|
44
44
|
export type RetrievalDelegate = (req: express.Request, fileId: string, location: number, res: express.Response) => void;
|
|
45
45
|
|
|
46
|
+
export interface IPresignBatchRequestItem {
|
|
47
|
+
id: string;
|
|
48
|
+
location: number;
|
|
49
|
+
fn?: string;
|
|
50
|
+
thumbnailId?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface IPresignBatchResponseItem {
|
|
54
|
+
id: string;
|
|
55
|
+
url: string;
|
|
56
|
+
thumbnailUrl?: string;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type PresignBatchDelegate = (
|
|
60
|
+
req: express.Request,
|
|
61
|
+
modelName: string,
|
|
62
|
+
files: IPresignBatchRequestItem[]
|
|
63
|
+
) => Promise<IPresignBatchResponseItem[]>;
|
|
64
|
+
|
|
46
65
|
export type DeletionDelegate = (req: express.Request, fileId: string, location: number) => void;
|
|
47
66
|
|
|
48
67
|
export interface JqUploadOptions {
|
|
@@ -51,6 +70,7 @@ export interface JqUploadOptions {
|
|
|
51
70
|
storageDelegate?: StorageDelegate;
|
|
52
71
|
storageLimitsDelegate?: StorageLimitsDelegate;
|
|
53
72
|
retrievalDelegate?: RetrievalDelegate;
|
|
73
|
+
presignBatchDelegate?: PresignBatchDelegate;
|
|
54
74
|
deletionDelegate: DeletionDelegate;
|
|
55
75
|
}
|
|
56
76
|
|
|
@@ -342,6 +362,33 @@ export function controller(
|
|
|
342
362
|
])
|
|
343
363
|
);
|
|
344
364
|
|
|
365
|
+
// batch route for retrieving signed URLs for many previously-uploaded files in a single request.
|
|
366
|
+
// the body is { files: [{ id, location, fn?, thumbnailId? }, ...] } and the response is
|
|
367
|
+
// { files: [{ id, url, thumbnailUrl? }, ...] }. this exists so that the page loading a record with
|
|
368
|
+
// lots of attachments doesn't have to make a separate round-trip per file.
|
|
369
|
+
fng.app.post.apply(
|
|
370
|
+
fng.app,
|
|
371
|
+
processArgs(modifiedFngOpts, [
|
|
372
|
+
"file/presign/:model",
|
|
373
|
+
async function (req: express.Request, res: express.Response) {
|
|
374
|
+
try {
|
|
375
|
+
if (!options.presignBatchDelegate) {
|
|
376
|
+
throw new Error("A presignBatchDelegate is required to use the batch presign route");
|
|
377
|
+
}
|
|
378
|
+
const items: IPresignBatchRequestItem[] = req.body?.files;
|
|
379
|
+
if (!Array.isArray(items)) {
|
|
380
|
+
res.status(400).send({ error: "Request body must include a 'files' array" });
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
const result = await options.presignBatchDelegate(req, req.params.model, items);
|
|
384
|
+
res.send({ files: result });
|
|
385
|
+
} catch (e) {
|
|
386
|
+
res.status(500).send({ error: e.message || "An unexpected error occurred" });
|
|
387
|
+
}
|
|
388
|
+
},
|
|
389
|
+
])
|
|
390
|
+
);
|
|
391
|
+
|
|
345
392
|
// the route used when retrieving a previously-uploaded file
|
|
346
393
|
fng.app.get.apply(
|
|
347
394
|
fng.app,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fng-jq-upload",
|
|
3
|
-
"version": "0.12.0-beta.
|
|
3
|
+
"version": "0.12.0-beta.333",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Mark Chapman",
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"async": "^3.2.5",
|
|
33
33
|
"body-parser": "^1.20.2",
|
|
34
34
|
"express": "^4.19.2",
|
|
35
|
-
"forms-angular": "0.12.0-beta.
|
|
35
|
+
"forms-angular": "0.12.0-beta.333",
|
|
36
36
|
"grunt": "^1.6.1",
|
|
37
37
|
"grunt-angular-templates": "^1.2.0",
|
|
38
38
|
"grunt-cli": "^1.5.0",
|