Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix: file downloads for unusual files

Brad Barbin1339951c84db
diff --git a/apps/erp/app/components/Documents.tsx b/apps/erp/app/components/Documents.tsx
index cda2536d4e..ef5f408928 100644
--- a/apps/erp/app/components/Documents.tsx
+++ b/apps/erp/app/components/Documents.tsx
@@ -109,7 +109,7 @@ const Documents = ({
toast.success(`${file.name} deleted successfully`);
revalidator.revalidate();
},
- [getReadPath, carbon?.storage, revalidator]
+ [carbon?.storage, getReadPath, revalidator]
);
const downloadModel = useCallback(
@@ -119,26 +119,22 @@ const Documents = ({
return;
}
- const result = await carbon?.storage
- .from("private")
- .download(model.modelPath);
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = model.modelName;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${model.modelPath}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = model.modelName;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
@@ -170,28 +166,24 @@ const Documents = ({
const download = useCallback(
async (file: StorageItem) => {
- const result = await carbon?.storage
- .from("private")
- .download(getReadPath(file));
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = file.name;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${getReadPath(file)}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = file.name;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
- [carbon?.storage, getReadPath]
+ [getReadPath]
);
const upload = useCallback(
diff --git a/apps/erp/app/modules/documents/ui/Documents/useDocument.ts b/apps/erp/app/modules/documents/ui/Documents/useDocument.ts
index 3b9837a5b1..5726167102 100644
--- a/apps/erp/app/modules/documents/ui/Documents/useDocument.ts
+++ b/apps/erp/app/modules/documents/ui/Documents/useDocument.ts
@@ -71,28 +71,26 @@ export const useDocument = () => {
async (doc: DocumentType) => {
if (!doc.path) throw new Error("Document path is undefined");
- const result = await carbon?.storage.from("private").download(doc.path);
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = doc.name ?? "File";
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${doc.path}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = doc.name ?? "File";
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
await insertTransaction(doc, "Download");
},
- [carbon, insertTransaction]
+ [insertTransaction]
);
const view = useCallback(
diff --git a/apps/erp/app/modules/items/ui/Item/ItemDocuments.tsx b/apps/erp/app/modules/items/ui/Item/ItemDocuments.tsx
index 24da37c8d7..642358d30b 100644
--- a/apps/erp/app/modules/items/ui/Item/ItemDocuments.tsx
+++ b/apps/erp/app/modules/items/ui/Item/ItemDocuments.tsx
@@ -323,26 +323,27 @@ export const useItemDocuments = ({ itemId, type }: Props) => {
return;
}
- const result = await carbon?.storage
- .from("private")
- .download(model.modelPath);
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
+ if (!model.modelPath || !model.modelName) {
+ toast.error("Model data is missing");
return;
}
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = model.modelName;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${model.modelPath}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = model.modelName;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
@@ -350,26 +351,22 @@ export const useItemDocuments = ({ itemId, type }: Props) => {
const download = useCallback(
async (file: FileObject) => {
- const result = await carbon?.storage
- .from("private")
- .download(getPath(file));
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = file.name;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${getPath(file)}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = file.name;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[getPath]
diff --git a/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionDocuments.tsx b/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionDocuments.tsx
index aac40dc640..c8d732aedb 100644
--- a/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionDocuments.tsx
+++ b/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionDocuments.tsx
@@ -223,28 +223,24 @@ export const useSupplierInteractionDocuments = ({
const download = useCallback(
async (attachment: FileObject) => {
- const result = await carbon?.storage
- .from("private")
- .download(getPath(attachment));
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = attachment.name;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${getPath(attachment)}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = attachment.name;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
- [carbon?.storage, getPath]
+ [getPath]
);
const createDocumentRecord = useCallback(
diff --git a/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionLineDocuments.tsx b/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionLineDocuments.tsx
index c45fb75629..153cbce6f7 100644
--- a/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionLineDocuments.tsx
+++ b/apps/erp/app/modules/purchasing/ui/SupplierInteraction/SupplierInteractionLineDocuments.tsx
@@ -91,28 +91,24 @@ const useSupplierInteractionLineDocuments = ({
const download = useCallback(
async (file: ItemFile) => {
- const result = await carbon?.storage
- .from("private")
- .download(getPath(file));
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = file.name;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${getPath(file)}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = file.name;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
- [carbon?.storage, getPath]
+ [getPath]
);
const createDocumentRecord = useCallback(
diff --git a/apps/erp/app/modules/sales/ui/Opportunity/OpportunityDocuments.tsx b/apps/erp/app/modules/sales/ui/Opportunity/OpportunityDocuments.tsx
index d551b7c8a5..6fd0000fb4 100644
--- a/apps/erp/app/modules/sales/ui/Opportunity/OpportunityDocuments.tsx
+++ b/apps/erp/app/modules/sales/ui/Opportunity/OpportunityDocuments.tsx
@@ -299,28 +299,24 @@ export const useOpportunityDocuments = ({
const download = useCallback(
async (attachment: FileObject) => {
- const result = await carbon?.storage
- .from("private")
- .download(getPath(attachment));
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = attachment.name;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${getPath(attachment)}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = attachment.name;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
- [carbon?.storage, getPath]
+ [getPath]
);
const createDocumentRecord = useCallback(
diff --git a/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx b/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx
index 309987bcf8..381ec85fef 100644
--- a/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx
+++ b/apps/erp/app/modules/sales/ui/Opportunity/OpportunityLineDocuments.tsx
@@ -129,26 +129,22 @@ const useOpportunityLineDocuments = ({
return;
}
- const result = await carbon?.storage
- .from("private")
- .download(model.modelPath);
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = model.modelName;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${model.modelPath}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = model.modelName;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
@@ -156,26 +152,22 @@ const useOpportunityLineDocuments = ({
const download = useCallback(
async (file: FileObject) => {
- const result = await carbon?.storage
- .from("private")
- .download(getPath(file));
-
- if (!result || result.error) {
- toast.error(result?.error?.message || "Error downloading file");
- return;
- }
-
- const a = document.createElement("a");
- document.body.appendChild(a);
- const url = window.URL.createObjectURL(result.data);
- a.href = url;
- a.download = file.name;
- a.click();
-
- setTimeout(() => {
- window.URL.revokeObjectURL(url);
+ const url = path.to.file.previewFile(`private/${getPath(file)}`);
+ try {
+ const response = await fetch(url);
+ const blob = await response.blob();
+ const blobUrl = window.URL.createObjectURL(blob);
+ const a = document.createElement("a");
+ document.body.appendChild(a);
+ a.href = blobUrl;
+ a.download = file.name;
+ a.click();
+ window.URL.revokeObjectURL(blobUrl);
document.body.removeChild(a);
- }, 0);
+ } catch (error) {
+ toast.error("Error downloading file");
+ console.error(error);
+ }
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[]