Skip to content

Commit f9a561b

Browse files
committed
fix: handle file.id when setFile() rendering.
1 parent c073276 commit f9a561b

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/client.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,8 @@ function setFiles(element, files) {
346346

347347
let selected = inputs.get(element) || new Map()
348348
for (let i = 0; i < files.length; i++) {
349+
if (!files[i].id)
350+
files[i].id = files[i].pathname
349351
files[i].input = element
350352
selected.set(files[i].id, files[i])
351353
Files.set(files[i].id, files[i])
@@ -802,37 +804,51 @@ async function exportFile(data) {
802804
}
803805

804806
// TODO: handled by import? if value is a valid url get file by url?
805-
async function importFileFromURL(url) {
807+
async function importURL(action) {
806808
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
816818

817-
// Convert the response data to a Blob
818-
const blob = await response.blob();
819+
}
819820

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();
822823

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,
827827
directory: '/',
828828
path: '/',
829-
pathname: '/' + filename,
830-
'content-type': file.type,
831-
input: handle.input || null,
832-
id: await getFileId(file)
829+
pathname: '/' + filename
833830
};
834831

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+
836852
} catch (error) {
837853
console.error('Error importing file from URL:', error);
838854
throw error;
@@ -951,7 +967,7 @@ Observer.init({
951967

952968
Actions.init([
953969
{
954-
name: ["upload", "download", "saveLocally", "asveAs", "import", "export"],
970+
name: ["upload", "download", "saveLocally", "asveAs", "import", "export", "importUrl"],
955971
callback: (action) => {
956972
if (action.name === 'upload')
957973
upload(action.element)
@@ -961,6 +977,8 @@ Actions.init([
961977
Export(action.element)
962978
} else if (action.name === 'import') {
963979
Import(action.element)
980+
} else if (action.name === 'importUrl') {
981+
importURL(action)
964982
} else {
965983
// Something...
966984
}

0 commit comments

Comments
 (0)