UNPKG

nodevk-ts

Version:

Simple Node.js module that allows you to interact with the VKontakte API.

43 lines (42 loc) 1.71 kB
import { promises as fs } from "fs"; import path from "path"; import request from "../request.js"; import API, { InvokeMethodException } from "./api.js"; ; export default class PhotosAPI extends API { constructor() { super(...arguments); this.api_name = "photos"; } async getMessagesUploadServer(peer_id) { let method = this.api_name + ".getMessagesUploadServer"; if (!this.checkValid("group", "user")) throw new InvokeMethodException(method, this.type); return await this.call(method, { peer_id: peer_id }); } async saveMessagesPhoto(info) { let method = this.api_name + ".saveMessagesPhoto"; if (!this.checkValid("group", "user")) throw new InvokeMethodException(method, this.type); if (info.photo == "[]") throw new ReferenceError("Don't load photo"); return await this.call(method, info); } async uploadPhoto(server, photo) { return JSON.parse((await request({ url: server.upload_url, data: { "photo": photo } })).toString()); } async uploadMessagePhoto(peer_id, photo) { if (photo instanceof Promise) photo = await photo; if (typeof photo === "string") photo = { filename: path.basename(photo), content: await fs.readFile(photo) }; return (await this.saveMessagesPhoto(await this.uploadPhoto(await this.getMessagesUploadServer(peer_id), photo)))[0]; } async uploadMessagePhotos(peer_id, photo) { const promises = photo.map(e => this.uploadMessagePhoto(peer_id, e)); return await Promise.all(promises); } }