7 changed files with 418 additions and 43 deletions
@ -0,0 +1,207 @@ |
|||
|
|||
const ut = require("../sdk/ts/sdk-unittest-node.js"); |
|||
const comd = require("../sdk/ts/common-data-node.js"); |
|||
const fs = require("fs"); |
|||
const path = require("path"); |
|||
// 读取json文件
|
|||
function readJsonFile(pathStr) { |
|||
let detail = fs.readFileSync(pathStr, "utf8"); |
|||
detail = JSON.parse(detail); |
|||
return detail; |
|||
} |
|||
|
|||
/** 读取物料详情 */ |
|||
function readMatDetail(reqR, id, inParam) { |
|||
const relatDetail = readJsonFile(path.join(reqR.detailDir, id + ".json")); |
|||
inParam.setMatDetail(id, relatDetail); |
|||
} |
|||
|
|||
/** 读取素材详情 */ |
|||
function readDetail(reqR, id, inParam) { |
|||
const relatDetail = readJsonFile(path.join(reqR.detailDir, id + ".json")); |
|||
inParam.setDetail(id, relatDetail); |
|||
|
|||
if (!inParam.hasBin(relatDetail.materialType)) { |
|||
inParam.makeContent(relatDetail); |
|||
return; |
|||
} |
|||
const relatPth = path.join(reqR.binDir, id + ".bin"); |
|||
const relatBin = fs.readFileSync(relatPth); |
|||
const r = inParam.setBin(relatBin, relatDetail); |
|||
for (const mid of r) { |
|||
readDetail(reqR, mid, inParam); |
|||
} |
|||
} |
|||
|
|||
|
|||
function saveFile(doa, reqR) { |
|||
// 修改详情信息
|
|||
const newDetail = doa.detail; |
|||
newDetail.binFileUrl = reqR.newReceiveMaterialId + ".bin"; |
|||
newDetail.materialId = reqR.newReceiveMaterialId; |
|||
|
|||
const newDetailStr = JSON.stringify(newDetail, "", "\t"); |
|||
|
|||
const newReceiveFullPath = path.join(reqR.detailDir, reqR.newReceiveMaterialId + ".json"); |
|||
fs.writeFileSync(newReceiveFullPath, newDetailStr); |
|||
|
|||
// 新生成的bin
|
|||
const newReceiveBinFullPath = path.join(reqR.binDir, reqR.newReceiveMaterialId + ".bin"); |
|||
fs.writeFileSync(newReceiveBinFullPath, doa.bin); |
|||
} |
|||
|
|||
function readAttr(dir, inParam, isAttr) { |
|||
const attrD = readJsonFile(dir); |
|||
for (const iterator of attrD) { |
|||
inParam.tvoM.createFTVByData(iterator, isAttr); |
|||
} |
|||
} |
|||
|
|||
function readExtends(reqR, inParam) { |
|||
const attrD = readJsonFile(reqR.extendDir); |
|||
inParam.extendsRuleM.initAttrMap(attrD); |
|||
} |
|||
|
|||
function readParam(reqR, inParam) { |
|||
const fns = fs.readdirSync(reqR.paramDir); |
|||
fns.forEach(fileName => { |
|||
const filedir = path.join(reqR.paramDir, fileName); |
|||
const stats = fs.statSync(filedir); |
|||
if (stats.isFile()) { |
|||
const fn = path.parse(filedir).name; |
|||
const paramDetail = readJsonFile(filedir); |
|||
inParam.setParamDetail(fn, paramDetail); |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/** 初始化参数 */ |
|||
function initParam(reqR) { |
|||
const inParam = new ut.model_unittest.DSdkInArgv(); |
|||
inParam.reveiveId = reqR.receiveMaterialId; |
|||
inParam.req = reqR; |
|||
|
|||
readParam(reqR, inParam); |
|||
readDetail(reqR, inParam.reveiveId, inParam); |
|||
readAttr(reqR.attrDir, inParam, true); |
|||
readAttr(reqR.categoryDir, inParam, false); |
|||
readExtends(reqR, inParam); |
|||
return inParam; |
|||
} |
|||
|
|||
// 推送素材
|
|||
function pushMaterial(reqR) { |
|||
const inParam = initParam(reqR); |
|||
inParam.pushId = reqR.pushMaterialId; |
|||
inParam.pushIds = reqR.pushMaterialIdList; |
|||
for (const pid of inParam.pushIds) { |
|||
readDetail(reqR, pid, inParam); |
|||
} |
|||
if (reqR.materialRelationParamVOList != null) { |
|||
for (const rpvo of reqR.materialRelationParamVOList) { |
|||
if (inParam.checkIsMat(rpvo.paramType)) { |
|||
readMatDetail(reqR, rpvo.paramDefaultValue, inParam); |
|||
} |
|||
else if (inParam.checkIsStyle(rpvo.paramType)) { |
|||
readDetail(reqR, rpvo.paramDefaultValue, inParam); |
|||
} |
|||
} |
|||
} |
|||
if (reqR.relationMaterialIdList != null) { |
|||
for (let index = 0; index < reqR.relationMaterialIdList.length; index++) { |
|||
const element = reqR.relationMaterialIdList[index]; |
|||
readDetail(reqR, element, inParam) |
|||
} |
|||
} |
|||
const etd = new ut.model_unittest.UpdateAuto(inParam.decoder); |
|||
let doa = etd.startPushMaterial(inParam); |
|||
if (doa == null) { |
|||
doa = createDefDoa(inParam); |
|||
} |
|||
saveFile(doa, reqR); |
|||
} |
|||
|
|||
function createDefDoa(inParam) { |
|||
const bin = inParam.getBin(inParam.reveiveId); |
|||
const detail = inParam.getDetail(inParam.reveiveId); |
|||
|
|||
return { detail, bin } |
|||
} |
|||
|
|||
function handleDir(reqR, workDir) { |
|||
if (workDir != null) { |
|||
reqR.binDir = workDir + reqR.binDir; |
|||
reqR.detailDir = workDir + reqR.detailDir; |
|||
reqR.paramDir = workDir + reqR.paramDir; |
|||
reqR.reqFileUrl = workDir + reqR.reqFileUrl; |
|||
reqR.resFileUrl = workDir + reqR.resFileUrl; |
|||
reqR.attrDir = workDir + reqR.attrDir; |
|||
reqR.categoryDir = workDir + reqR.categoryDir; |
|||
reqR.extendDir = workDir + reqR.extendDir; |
|||
} |
|||
} |
|||
|
|||
function doPush(reqR, workDir = "") { |
|||
console.time("doPush") |
|||
comd.model_d.UClassTree.rootModule = comd.model_d.UClassTree.getNodeJsRootModule(module); |
|||
// setTimeout(()=>{
|
|||
handleDir(reqR, workDir); |
|||
if (reqR.materialRelationParamVOList != null) { |
|||
for (const o of reqR.materialRelationParamVOList) { |
|||
o.paramDefaultValue = o.paramDefaultValueVersion; |
|||
} |
|||
} |
|||
pushMaterial(reqR); |
|||
console.timeEnd("doPush") |
|||
// },1000)
|
|||
} |
|||
|
|||
function tryReadReq(reqR, workDir = "") { |
|||
let isSuccess = true; |
|||
let code = 200; |
|||
let msg = "来自node的回复"; |
|||
try { |
|||
doPush(reqR, workDir); |
|||
} catch (error) { |
|||
isSuccess = false; |
|||
msg = error; |
|||
code = -1 |
|||
} |
|||
writeRes(reqR.resFileUrl, code, msg, isSuccess); |
|||
} |
|||
|
|||
function writeRes(pathStr, code, msg, isSuccess) { |
|||
const resD = { |
|||
"code": code, |
|||
"data": {}, |
|||
"msg": msg.stack, |
|||
"success": isSuccess |
|||
} |
|||
const resDStr = JSON.stringify(resD, "", "\t") |
|||
fs.writeFileSync(pathStr, resDStr) |
|||
} |
|||
|
|||
function readArgv(reqPath) { |
|||
// const reqPath = process.argv[2];
|
|||
const reqR = readJsonFile(reqPath); |
|||
tryReadReq(reqR); |
|||
} |
|||
|
|||
function localRun(reqPath) { |
|||
// 项目工程地址
|
|||
console.log('当前文件根目录:', __dirname) |
|||
console.log('当前文件根目录:', path.join(`${__dirname}/data/material_auto/upload/20240513143213139802/req/0297505113_62.json`)) |
|||
const p = path.join(`${__dirname}/data/material_auto/upload/20240513143213139802/req/0297505113_62.json`); //
|
|||
// const reqR = readJsonFile(resolve('./sdk/data/material_auto/upload/20240513143213139802/req/0297505113_62.json'));
|
|||
const reqR = readJsonFile(p); |
|||
// tryReadReq(reqR, __dirname);
|
|||
doPush(reqR, __dirname); |
|||
} |
|||
|
|||
// readArgv()
|
|||
// localRun()
|
|||
|
|||
const autoUpdate = module.exports = { |
|||
localRun: localRun, |
|||
readArgvs: readArgv |
|||
}; |
@ -0,0 +1,21 @@ |
|||
const axios = require('axios'); |
|||
|
|||
//下载资源文件流
|
|||
async function downloadFileToBuffer(url) { |
|||
try { |
|||
// 发起 GET 请求并设置 responseType 为 'arraybuffer'
|
|||
const response = await axios({ |
|||
method: 'get', |
|||
url, |
|||
responseType: 'arraybuffer' |
|||
}); |
|||
console.log("数据流", response.data); |
|||
return response.data; |
|||
} catch (error) { |
|||
throw new Error(`Error downloading file from ${url}: ${error.message}`); |
|||
} |
|||
} |
|||
|
|||
module.exports = { |
|||
downloadFileToBuffer: downloadFileToBuffer |
|||
} |
Loading…
Reference in new issue