@@ -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 ] )
@@ -802,37 +804,51 @@ async function exportFile(data) {
802
804
}
803
805
804
806
// TODO: handled by import? if value is a valid url get file by url?
805
- async function importFileFromURL ( url ) {
807
+ async function importURL ( action ) {
806
808
try {
807
- // Fetch the file data from the URL
808
- const response = await fetch ( url ) ;
809
- if ( ! response . ok ) {
810
- throw new Error ( 'Network response was not ok ' + response . statusText ) ;
811
- }
812
-
813
- // Get the filename from the URL
814
- const urlParts = url . split ( '/' ) ;
815
- const filename = urlParts [ urlParts . length - 1 ] ;
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
816
818
817
- // Convert the response data to a Blob
818
- const blob = await response . blob ( ) ;
819
+ }
819
820
820
- // Create a File object
821
- const file = new File ( [ blob ] , filename , { type : blob . type } ) ;
821
+ const urlObject = new URL ( url ) ;
822
+ const filename = urlObject . pathname . split ( '/' ) . pop ( ) ;
822
823
823
- // Create a custom file object with additional properties
824
- const fileObject = {
825
- src : file ,
826
- size : file . size ,
824
+ const file = {
825
+ src : url ,
826
+ name : filename ,
827
827
directory : '/' ,
828
828
path : '/' ,
829
- pathname : '/' + filename ,
830
- 'content-type' : file . type ,
831
- input : handle . input || null ,
832
- id : await getFileId ( file )
829
+ pathname : '/' + filename
833
830
} ;
834
831
835
- return fileObject ;
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
+
836
852
} catch ( error ) {
837
853
console . error ( 'Error importing file from URL:' , error ) ;
838
854
throw error ;
@@ -951,7 +967,7 @@ Observer.init({
951
967
952
968
Actions . init ( [
953
969
{
954
- name : [ "upload" , "download" , "saveLocally" , "asveAs" , "import" , "export" ] ,
970
+ name : [ "upload" , "download" , "saveLocally" , "asveAs" , "import" , "export" , "importUrl" ] ,
955
971
callback : ( action ) => {
956
972
if ( action . name === 'upload' )
957
973
upload ( action . element )
@@ -961,6 +977,8 @@ Actions.init([
961
977
Export ( action . element )
962
978
} else if ( action . name === 'import' ) {
963
979
Import ( action . element )
980
+ } else if ( action . name === 'importUrl' ) {
981
+ importURL ( action )
964
982
} else {
965
983
// Something...
966
984
}
0 commit comments