-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Part 2 - applying File URL or Path improvements #17091
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1aef040
3d6ce07
54a0c8d
49b328f
d174051
9e11477
8a57ff3
9bf8aad
c104045
5624c24
fe3bb7e
fed67fa
b560b05
04ae59a
69bbb92
094bf97
0107ed6
8aa2266
ed11f6e
1afc1fa
c0438fb
055a2e4
b6a68ad
9158a07
c34250f
13d0e9f
3021054
1192865
38a9f0d
6106cd9
9aa479e
70082f3
0ea7b35
5d5eb5f
e09ce7e
d8b5d3c
c90462e
e89430f
46fd256
ade6f85
d8e872a
5b7ca8c
86cb294
648cc42
2f07169
c30b8b9
1b5c444
7f2bceb
fab27d8
a0bc8d7
b1a6f91
d92f622
637447e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,13 +1,13 @@ | ||||||||||||||||||||||||||||||||||||||||||
import app from "../../akeneo.app.mjs"; | ||||||||||||||||||||||||||||||||||||||||||
import utils from "../../common/utils.mjs"; | ||||||||||||||||||||||||||||||||||||||||||
import { ConfigurationError } from "@pipedream/platform"; | ||||||||||||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||||||||||||
ConfigurationError, getFileStreamAndMetadata, | ||||||||||||||||||||||||||||||||||||||||||
} from "@pipedream/platform"; | ||||||||||||||||||||||||||||||||||||||||||
import FormData from "form-data"; | ||||||||||||||||||||||||||||||||||||||||||
import fs from "fs"; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
export default { | ||||||||||||||||||||||||||||||||||||||||||
type: "action", | ||||||||||||||||||||||||||||||||||||||||||
key: "akeneo-create-a-new-product-media-file", | ||||||||||||||||||||||||||||||||||||||||||
version: "0.0.1", | ||||||||||||||||||||||||||||||||||||||||||
version: "0.1.0", | ||||||||||||||||||||||||||||||||||||||||||
name: "Create A New Product Media File", | ||||||||||||||||||||||||||||||||||||||||||
description: "Allows you to create a new media file and associate it to an attribute value of a given product or product model. [See the docs](https://api.akeneo.com/api-reference.html#post_media_files)", | ||||||||||||||||||||||||||||||||||||||||||
props: { | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -32,18 +32,15 @@ export default { | |||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
filename: { | ||||||||||||||||||||||||||||||||||||||||||
type: "string", | ||||||||||||||||||||||||||||||||||||||||||
label: "File", | ||||||||||||||||||||||||||||||||||||||||||
description: "The file to be uploaded, please provide a file from `/tmp`. To upload a file to `/tmp` folder, please follow the doc [here](https://pipedream.com/docs/code/nodejs/working-with-files/#writing-a-file-to-tmp)", | ||||||||||||||||||||||||||||||||||||||||||
label: "File Path or URL", | ||||||||||||||||||||||||||||||||||||||||||
description: "The file to be uploaded. Provide either a file URL or a path to a file in the `/tmp` directory (for example, `/tmp/myFile.txt`)", | ||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||||||||||||||
async run ({ $ }) { | ||||||||||||||||||||||||||||||||||||||||||
if (!this.productId && !this.productModelCode) { | ||||||||||||||||||||||||||||||||||||||||||
throw new ConfigurationError("Either `Product Identifier` or `Product Model Code` should be set!"); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const path = utils.checkTmp(this.filename); | ||||||||||||||||||||||||||||||||||||||||||
if (!fs.existsSync(path)) { | ||||||||||||||||||||||||||||||||||||||||||
throw new ConfigurationError("File does not exist!"); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
const payload = { | ||||||||||||||||||||||||||||||||||||||||||
attribute: this.mediaFileAttributeCode, | ||||||||||||||||||||||||||||||||||||||||||
scope: null, | ||||||||||||||||||||||||||||||||||||||||||
|
@@ -57,9 +54,15 @@ export default { | |||||||||||||||||||||||||||||||||||||||||
payload.code = this.productModelCode; | ||||||||||||||||||||||||||||||||||||||||||
data.append("product_model", JSON.stringify(payload)); | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
const file = fs.readFileSync(path); | ||||||||||||||||||||||||||||||||||||||||||
const fileParts = path.split("/"); | ||||||||||||||||||||||||||||||||||||||||||
data.append("file", file, fileParts[fileParts.length - 1]); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
const { | ||||||||||||||||||||||||||||||||||||||||||
stream, metadata, | ||||||||||||||||||||||||||||||||||||||||||
} = await getFileStreamAndMetadata(this.filename); | ||||||||||||||||||||||||||||||||||||||||||
data.append("file", stream, { | ||||||||||||||||||||||||||||||||||||||||||
contentType: metadata.contentType, | ||||||||||||||||||||||||||||||||||||||||||
knownLength: metadata.size, | ||||||||||||||||||||||||||||||||||||||||||
filename: metadata.name, | ||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||
const contentLength = data.getLengthSync(); | ||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+58
to
66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion
For remote URLs where Safer approach: -const contentLength = data.getLengthSync();
+const contentLength = await new Promise((res, rej) =>
+ data.getLength((err, len) => err ? rej(err) : res(len))); and omit 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
|
||||||||||||||||||||||||||||||||||||||||||
await this.app.createProductMediaFile({ | ||||||||||||||||||||||||||||||||||||||||||
$, | ||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@pipedream/akeneo", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"description": "Pipedream Akeneo Components", | ||
"main": "akeneo.app.mjs", | ||
"keywords": [ | ||
|
@@ -10,7 +10,7 @@ | |
"homepage": "https://pipedream.com/apps/akeneo", | ||
"author": "Pipedream <[email protected]> (https://pipedream.com/)", | ||
"dependencies": { | ||
"@pipedream/platform": "^1.3.0", | ||
"@pipedream/platform": "^3.1.0", | ||
"form-data": "^4.0.0" | ||
}, | ||
"publishConfig": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Wrap file-stream retrieval in try/catch and validate metadata
getFileStreamAndMetadata()
throws for 404/invalid URLs. Wrapping it provides clearer, action-specific errors:Also only set
knownLength
whenmetadata.size
is defined to preventform-data
errors.📝 Committable suggestion
🤖 Prompt for AI Agents