|
| 1 | +"use strict"; |
| 2 | +const axios = require("axios"); |
| 3 | +const FormData = require("form-data"); |
| 4 | +const fs = require("fs"); |
| 5 | +const plrequest = require("../axiosInstance"); |
| 6 | +const getSummary = require("../projects/getSummary"); |
| 7 | +/** |
| 8 | + * |
| 9 | + * @param {String} filePath |
| 10 | + * @param {*} t |
| 11 | + * @returns |
| 12 | + */ |
| 13 | +async function uploadFile(filePath, t) { |
| 14 | + const formData = new FormData(); |
| 15 | + const f = fs.createReadStream(filePath); |
| 16 | + formData.append("authorization", t.Authorization); |
| 17 | + formData.append("policy", t.Policy); |
| 18 | + formData.append("file", f, { filename: "temp.jpg" }); |
| 19 | + try { |
| 20 | + // @ts-ignore |
| 21 | + const response = await axios.post("https://v0.api.upyun.com/qphysics", formData, { |
| 22 | + headers: formData.getHeaders(), |
| 23 | + }); |
| 24 | + return response.data; |
| 25 | + } |
| 26 | + catch (error) { |
| 27 | + console.error("上传文件失败:", error); |
| 28 | + } |
| 29 | +} |
| 30 | +/** |
| 31 | + * 更新作品封面 |
| 32 | + * @param {string} filename - 需要上传的图片路径(本地) |
| 33 | + * @param {string} c - 作品分区 |
| 34 | + * @param {number} i - 作品ID |
| 35 | + * @returns {Promise<void>} - 无返回值 |
| 36 | + */ |
| 37 | +async function cover(filename, c, i) { |
| 38 | + this._getSummary = getSummary.bind(this); |
| 39 | + const getSummaryResponse = await this._getSummary(i, c); |
| 40 | + // @ts-ignore |
| 41 | + const imageIdex = getSummaryResponse.Data.Image + 1; |
| 42 | + try { |
| 43 | + const confirmExperimentResponse = await plrequest.post("/Contents/ConfirmExperiment", { |
| 44 | + Category: c, |
| 45 | + SummaryID: i, |
| 46 | + Image: imageIdex, |
| 47 | + Extension: ".png", |
| 48 | + }, { |
| 49 | + headers: { |
| 50 | + "Content-Type": "application/json", |
| 51 | + Accept: "application/json", |
| 52 | + "Accept-Language": "zh-CN", |
| 53 | + // @ts-ignore |
| 54 | + "x-API-Token": this.token, |
| 55 | + // @ts-ignore |
| 56 | + "x-API-AuthCode": this.authCode, |
| 57 | + }, |
| 58 | + }); |
| 59 | + // @ts-ignore |
| 60 | + const summaryData = getSummaryResponse.Data; |
| 61 | + const submitExperimentResponse = await plrequest.post("/Contents/SubmitExperiment", { |
| 62 | + Request: { |
| 63 | + FileSize: 0 - Math.abs(fs.statSync(filename).size), |
| 64 | + Extension: ".jpg", |
| 65 | + }, |
| 66 | + Summary: summaryData, |
| 67 | + }, { |
| 68 | + headers: { |
| 69 | + "Content-Type": "application/json", |
| 70 | + Accept: "application/json", |
| 71 | + "Accept-Language": "zh-CN", |
| 72 | + // @ts-ignore |
| 73 | + "x-API-Token": this.token, |
| 74 | + // @ts-ignore |
| 75 | + "x-API-AuthCode": this.authCode, |
| 76 | + }, |
| 77 | + }); |
| 78 | + await uploadFile(filename, submitExperimentResponse.data.Data.Token); |
| 79 | + await plrequest.post("/Contents/ConfirmExperiment", { |
| 80 | + Category: c, |
| 81 | + SummaryID: i, |
| 82 | + Image: imageIdex, |
| 83 | + Extension: ".png", |
| 84 | + }, { |
| 85 | + headers: { |
| 86 | + "Content-Type": "application/json", |
| 87 | + Accept: "application/json", |
| 88 | + "Accept-Language": "zh-CN", |
| 89 | + // @ts-ignore |
| 90 | + "x-API-Token": this.token, |
| 91 | + // @ts-ignore |
| 92 | + "x-API-AuthCode": this.authCode, |
| 93 | + }, |
| 94 | + }); |
| 95 | + Promise.resolve(); |
| 96 | + } |
| 97 | + catch (error) { |
| 98 | + throw error; |
| 99 | + } |
| 100 | +} |
| 101 | +module.exports = cover; |
0 commit comments