| 14 | |
| 15 | let updatingMetaFromCivitAI = false; |
| 16 | class ModelManager { |
| 17 | constructor() { |
| 18 | channelService.on(CHANNELS.MAIN, CHANNEL_EVENTS.UPDATE_MODEL_META, (ev) => { |
| 19 | this.updateModelMeta(ev.payload); |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | getMetaFilePath = () => { |
| 24 | const appDir = getAppDataDir(); |
| 25 | return path.resolve(appDir, 'models.meta.json'); |
| 26 | } |
| 27 | |
| 28 | tryUpdateMetaFromCivitAI = async (dispatch: TaskEventDispatcher) => { |
| 29 | if (updatingMetaFromCivitAI) { |
| 30 | return; |
| 31 | } |
| 32 | updatingMetaFromCivitAI = true; |
| 33 | const metas = this.getModelMetas(); |
| 34 | const metaPath = this.getMetaFilePath(); |
| 35 | dispatch({ |
| 36 | message: "Updating model meta from CivitAI, you can config your api token key in settings pannel" |
| 37 | }); |
| 38 | try { |
| 39 | const modelFiles = await this.getAllInstalledModels(); |
| 40 | for (const modelType in modelFiles) { |
| 41 | const models = modelFiles[modelType]; |
| 42 | for (const model of models) { |
| 43 | const modelKey = this.getModelMetaKey(modelType, model.name) |
| 44 | if (!metas[modelKey]) { |
| 45 | await fetchFileInfo(modelKey, model.path); |
| 46 | } else { |
| 47 | const meta = metas[modelKey]; |
| 48 | if (meta.failed) { |
| 49 | const lastCheck = new Date(metas[modelKey].last_check); |
| 50 | if (new Date().getTime() - lastCheck.getTime() > 1000 * 60 * 60 * 24 * 3) { |
| 51 | await fetchFileInfo(modelKey, model.path); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | } catch (error) { |
| 58 | console.error('Failed to update model meta from CivitAI', error); |
| 59 | } |
| 60 | |
| 61 | updatingMetaFromCivitAI = false; |
| 62 | fs.writeFileSync(metaPath, JSON.stringify(metas, null, 2)); |
| 63 | |
| 64 | channelService.emit(CHANNELS.MAIN, { |
| 65 | type: CHANNEL_EVENTS.MODEL_META_UPDATED, |
| 66 | payload: { |
| 67 | message: "Model meta updated from CivitAI successrm " |
| 68 | } |
| 69 | }); |
| 70 | |
| 71 | async function fetchFileInfo(modelKey: string, file: string) { |
| 72 | try { |
| 73 | dispatch({ |
nothing calls this directly
no test coverage detected