@cocreate/file 1.17.5 → 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,15 @@
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
+
1
13
  ## [1.17.5](https://github.com/CoCreate-app/CoCreate-file/compare/v1.17.4...v1.17.5) (2024-06-19)
2
14
 
3
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/file",
3
- "version": "1.17.5",
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
@@ -346,6 +346,8 @@ function setFiles(element, files) {
346
346
 
347
347
  let selected = inputs.get(element) || new Map()
348
348
  for (let i = 0; i < files.length; i++) {
349
+ if (!files[i].id)
350
+ files[i].id = files[i].pathname
349
351
  files[i].input = element
350
352
  selected.set(files[i].id, files[i])
351
353
  Files.set(files[i].id, files[i])
@@ -801,6 +803,58 @@ async function exportFile(data) {
801
803
  link.remove();
802
804
  }
803
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
+
804
858
  async function fileRenderAction(action) {
805
859
  const element = action.element
806
860
 
@@ -913,7 +967,7 @@ Observer.init({
913
967
 
914
968
  Actions.init([
915
969
  {
916
- name: ["upload", "download", "saveLocally", "asveAs", "import", "export"],
970
+ name: ["upload", "download", "saveLocally", "asveAs", "import", "export", "importUrl"],
917
971
  callback: (action) => {
918
972
  if (action.name === 'upload')
919
973
  upload(action.element)
@@ -923,6 +977,8 @@ Actions.init([
923
977
  Export(action.element)
924
978
  } else if (action.name === 'import') {
925
979
  Import(action.element)
980
+ } else if (action.name === 'importUrl') {
981
+ importURL(action)
926
982
  } else {
927
983
  // Something...
928
984
  }