Skip to content

Commit

Permalink
feat: import file from url
Browse files Browse the repository at this point in the history
  • Loading branch information
frankpagan committed Jun 27, 2024
1 parent d132289 commit c073276
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,44 @@ async function exportFile(data) {
link.remove();
}

// TODO: handled by import? if value is a valid url get file by url?
async function importFileFromURL(url) {
try {
// Fetch the file data from the URL
const response = await fetch(url);
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}

// Get the filename from the URL
const urlParts = url.split('/');
const filename = urlParts[urlParts.length - 1];

// Convert the response data to a Blob
const blob = await response.blob();

// Create a File object
const file = new File([blob], filename, { type: blob.type });

// Create a custom file object with additional properties
const fileObject = {
src: file,
size: file.size,
directory: '/',
path: '/',
pathname: '/' + filename,
'content-type': file.type,
input: handle.input || null,
id: await getFileId(file)
};

return fileObject;
} catch (error) {
console.error('Error importing file from URL:', error);
throw error;
}
}

async function fileRenderAction(action) {
const element = action.element

Expand Down

0 comments on commit c073276

Please sign in to comment.