UNPKG

@tak-ps/node-tak

Version:

Lightweight JavaScript library for communicating with TAK Server

145 lines 4.76 kB
import FormData from 'form-data'; import { Readable } from 'node:stream'; import mime from 'mime'; import Commands, { CommandOutputFormat } from '../commands.js'; import { TAKList } from './types.js'; import { Type } from '@sinclair/typebox'; export const Content = Type.Object({ UID: Type.String(), SubmissionDateTime: Type.String(), Keywords: Type.Array(Type.String()), MIMEType: Type.String(), SubmissionUser: Type.String(), PrimaryKey: Type.String(), Hash: Type.String(), CreatorUid: Type.String(), Name: Type.String() }); export const TAKList_Content = TAKList(Type.Object({ filename: Type.String(), keywords: Type.Array(Type.String()), mimeType: Type.String(), name: Type.String(), submissionTime: Type.String(), submitter: Type.String(), uid: Type.String(), size: Type.Integer(), })); export const Config = Type.Object({ uploadSizeLimit: Type.Integer() }); export default class FileCommands extends Commands { schema = { list: { description: 'List Files', params: Type.Object({}), query: Type.Object({}), formats: [CommandOutputFormat.JSON] } }; async cli(args) { if (args._[3] === 'list') { const list = await this.list(); if (args.format === 'json') { return list; } else { return list.data.map((data) => { return data.filename; }).join('\n'); } } else { throw new Error('Unsupported Subcommand'); } } async list() { const url = new URL(`/Marti/api/sync/search`, this.api.url); return await this.api.fetch(url, { method: 'GET' }); } async meta(hash) { const url = new URL(`/Marti/sync/${encodeURIComponent(hash)}/metadata`, this.api.url); const res = await this.api.fetch(url, { method: 'GET' }, true); const body = await res.text(); return body; } async download(hash) { const url = new URL(`/Marti/sync/content`, this.api.url); url.searchParams.append('hash', hash); const res = await this.api.fetch(url, { method: 'GET' }, true); return res.body; } async adminDelete(hash) { const url = new URL(`/Marti/api/files/${hash}`, this.api.url); return await this.api.fetch(url, { method: 'DELETE', }); } async delete(hash) { const url = new URL(`/Marti/sync/delete`, this.api.url); url.searchParams.append('hash', hash); return await this.api.fetch(url, { method: 'DELETE', }); } // TODO Return a Content Object async uploadPackage(opts, body) { const url = new URL(`/Marti/sync/missionupload`, this.api.url); url.searchParams.append('filename', opts.name); url.searchParams.append('creatorUid', opts.creatorUid); url.searchParams.append('hash', opts.hash); if (body instanceof Buffer) { body = Readable.from(body); } const form = new FormData(); form.append('assetfile', body); const res = await this.api.fetch(url, { method: 'POST', body: form }); return res; } async upload(opts, body) { const url = new URL(`/Marti/sync/upload`, this.api.url); url.searchParams.append('name', opts.name); url.searchParams.append('keywords', opts.keywords.join(',')); url.searchParams.append('creatorUid', opts.creatorUid); if (opts.altitude) url.searchParams.append('altitude', opts.altitude); if (opts.longitude) url.searchParams.append('longitude', opts.longitude); if (opts.latitude) url.searchParams.append('latitude', opts.latitude); if (body instanceof Buffer) { body = Readable.from(body); } const res = await this.api.fetch(url, { method: 'POST', headers: { 'Content-Type': opts.contentType ? opts.contentType : mime.getType(opts.name), 'Content-Length': opts.contentLength }, body: body }); return JSON.parse(res); } async count() { const url = new URL('/Marti/api/files/metadata/count', this.api.url); return await this.api.fetch(url, { method: 'GET' }); } async config() { const url = new URL('/files/api/config', this.api.url); return await this.api.fetch(url, { method: 'GET' }); } } //# sourceMappingURL=files.js.map