This repository was archived by the owner on Dec 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
107 lines (101 loc) · 3.21 KB
/
index.ts
File metadata and controls
107 lines (101 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import axios, { AxiosError } from 'axios';
import fs from 'fs';
import { MMFile } from './types';
const baseUrl = 'https://app.metamaze.eu/gql/api';
/**
* Upload files to Metamaze. You can choose whether you want
* to upload files to production or in training.
*
* @param folderPath Path to the folder where the files are located that should be processed.
* @param projectId Id of your project.
* @param organisationId Id of your organisation.
* @param token Input API token of your project.
* @param pipelineType Whether you want to upload your files in training or in production.
*/
export const uploadFilesInFolder = async (
folderPath: string,
projectId: string,
organisationId: string,
token: string,
pipelineType: 'TRAINING' | 'PRODUCTION' = 'PRODUCTION'
) => {
const folder = fs.readdirSync(folderPath, { withFileTypes: true });
const files: MMFile[] = [];
for (const file of folder) {
if (!file.isDirectory()) {
const filePath = `${folderPath}/${file.name}`;
const fileBuffer = fs.readFileSync(filePath);
files.push({
id: file.name,
name: file.name,
file: fileBuffer.toString('base64'),
});
}
}
if (files.length) {
const url = `${baseUrl}/organisations/${organisationId}/projects/${projectId}/${pipelineType === 'TRAINING' ? 'upload' : 'process'
}`;
try {
for (const file of files) {
console.log(`Uploading ${file.name} files to Metamaze.`);
const result = await axios.post(
url,
{ options: { eachFileIsADocument: true }, files: [file] },
{
headers: {
Authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
}
);
console.log('Upload has been successful.');
console.log(`Response: ${JSON.stringify(result.data, null, 2)}`);
}
} catch (e) {
console.log('Something failed with uploading files to Metamaze.');
if (e instanceof AxiosError) {
console.log(e.response.status);
console.log(e.response.data);
} else {
console.log(e);
}
}
}
};
/**
* Get payload of an upload from Metamaze.
* @param uploadId Id of upload.
* @param projectId Id of project.
* @param organisationId Id of organisation.
* @param token Input API token of your project.
*/
export const getUploadById = async (
uploadId: string,
projectId: string,
organisationId: string,
token: string
) => {
const url = `${baseUrl}/organisations/${organisationId}/projects/${projectId}/process/${uploadId}`;
try {
console.log(`Fetching upload from Metamaze.`);
const result = await axios.get(url, {
headers: {
Authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
maxContentLength: Infinity,
maxBodyLength: Infinity,
});
console.log(`Response: ${JSON.stringify(result.data, null, 2)}`);
} catch (e) {
console.log('Something failed with uploading files to Metamaze.');
if (e instanceof AxiosError) {
console.log(e.response.status);
console.log(e.response.data);
} else {
console.log(e);
}
}
};