refactor: asset service & markdown render asset

This commit is contained in:
Innei
2021-09-27 10:55:58 +08:00
parent 28ec5ae177
commit 29618d27f5
17 changed files with 72 additions and 880 deletions

View File

@@ -1,8 +1,6 @@
import { Injectable, Logger } from '@nestjs/common'
import { render } from 'ejs'
import { writeFileSync } from 'fs'
import { createTransport } from 'nodemailer'
import path from 'path'
import { ConfigsService } from '~/modules/configs/configs.service'
import { LinkModel } from '~/modules/link/link.model'
import { AssetService } from './hepler.asset.service'
@@ -47,20 +45,14 @@ export class EmailService {
writeTemplate(type: ReplyMailType, source: string) {
switch (type) {
case ReplyMailType.Guest:
return writeFileSync(
path.resolve(
process.cwd(),
'assets/email-template/guest.template.ejs',
),
return this.assetService.writeAsset(
'/email-template/guest.template.ejs',
source,
{ encoding: 'utf-8' },
)
case ReplyMailType.Owner:
return writeFileSync(
path.resolve(
process.cwd(),
'assets/email-template/owner.template.ejs',
),
return this.assetService.writeAsset(
'/email-template/owner.template.ejs',
source,
{ encoding: 'utf-8' },
)

View File

@@ -4,49 +4,65 @@
* @description 用于获取静态资源的服务
*/
import { Injectable, Logger } from '@nestjs/common'
import fs, { mkdirSync } from 'fs'
import fs from 'fs'
import path, { join } from 'path'
import { ASSET_DIR } from '~/constants/path.constant'
import { HttpService } from './helper.http.service'
// 先从 ASSET_DIR 找用户自定义的资源, 没有就从默认的 ASSET_DIR 找, 没有就从网上拉取, 存到默认的 ASSET_DIR
@Injectable()
export class AssetService {
private logger: Logger
constructor(private readonly httpService: HttpService) {
this.logger = new Logger(AssetService.name)
if (!this.checkRoot()) {
this.logger.log('资源目录不存在,创建资源目录')
mkdirSync(this.assetPath, { recursive: true })
}
}
public assetPath = path.resolve(process.cwd(), 'assets')
public embedAssetPath = path.resolve(process.cwd(), 'assets')
// 在线资源的地址 `/` 结尾
private onlineAssetPath =
'https://cdn.jsdelivr.net/gh/mx-space/assets@master/'
private checkRoot() {
if (!fs.existsSync(this.assetPath)) {
if (!fs.existsSync(this.embedAssetPath)) {
return false
}
return true
}
/**
* 找默认资源
* @param path 资源路径
* @returns
*/
private checkAssetPath(path: string) {
if (!this.checkRoot()) {
return false
}
path = join(this.assetPath, path)
path = join(this.embedAssetPath, path)
if (!fs.existsSync(path)) {
return false
}
return true
}
private async getUserCustomAsset(
path: string,
options: Parameters<typeof fs.readFileSync>[1],
) {
if (fs.existsSync(join(ASSET_DIR, path))) {
return fs.readFileSync(join(ASSET_DIR, path), options)
}
return null
}
public async getAsset(
path: string,
options: Parameters<typeof fs.readFileSync>[1],
) {
// 想找用户自定义的资源入口
if (await this.getUserCustomAsset(path, options)) {
return this.getUserCustomAsset(path, options)
}
if (!this.checkAssetPath(path)) {
try {
// 去线上拉取
@@ -56,18 +72,33 @@ export class AssetService {
fs.mkdirSync(
(() => {
const p = join(this.assetPath, path).split('/')
const p = join(this.embedAssetPath, path).split('/')
return p.slice(0, p.length - 1).join('/')
})(),
{ recursive: true },
)
fs.writeFileSync(join(this.assetPath, path), data, options)
fs.writeFileSync(join(this.embedAssetPath, path), data, options)
return data
} catch (e) {
this.logger.error('本地资源不存在,线上资源无法拉取')
throw e
}
}
return fs.readFileSync(join(this.assetPath, path), options)
return fs.readFileSync(join(this.embedAssetPath, path), options)
}
public writeAsset(
path: string,
data: any,
options: Parameters<typeof fs.writeFileSync>[2],
) {
fs.mkdirSync(
(() => {
const p = join(ASSET_DIR, path).split('/')
return p.slice(0, p.length - 1).join('/')
})(),
{ recursive: true },
)
fs.writeFileSync(join(ASSET_DIR, path), data, options)
}
}