-
Notifications
You must be signed in to change notification settings - Fork 119
/
index.js
89 lines (77 loc) · 2.29 KB
/
index.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
import { readFile } from "fs/promises";
import { fileFromPath } from "formdata-node/file-from-path";
import mime from "mime-types";
import { Blob, File } from "@esri/arcgis-rest-form-data";
import { addAttachment, addFeatures } from "@esri/arcgis-rest-feature-service";
const serviceUrl =
"https://services9.arcgis.com/tPT6CGkx2VJKZshp/arcgis/rest/services/test_w_attachments/FeatureServer/0";
async function addDemoPoint() {
const { addResults } = await addFeatures({
url: serviceUrl,
features: [
{
geometry: {
x: getRandomInRange(-180, 180, 3),
y: getRandomInRange(-180, 180, 3),
spatialReference: { wkid: 4326 }
},
attributes: { name: `Upload Test: ${new Date().toISOString()}` }
}
]
});
console.log("Add Feature Results:", addResults);
return addResults[0].objectId;
}
try {
const imagePath = "./image.jpg";
const objectId = await addDemoPoint();
// Option 1: using the `fileFromPath` utility
const file = await fileFromPath(imagePath);
// Option 2: using `fs.readFile` and converting Buffer to Blob
const buffer = await readFile(imagePath);
const blob = new Blob([new Uint8Array(buffer).buffer], {
type: mime.lookup(imagePath)
});
// Option 3: convert from Buffer to Blob to file
const fileFromBlob = new File([blob], "fileFromBlob.jpg", {
type: mime.lookup(imagePath)
});
// Option 4: convert from Buffer to File
const fileFromBuffer = new File(
[new Uint8Array(buffer).buffer],
"fileFromBuffer.jpg",
{
type: mime.lookup(imagePath)
}
);
const results = await Promise.all([
addAttachment({
url: serviceUrl,
featureId: objectId,
attachment: file
}),
addAttachment({
url: serviceUrl,
featureId: objectId,
attachment: blob
}),
addAttachment({
url: serviceUrl,
featureId: objectId,
attachment: fileFromBlob
}),
addAttachment({
url: serviceUrl,
featureId: objectId,
attachment: fileFromBuffer
})
]);
console.log("Add Attachment Results:", results);
} catch (e) {
console.error(e);
}
// Used for generating a random lat/lng
// from https://stackoverflow.com/a/6878845
function getRandomInRange(from, to, fixed) {
return (Math.random() * (to - from) + from).toFixed(fixed) * 1;
}