@cocreate/file 1.17.4 → 1.17.5

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/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## [1.17.5](https://github.com/CoCreate-app/CoCreate-file/compare/v1.17.4...v1.17.5) (2024-06-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * getFileId uses pathname as id ([be330a5](https://github.com/CoCreate-app/CoCreate-file/commit/be330a54324ae878a8afadc68bdae1684e944b45))
7
+ * observer target selector ([50dc031](https://github.com/CoCreate-app/CoCreate-file/commit/50dc0318b7c9c02c2d610fe86158d47e6277e667))
8
+ * replace selected file if multiple false ([d419dc3](https://github.com/CoCreate-app/CoCreate-file/commit/d419dc38f223d1d809bcf014dd2fadb0a629fae7))
9
+
1
10
  ## [1.17.4](https://github.com/CoCreate-app/CoCreate-file/compare/v1.17.3...v1.17.4) (2024-06-12)
2
11
 
3
12
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file",
3
- "version": "1.17.4",
3
+ "version": "1.17.5",
4
4
  "description": "A versatile, configurable headless file uploader supporting local and server operations. Accessible via a JavaScript API and HTML5 attributes, it provides seamless file reading, writing, and uploading with fallbacks to the standard HTML5 file input API. Ideal for developers needing robust file management in headless environments.",
5
5
  "keywords": [
6
6
  "file-uploader",
package/src/client.js CHANGED
@@ -108,12 +108,21 @@ async function init(elements) {
108
108
 
109
109
  async function fileEvent(event) {
110
110
  try {
111
- const input = event.target;
111
+ const input = event.currentTarget;
112
+ let multiple = input.multiple
113
+ if (!multiple) {
114
+ multiple = input.getAttribute('multiple');
115
+ if (multiple !== null && multiple !== "false") {
116
+ multiple = true;
117
+ } else {
118
+ multiple = false;
119
+ }
120
+ }
121
+
112
122
  let selected = inputs.get(input) || new Map()
113
123
  let files = input.files;
114
124
  if (!files || !files.length) {
115
125
  event.preventDefault()
116
- const multiple = input.multiple
117
126
  if (input.hasAttribute('directory')) {
118
127
  let handle = await window.showDirectoryPicker();
119
128
  let file = {
@@ -130,6 +139,14 @@ async function fileEvent(event) {
130
139
  }
131
140
 
132
141
  file.handle = handle
142
+
143
+ if (!multiple) {
144
+ for (let [id] of selected) {
145
+ Files.delete(id);
146
+ }
147
+ selected.clear();
148
+ }
149
+
133
150
  selected.set(file.id, file)
134
151
  Files.set(file.id, file)
135
152
 
@@ -169,6 +186,13 @@ async function fileEvent(event) {
169
186
  console.log('Duplicate file has been selected. This could be in error as the browser does not provide a clear way of checking duplictaes')
170
187
  }
171
188
 
189
+ if (!multiple) {
190
+ for (let [id] of selected) {
191
+ Files.delete(id);
192
+ }
193
+ selected.clear();
194
+ }
195
+
172
196
  selected.set(files[i].id, files[i])
173
197
  Files.set(files[i].id, files[i])
174
198
  }
@@ -220,14 +244,11 @@ async function getDirectoryHandles(handle, name) {
220
244
 
221
245
  async function getFileId(file) {
222
246
 
223
- if (file.id = file.path || file.webkitRelativePath) {
247
+ if (file.id = file.pathname) {
224
248
  return file.id;
225
249
  } else {
226
- const { name, size, type, lastModified } = file;
227
- const key = `${name}${size}${type}${lastModified}`;
228
-
229
- file.id = key
230
- return key;
250
+ file.id = `${file.name}${file.size}${file.type}${file.lastModified}`;
251
+ return file.id;
231
252
  }
232
253
  }
233
254
 
@@ -534,7 +555,7 @@ async function upload(element, data) {
534
555
  }
535
556
 
536
557
  // Append the first segment to start
537
- appendSegmfent(0);
558
+ appendSegment(0);
538
559
  });
539
560
 
540
561
 
@@ -877,7 +898,7 @@ async function Delete(file) {
877
898
  Observer.init({
878
899
  name: 'CoCreateFileAddedNodes',
879
900
  observe: ['addedNodes'],
880
- target: 'input[type="file"]',
901
+ target: '[type="file"]',
881
902
  callback: mutation => init(mutation.target)
882
903
 
883
904
  });
@@ -886,13 +907,13 @@ Observer.init({
886
907
  name: 'CoCreateFileAttributes',
887
908
  observe: ['attributes'],
888
909
  attributeName: ['type'],
889
- target: 'input[type="file"]',
910
+ target: '[type="file"]',
890
911
  callback: mutation => init(mutation.target)
891
912
  });
892
913
 
893
914
  Actions.init([
894
915
  {
895
- name: ["upload", "download", "saveLocally", "import", "export"],
916
+ name: ["upload", "download", "saveLocally", "asveAs", "import", "export"],
896
917
  callback: (action) => {
897
918
  if (action.name === 'upload')
898
919
  upload(action.element)
package/src/server.js CHANGED
@@ -334,7 +334,7 @@ module.exports = async function file(CoCreateConfig, configPath, match) {
334
334
  let binary = fs.readFileSync(path);
335
335
  let content = new Buffer.from(binary).toString(readType);
336
336
 
337
- return content
337
+ return content;
338
338
  }
339
339
 
340
340
  /**