-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathimageUpload.js
97 lines (84 loc) · 3.29 KB
/
imageUpload.js
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
const IMAGE_SERVICE_API_V3_URL =
'https://cube.tobit.cloud/image-service/v3/Images';
const IMAGE_RESIZER_API_URL =
'https://cube.tobit.cloud/image-resizer-backend/api/v1.0/image';
class ImageUploadError extends Error {
constructor(requestId, error) {
super(error instanceof Error ? error.message : error);
this.requestId = requestId;
if (error instanceof Error) {
this.originalError = error;
}
}
}
/**
* Uploads an image to the tsimg cloud service.
*
* @export
* @param {string | File} file A URL as a string or a `File` object that should be uploaded.
* @param {object} options object
* @param {string} [options.personId] A person is that should be associated with the uploaded image.
* @param {string} [options.siteId] A site id that should be associated with the uploaded image.
* @param {string} [options.accessToken=chayns.env.user.tobitAccessToken] The tobitAccessToken
* @param {string} _personId A person is that should be associated with the uploaded image. (deprecated, use options instead)
* @param {string} _siteId A site id that should be associated with the uploaded image. (deprecated, use options instead)
* @return {Promise<object>} The response data for the image upload.
*/
export default async function imageUpload(file, options, _personId, _siteId) {
const headers = new Headers({ Accept: 'application/json' });
let personId;
let siteId;
let accessToken;
if (options && typeof options === 'object') {
({ personId, siteId, accessToken } = options);
} else {
personId = _personId;
siteId = _siteId;
}
if (
!accessToken &&
typeof chayns !== 'undefined' &&
chayns?.env?.user?.isAuthenticated
) {
accessToken = chayns.env.user.tobitAccessToken;
}
if (!personId) {
personId = chayns.env.user.personId;
}
const owner = siteId || personId;
if (accessToken) headers.set('Authorization', `bearer ${accessToken}`);
// Build formData object.
const body = new FormData();
if (typeof file === 'string') {
body.append('SourceUrl', file);
} else {
body.append('File', file);
}
const url =
file.size > 10 * 1024 * 1024
? `${IMAGE_RESIZER_API_URL}/${owner}`
: `${IMAGE_SERVICE_API_V3_URL}/${owner}`;
const response = await fetch(url, { method: 'POST', body, headers });
const requestId = response.headers.get('x-request-id');
if (response.ok) {
try {
const { image, baseDomain } = await response.json();
// Maps image-service-api-v3 result to v2 result.
return {
key: image.path,
// Removes trailing slash from baseDomain, since image-service-api-v2 doesn't have a trailing slash on base.
base: baseDomain.endsWith('/')
? baseDomain.slice(0, -1)
: baseDomain,
meta: {
preview: image.preview,
width: image.width,
height: image.height,
},
};
} catch (ex) {
throw new ImageUploadError(requestId, ex);
}
}
throw new ImageUploadError(requestId, `Uploading the image failed with status code ${response.status}.`);
}