Skip to content

Commit c073276

Browse files
committed
feat: import file from url
1 parent d132289 commit c073276

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/client.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,44 @@ async function exportFile(data) {
801801
link.remove();
802802
}
803803

804+
// TODO: handled by import? if value is a valid url get file by url?
805+
async function importFileFromURL(url) {
806+
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];
816+
817+
// Convert the response data to a Blob
818+
const blob = await response.blob();
819+
820+
// Create a File object
821+
const file = new File([blob], filename, { type: blob.type });
822+
823+
// Create a custom file object with additional properties
824+
const fileObject = {
825+
src: file,
826+
size: file.size,
827+
directory: '/',
828+
path: '/',
829+
pathname: '/' + filename,
830+
'content-type': file.type,
831+
input: handle.input || null,
832+
id: await getFileId(file)
833+
};
834+
835+
return fileObject;
836+
} catch (error) {
837+
console.error('Error importing file from URL:', error);
838+
throw error;
839+
}
840+
}
841+
804842
async function fileRenderAction(action) {
805843
const element = action.element
806844

0 commit comments

Comments
 (0)