Skip to content

Commit

Permalink
修复同步地址错误可以创建Sheet的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Val-istar-Guo committed Aug 18, 2024
1 parent d88f16b commit 2c07878
Show file tree
Hide file tree
Showing 63 changed files with 776 additions and 654 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-berries-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opendoc/backend": patch
---

修复同步地址错误可以创建 Sheet 的问题
3 changes: 2 additions & 1 deletion app/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@nestjs/swagger": "^7.3.1",
"@nestjs/terminus": "^10.2.3",
"@types/semver": "^7.5.8",
"async": "^3.2.5",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"compressing": "^1.10.1",
Expand All @@ -62,7 +63,7 @@
"promise-writable": "^8.0.0",
"ramda": "^0.30.1",
"reflect-metadata": "^0.2.2",
"rev-hash": "^4.1.0",
"rev-hash": "^2.0.0",
"rxjs": "^7.8.1",
"semver": "^7.6.2",
"tempy": "^3.1.0",
Expand Down
20 changes: 14 additions & 6 deletions app/backend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/backend/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import { PluginModule } from './modules/plugin/plugin.module'
SheetVersionModule,
ApiFileModule,
SdkModule,
// RegistryModule,
OptionModule,
PluginModule,
],
Expand Down
2 changes: 2 additions & 0 deletions app/backend/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,7 @@ export class AppService {
await this.registerApplication()
await this.registerDocs()
await this.registerOpenapi(openapi)

await this.em.flush()
}
}
4 changes: 3 additions & 1 deletion app/backend/src/entities/base.entity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { OptionalProps, PrimaryKey, Property } from '@mikro-orm/core'
import { Config, DefineConfig, OptionalProps, PrimaryKey, Property } from '@mikro-orm/core'

export abstract class BaseEntity<Optional = never> {
[OptionalProps]?: 'createdAt' | 'updatedAt' | Optional
[Config]?: DefineConfig<{ forceObject: true }>


/**
* 主键
Expand Down
3 changes: 2 additions & 1 deletion app/backend/src/mikro-orm.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigModule } from '@buka/nestjs-config'
import * as util from 'util'
import { MySqlDriver, defineConfig } from '@mikro-orm/mysql'
import { FlushMode, MySqlDriver, defineConfig } from '@mikro-orm/mysql'
import { MysqlConfig } from './config/mysql.config'
import { Migrator } from '@mikro-orm/migrations'
import { BadRequestException } from '@nestjs/common'
Expand All @@ -16,6 +16,7 @@ export default (async function loadConfig() {
return defineConfig({
...config,
entities: ['dist/**/*.entity.js'],
flushMode: FlushMode.COMMIT,
extensions: [Migrator],
serialization: {
forceObject: true,
Expand Down
75 changes: 75 additions & 0 deletions app/backend/src/modules/api-file/api-file-storage.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { SheetVersionRepository } from './../sheet-version/repository/sheet-version.repository'
import * as path from 'path'
import { EntityManager, wrap } from '@mikro-orm/core'
import { MikroORM } from '@mikro-orm/mysql'
import { Injectable } from '@nestjs/common'
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino'
import { ApiFile } from './entities/api-file.entity'
import { StorageService } from '../storage/storage.service'
import { Readable } from 'stream'


/**
* 管理 ApiFile 的文件内容写入和读取
* 配合 MikroOrm 的 EntitySubscriber 在 ApiFile 创建/更新/删除后,再调用 StorageService 持久化文件
* 确保对 ApiFile 的文件内容的更改,在未保存到数据库前不会被持久化
*/
@Injectable()
export class ApiFileStorageService {
constructor(
@InjectPinoLogger(ApiFileStorageService.name)
private readonly logger: PinoLogger,


private readonly em: EntityManager,
private readonly orm: MikroORM,

private readonly storageService: StorageService,

private readonly sheetVersionRepo: SheetVersionRepository,
) {}

async getFilepath(apiFile: ApiFile): Promise<string> {
const sheetVersion = await apiFile.version.loadOrFail()
const filepath = path.join('api-file', apiFile.sheet.id, sheetVersion.string, apiFile.path)
return filepath
}

async readFile(apiFile: ApiFile): Promise<Buffer | null> {
if (apiFile.raw === null) {
if (!wrap(apiFile).isInitialized()) {
throw new Error('Cannot read ApiFile that is not initialized')
}

if (!apiFile.id) {
return null
}

const filepath = await this.getFilepath(apiFile)
apiFile.raw = await this.storageService.readFile(filepath)
}

return apiFile.raw
}

async createStream(apiFile: ApiFile): Promise<Readable> {
const filepath = await this.getFilepath(apiFile)
return this.storageService.createStream(filepath)
}

writeFile(apiFile: ApiFile, raw: Buffer): void {
apiFile.raw = raw
}

async flush(apiFile: ApiFile): Promise<void> {
if (apiFile.raw === null) return

const filepath = await this.getFilepath(apiFile)
await this.storageService.writeFile(filepath, apiFile.raw)
}

async removeFile(apiFile: ApiFile): Promise<void> {
const filepath = await this.getFilepath(apiFile)
await this.storageService.removeFile(filepath)
}
}
5 changes: 5 additions & 0 deletions app/backend/src/modules/api-file/api-file.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ import { ApiInternalServerErrorResponse, ApiOkResponse, ApiOperation, ApiTags }
import { QueryApiFilesDTO } from './dto/query-api-files.dto'
import { ResponseOfQueryApiFilesDTO } from './dto/response-of-query-api-files.dto'
import { ApiFile } from './entities/api-file.entity'
import { EntityManager } from '@mikro-orm/core'
import { MikroORM } from '@mikro-orm/mysql'


@ApiTags('API 文件')
@Controller('api-file')
@ApiInternalServerErrorResponse({ description: '系统异常' })
export class ApiFileController {
constructor(
private readonly em: EntityManager,
private readonly orm: MikroORM,

private readonly ApiFileService: ApiFileService,
) {}

Expand Down
3 changes: 2 additions & 1 deletion app/backend/src/modules/api-file/api-file.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { StorageModule } from '../storage/storage.module'
import { SheetVersion } from '../sheet-version/entities/sheet-version.entity'
import { SheetVersionModule } from '../sheet-version/sheet-version.module'
import { ApiFileSubscriber } from './api-file.subscriber'
import { ApiFileStorageService } from './api-file-storage.service'


@Module({
Expand All @@ -21,7 +22,7 @@ import { ApiFileSubscriber } from './api-file.subscriber'
SheetVersionModule,
],
controllers: [ApiFileController],
providers: [ApiFileService, ApiFileSubscriber],
providers: [ApiFileService, ApiFileStorageService, ApiFileSubscriber],
exports: [ApiFileService],
})
export class ApiFileModule {}
Loading

0 comments on commit 2c07878

Please sign in to comment.