Skip to content

Commit f6abfac

Browse files
committed
some adjusts
1 parent 1a5baf4 commit f6abfac

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

components/crowdin/actions/add-file/add-file.mjs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
import fs from "fs";
12
import { TYPE_OPTIONS } from "../../common/constants.mjs";
2-
import { parseObject } from "../../common/utils.mjs";
3+
import {
4+
checkTmp,
5+
parseObject,
6+
} from "../../common/utils.mjs";
37
import crowdin from "../../crowdin.app.mjs";
48

59
export default {
@@ -16,11 +20,10 @@ export default {
1620
"projectId",
1721
],
1822
},
19-
storageId: {
20-
propDefinition: [
21-
crowdin,
22-
"storageId",
23-
],
23+
file: {
24+
type: "string",
25+
label: "File",
26+
description: "The path to the file saved to the `/tmp` directory (e.g. `/tmp/example.jpg`) to process. [See the documentation](https://pipedream.com/docs/workflows/steps/code/nodejs/working-with-files/#the-tmp-directory).",
2427
},
2528
name: {
2629
type: "string",
@@ -85,14 +88,29 @@ export default {
8588
crowdin,
8689
attachLabelIds,
8790
projectId,
91+
file,
8892
...data
8993
} = this;
9094

95+
const fileBinary = fs.readFileSync(checkTmp(file));
96+
const crowdinFilename = file.startsWith("/tmp/")
97+
? file.slice(5)
98+
: file;
99+
100+
const fileResponse = await crowdin.createStorage({
101+
data: Buffer.from(fileBinary, "binary"),
102+
headers: {
103+
"Crowdin-API-FileName": encodeURI(crowdinFilename),
104+
"Content-Type": "application/octet-stream",
105+
},
106+
});
107+
91108
const response = await crowdin.uploadFileToProject({
92109
$,
93110
projectId,
94111
data: {
95112
...data,
113+
storageId: fileResponse.data.id,
96114
attachLabelIds: parseObject(attachLabelIds),
97115
},
98116
});

components/crowdin/common/utils.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,10 @@ export const parseObject = (obj) => {
2222
}
2323
return obj;
2424
};
25+
26+
export const checkTmp = (filename) => {
27+
if (filename.indexOf("/tmp") === -1) {
28+
return `/tmp/${filename}`;
29+
}
30+
return filename;
31+
};

components/crowdin/crowdin.app.mjs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export default {
7373
},
7474
},
7575
storageId: {
76-
type: "string",
76+
type: "integer",
7777
label: "Storage ID",
7878
description: "The ID of the storage",
7979
async options({ page }) {
@@ -169,17 +169,18 @@ export default {
169169
_baseUrl() {
170170
return "https://api.crowdin.com/api/v2";
171171
},
172-
_headers() {
172+
_headers(headers = {}) {
173173
return {
174174
Authorization: `Bearer ${this.$auth.oauth_access_token}`,
175+
...headers,
175176
};
176177
},
177178
_makeRequest({
178-
$ = this, path, ...opts
179+
$ = this, path, headers, ...opts
179180
}) {
180181
return axios($, {
181182
url: this._baseUrl() + path,
182-
headers: this._headers(),
183+
headers: this._headers(headers),
183184
...opts,
184185
});
185186
},
@@ -238,6 +239,13 @@ export default {
238239
...opts,
239240
});
240241
},
242+
createStorage(opts = {}) {
243+
return this._makeRequest({
244+
method: "POST",
245+
path: "/storages",
246+
...opts,
247+
});
248+
},
241249
uploadFileToProject({
242250
projectId, ...opts
243251
}) {

components/crowdin/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"access": "public"
1414
},
1515
"dependencies": {
16-
"@pipedream/platform": "^3.0.3"
16+
"@pipedream/platform": "^3.0.3",
17+
"fs": "^0.0.1-security"
1718
}
1819
}

0 commit comments

Comments
 (0)