处理不同路径下的图片时,可能需要保存执行的情况。例如 A 目录在当前时间点进行维护,获取了所有的图片,下次读取 A 目录时,应当从上次维护的时间开始获取最新的图片。现在新加了 B 目录,如果从上次维护 A 的时间点开始读取图片的话,在时间点之前的图片将无法上传。因此,针对不同文件夹的维护,可以分别建立一个单独的数据项。
let collectionPaths = config.generateCollectionIndex.path; if (!Array.isArray(collectionPaths)) { collectionPaths = [collectionPaths]; }
// Get filenames in collection paths let allFiles = []; for (const collectionPath of collectionPaths) { let files = []; files = files.concat(awaitreaddir(collectionPath));
// Only keep files with Pixiv naming style const reg = /^\d+_p\d+.(jpg|png|gif)$/; files = files.filter((filename) => { return reg.test(filename); });
// Get file stat and resolve file info for (let i = 0; i < files.length; i++) { const filename = files[i]; const filePath = path.join(collectionPath, filename);
// Only keep files that recently saved const serviceProcess = awaitServiceProcess.findOne({ where: { serviceName, serviceConfig: collectionPath }, }); if (serviceProcess) { // Only update or create Pixiv artwork that saved after last time this service is done let lastUpdateIndexTime = serviceProcess.dataValues.lastExecAt; if (lastUpdateIndexTime) { lastUpdateIndexTime = newDate(lastUpdateIndexTime).getTime(); files = files.filter((pic) => { return pic.picCreatedAt > lastUpdateIndexTime; }); } } else { awaitServiceProcess.create({ serviceName, serviceConfig: collectionPath, }); }
allFiles = allFiles.concat(files); }
const updateIndexAt = newDate().toISOString();
// Update or create pic index for (const picFile of allFiles) { // 注意:此处的 updateOrCreate() 为笔者自定义的方法 // 其作用为:当存在 item 时,更新 item;不存在时,创建 item await sequelize.updateOrCreate( ServicePixivCollection, { picId: picFile.picId, picIndex: picFile.picIndex, }, picFile, ); }
// Update service process record ServiceProcess.update( { lastExecAt: updateIndexAt, }, { where: { serviceName }, }, ); ServiceProcess.increment("haveExecTime", { where: { serviceName } }); };
msgReplied = true; } catch (err) { if (picIndex == 0) { try { // Comic mode artwork with index=0 may send failed // Use comic mode url instead const picProxyUrl = `https://pixiv.cat/${picId}-1.${picType}`; await bot.sendPhoto(chatId, picProxyUrl, sendPhotoOptions);
msgReplied = true; } catch (err) { // } } } }
// Artwork size is not smaller than 5 MB or send failed again, // send caption message if (!msgReplied) { await bot.sendMessage(chatId, caption, { parse_mode: "MarkdownV2", disable_web_page_preview: false, }); }
// Remove placeholder message bot.deleteMessage(chatId, placeholderMessage.message_id); } else { bot.sendMessage( chatId, "Get random pixiv artwork failed. You may try to call it again later!", ); } });