in src/commands/image/upload.ts [21:50]
async run() {
const { args: { image } } = await this.parse(ImageUpload)
const maybeUrl = new Try(() => new URL(image))
if (!maybeUrl.isSuccess && !existsSync(image)) {
this.error(`File ${image} does not exist`, { exit: 1 })
}
const profile = this.profile!
const http = this.http!
const serviceDiscovery = await new ServiceDiscovery(http, profile.mediaApiHost).discover()
const loader = serviceDiscovery.getLink('loader')
if (!loader) {
this.error('loader link not found', { exit: 1 })
}
// Grid has a different endpoint for uploading an image as binary and for uploading the image via a url
if (maybeUrl.isSuccess) {
const url = new URL(`${loader!.href.toString()}/imports?uri=${encodeURIComponent(maybeUrl.get().toString())}`)
const uploadedImage = await http.post(url).then(_ => _.json())
this.log(JSON.stringify(uploadedImage))
} else {
const url = new URL(loader!.href.toString() + '/images')
const uploadedImage = await http.post(url, readFileSync(image)).then(_ => _.json())
this.log(JSON.stringify(uploadedImage))
}
}