help prs, why do i get ENOENT on second downloadFile in downloadPlugin?

This commit is contained in:
Daniel Hübleitner
2019-08-01 16:56:53 +02:00
parent ea346fe0cb
commit 4edf6c1a7f
5 changed files with 388 additions and 74 deletions
+9 -59
View File
@@ -3,46 +3,26 @@
import * as Logger from 'log4js'
import { Plugin, socketioRPC } from 'frontblock-generic/Plugin';
import { ErrorResponse, SuccessResponse } from 'frontblock-generic/Types';
import { PluginManager } from './PluginManager'
import express = require('express');
import http = require('http');
import fs = require('fs');
import bsock = require('bsock');
import fetch = require("node-fetch");
const kfs = require("key-file-storage").default('kfs')
type pluginEntry =
{
pluginName:string
}
const pluginList:pluginEntry[] =
[
//{ pluginName: 'apiclient' },
//{ pluginName: 'paymentmanager' },
//{ pluginName: 'wallet' },
{ pluginName: 'htmlsupplier' }
]
type releaseEntry =
{
tagName: string,
staticZipURL: URL,
checksumFileURL: URL
}
Logger.configure({
appenders:
{
"admin": { type: 'stdout' },
"frontblock-admin": { type: 'stdout' },
//app: { type: 'file', filename: 'application.log' }
},
categories:
{
default: { appenders: [ 'admin' ], level: 'debug' }
default: { appenders: [ 'frontblock-admin' ], level: 'debug' }
}
})
const logger = Logger.getLogger("admin")
const logger = Logger.getLogger("frontblock-admin")
type hookRPC =
{
@@ -80,58 +60,28 @@ export type rpcInfo =
* The list of available plugins is published via the frontblock API and downloaded via gitea-releases
*/
export class FrontblockAdmin{
private plugins: Plugin[]
private plugins: Plugin[] = []
private hookToUids:{[hookName:string]:string[]} = {}
private express
private httpServer
private io = bsock.createServer()
private wsServer = http.createServer()
private pm: PluginManager
constructor(){
if(!('admin.conf' in kfs)){
logger.info('Generating ./kfs/admin.conf')
kfs['admin.conf'] = { httpPort: 8080 }
}
this.pm = new PluginManager()
this.initialize()
}
private async initialize(){
const wat = await this.fetchReleaseList('htmlsupplier')
logger.debug(wat)
//await this.downloadPlugins()
//this.plugins.forEach(plugin => plugin.start())
//this.startWebsocket()
//this.startWebserver()
}
private async fetchReleaseList(pluginName:string): Promise<releaseEntry[]>{
const res = await fetch('https://gitea.frontblock.me/api/v1/repos/fb-vendor/' + pluginName + '/releases')
const json = await res.json();
const releaseList:releaseEntry[] = []
json.forEach((release) => {
const tagName = release.tag_name
const staticZipURL = release.assets.filter(asset => asset.name === pluginName + '.zip')[0].browser_download_url
const checksumFileURL = release.assets.filter(asset => asset.name === 'md5sum.txt')[0].browser_download_url
releaseList.push({tagName,staticZipURL,checksumFileURL})
})
return releaseList
}
private async downloadPlugin(release:releaseEntry) {
}
private async verifyPlugin(release:releaseEntry) {
}
private async extractPlugin(release:releaseEntry) {
}
private async installPlugin(release:releaseEntry) {
}
private initApis(socket){
//Declare own functions
const rpcInfos:rpcInfo[] = [
@@ -234,7 +184,7 @@ export class FrontblockAdmin{
private restartWebserver(port:number){
this.stopWebserver()
kfs.FrontblockAdmin = { httpPort: port }
kfs['admin.conf'] = { httpPort: port }
this.startWebserver()
}
+136
View File
@@ -0,0 +1,136 @@
'use strict'
import * as Logger from 'log4js';
import fetch = require('node-fetch');
import fs = require('fs');
import path = require('path')
import unzip = require('unzip')
Logger.configure({
appenders:
{
"plugin-manager": { type: 'stdout' },
//app: { type: 'file', filename: 'application.log' }
},
categories:
{
default: { appenders: [ 'plugin-manager' ], level: 'debug' }
}
})
const logger = Logger.getLogger("plugin-manager")
export class PluginManager{
private installDir: string
private cacheDir: string
private loadedPlugins: string[]
constructor(installDir: string = './plugins'){
this.installDir = installDir
this.cacheDir = path.join(installDir, '.cache')
this.initialize()
}
private initialize() {
logger.info('Initializing manager')
try {
fs.access(this.cacheDir, async err => {
if (err && err.code === 'ENOENT') {
await fs.promises.mkdir(this.cacheDir, {recursive: true})
logger.info('Manager installed to ' + this.installDir)
} else {
logger.info('Manager already installed at ' + this.installDir)
}
})
} catch (error) {
logger.error(error)
}
}
public async getAvailablePlugins(): Promise<string[] | void> {
return [
'apiclient',
'paymentmanager',
'wallet',
'htmlsupplier'
]
}
public async getInstalledPlugins(): Promise<string[] | void> {
try {
const installed = await fs.promises.readdir(this.installDir)
return installed.filter( entry => entry[0] != '.')
} catch (error) {
logger.error(error)
return
}
}
public async getLoadedPlugins(): Promise<string[] | void> {
return this.loadedPlugins
}
private async downloadFile(url: URL, dest: string) {
try {
fs.access(path.dirname(dest), err => {
if (err && err.code === 'ENOENT') {
fs.mkdirSync(path.dirname(dest), {recursive: true})
}
})
const file = fs.createWriteStream(dest)
file.on('error', err => {throw new Error(err)})
const response = await fetch(url)
if (!response.ok) throw new Error(response.statusText)
response.body.pipe(file)
} catch (error) {
logger.error(error)
await fs.promises.unlink(dest)
}
}
private async downloadPlugin(pluginName: string) {
try {
const response = await fetch('https://gitea.frontblock.me/api/v1/repos/fb-vendor/' + pluginName + '/releases')
if (!response.ok) throw new Error(response.statusText)
const releases = await response.json()
if (!releases || !releases.length) throw new Error('Empty response')
const latest = releases[0]
const version = latest.tag_name
const archiveURL = latest.assets.filter(asset => asset.name === pluginName + '.zip')[0].browser_download_url
const checksumURL = latest.assets.filter(asset => asset.name === 'md5sum.txt')[0].browser_download_url
if (!version || ! archiveURL || !checksumURL) throw new Error('Malformed response')
await this.downloadFile(archiveURL, path.join(this.cacheDir, pluginName, version, 'plugin.zip'))
await this.downloadFile(checksumURL, path.join(this.cacheDir, pluginName, version, 'md5sum.txt'))
} catch (error) {
logger.error(error)
}
}
public async installPlugin(pluginName: string) {
try {
await this.downloadPlugin(pluginName)
} catch (error) {
logger.debug(error)
}
}
public uninstallPlugin(pluginName: string) {
}
public async loadPlugin() {
}
public async unloadPlugin() {
}
}