@cocreate/file 1.17.4 → 1.18.0

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,24 @@
1
+ # [1.18.0](https://github.com/CoCreate-app/CoCreate-file/compare/v1.17.5...v1.18.0) (2024-08-24)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * handle file.id when setFile() rendering. ([f9a561b](https://github.com/CoCreate-app/CoCreate-file/commit/f9a561b325ae0b8fe964b11d192c09ba2d144e4f))
7
+
8
+
9
+ ### Features
10
+
11
+ * import file from url ([c073276](https://github.com/CoCreate-app/CoCreate-file/commit/c073276894009a04fdf59e3bebabbd34bea3f55b))
12
+
13
+ ## [1.17.5](https://github.com/CoCreate-app/CoCreate-file/compare/v1.17.4...v1.17.5) (2024-06-19)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * getFileId uses pathname as id ([be330a5](https://github.com/CoCreate-app/CoCreate-file/commit/be330a54324ae878a8afadc68bdae1684e944b45))
19
+ * observer target selector ([50dc031](https://github.com/CoCreate-app/CoCreate-file/commit/50dc0318b7c9c02c2d610fe86158d47e6277e667))
20
+ * replace selected file if multiple false ([d419dc3](https://github.com/CoCreate-app/CoCreate-file/commit/d419dc38f223d1d809bcf014dd2fadb0a629fae7))
21
+
1
22
  ## [1.17.4](https://github.com/CoCreate-app/CoCreate-file/compare/v1.17.3...v1.17.4) (2024-06-12)
2
23
 
3
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file",
3
- "version": "1.17.4",
3
+ "version": "1.18.0",
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
 
@@ -325,6 +346,8 @@ function setFiles(element, files) {
325
346
 
326
347
  let selected = inputs.get(element) || new Map()
327
348
  for (let i = 0; i < files.length; i++) {
349
+ if (!files[i].id)
350
+ files[i].id = files[i].pathname
328
351
  files[i].input = element
329
352
  selected.set(files[i].id, files[i])
330
353
  Files.set(files[i].id, files[i])
@@ -534,7 +557,7 @@ async function upload(element, data) {
534
557
  }
535
558
 
536
559
  // Append the first segment to start
537
- appendSegmfent(0);
560
+ appendSegment(0);
538
561
  });
539
562
 
540
563
 
@@ -780,6 +803,58 @@ async function exportFile(data) {
780
803
  link.remove();
781
804
  }
782
805
 
806
+ // TODO: handled by import? if value is a valid url get file by url?
807
+ async function importURL(action) {
808
+ try {
809
+ let element = action.element
810
+ let url = element.getAttribute('url')
811
+ if (!url) {
812
+ element = action.form.querySelector('[import-url]')
813
+ if (!element)
814
+ return
815
+ url = element.getValue()
816
+ if (!url)
817
+ return
818
+
819
+ }
820
+
821
+ const urlObject = new URL(url);
822
+ const filename = urlObject.pathname.split('/').pop();
823
+
824
+ const file = {
825
+ src: url,
826
+ name: filename,
827
+ directory: '/',
828
+ path: '/',
829
+ pathname: '/' + filename
830
+ };
831
+
832
+ await getCustomData(file)
833
+
834
+ let data = await Crud.socket.send({
835
+ method: 'importUrl',
836
+ file,
837
+ broadcast: false,
838
+ broadcastClient: false
839
+ })
840
+
841
+ let queriedElements = queryElements({ element, prefix: 'import-url' })
842
+ if (queriedElements) {
843
+ for (let queriedElement of queriedElements)
844
+ queriedElement.setValue(data.file)
845
+
846
+ }
847
+
848
+ document.dispatchEvent(new CustomEvent(action.name, {
849
+ detail: {}
850
+ }));
851
+
852
+ } catch (error) {
853
+ console.error('Error importing file from URL:', error);
854
+ throw error;
855
+ }
856
+ }
857
+
783
858
  async function fileRenderAction(action) {
784
859
  const element = action.element
785
860
 
@@ -877,7 +952,7 @@ async function Delete(file) {
877
952
  Observer.init({
878
953
  name: 'CoCreateFileAddedNodes',
879
954
  observe: ['addedNodes'],
880
- target: 'input[type="file"]',
955
+ target: '[type="file"]',
881
956
  callback: mutation => init(mutation.target)
882
957
 
883
958
  });
@@ -886,13 +961,13 @@ Observer.init({
886
961
  name: 'CoCreateFileAttributes',
887
962
  observe: ['attributes'],
888
963
  attributeName: ['type'],
889
- target: 'input[type="file"]',
964
+ target: '[type="file"]',
890
965
  callback: mutation => init(mutation.target)
891
966
  });
892
967
 
893
968
  Actions.init([
894
969
  {
895
- name: ["upload", "download", "saveLocally", "import", "export"],
970
+ name: ["upload", "download", "saveLocally", "asveAs", "import", "export", "importUrl"],
896
971
  callback: (action) => {
897
972
  if (action.name === 'upload')
898
973
  upload(action.element)
@@ -902,6 +977,8 @@ Actions.init([
902
977
  Export(action.element)
903
978
  } else if (action.name === 'import') {
904
979
  Import(action.element)
980
+ } else if (action.name === 'importUrl') {
981
+ importURL(action)
905
982
  } else {
906
983
  // Something...
907
984
  }
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
  /**