mirror of
https://github.com/Afilmory/afilmory
synced 2026-02-01 22:48:17 +00:00
feat: add image processing error handling and logging
- Introduced a new error code for image processing failures. - Enhanced the DataSyncService to log effective storage configurations and errors during processing. - Updated the PhotoBuilderService to remove unused methods and streamline the code. - Refactored plugin loading to support ESM imports for better compatibility. Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
@@ -14,6 +14,9 @@ export enum ErrorCode {
|
||||
TENANT_NOT_FOUND = 20,
|
||||
TENANT_SUSPENDED = 21,
|
||||
TENANT_INACTIVE = 22,
|
||||
|
||||
// Image Processing
|
||||
IMAGE_PROCESSING_FAILED = 30,
|
||||
}
|
||||
|
||||
export interface ErrorDescriptor {
|
||||
@@ -62,4 +65,9 @@ export const ERROR_CODE_DESCRIPTORS: Record<ErrorCode, ErrorDescriptor> = {
|
||||
httpStatus: 403,
|
||||
message: 'Tenant is not active',
|
||||
},
|
||||
|
||||
[ErrorCode.IMAGE_PROCESSING_FAILED]: {
|
||||
httpStatus: 500,
|
||||
message: 'Image processing failed',
|
||||
},
|
||||
}
|
||||
|
||||
@@ -1,17 +1,21 @@
|
||||
import type {BuilderConfig, PhotoManifestItem, StorageConfig, StorageManager, StorageObject} from '@afilmory/builder';
|
||||
import {
|
||||
createDefaultBuilderConfig,
|
||||
StorageFactory
|
||||
} from '@afilmory/builder'
|
||||
import type { BuilderConfig, PhotoManifestItem, StorageConfig, StorageManager, StorageObject } from '@afilmory/builder'
|
||||
import { createDefaultBuilderConfig, StorageFactory } from '@afilmory/builder'
|
||||
import {
|
||||
EagleStorageProvider,
|
||||
GitHubStorageProvider,
|
||||
LocalStorageProvider,
|
||||
S3StorageProvider,
|
||||
} from '@afilmory/builder/storage'
|
||||
import type { EagleConfig, EagleRule, GitHubConfig, LocalConfig, S3Config } from '@afilmory/builder/storage/interfaces'
|
||||
} from '@afilmory/builder/storage/index.js'
|
||||
import type {
|
||||
EagleConfig,
|
||||
EagleRule,
|
||||
GitHubConfig,
|
||||
LocalConfig,
|
||||
S3Config,
|
||||
} from '@afilmory/builder/storage/interfaces.js'
|
||||
import type { PhotoAssetConflictPayload, PhotoAssetConflictSnapshot, PhotoAssetManifest } from '@afilmory/db'
|
||||
import { CURRENT_PHOTO_MANIFEST_VERSION, photoAssets } from '@afilmory/db'
|
||||
import { createLogger } from '@afilmory/framework'
|
||||
import { BizException, ErrorCode } from 'core/errors'
|
||||
import { PhotoBuilderService } from 'core/modules/photo/photo.service'
|
||||
import { and, eq } from 'drizzle-orm'
|
||||
@@ -68,6 +72,7 @@ interface SyncPreparation {
|
||||
|
||||
@injectable()
|
||||
export class DataSyncService {
|
||||
private readonly logger = createLogger('DataSyncService')
|
||||
constructor(
|
||||
private readonly dbAccessor: DbAccessor,
|
||||
private readonly photoBuilderService: PhotoBuilderService,
|
||||
@@ -143,6 +148,8 @@ export class DataSyncService {
|
||||
): Promise<SyncPreparation> {
|
||||
const builder = this.photoBuilderService.createBuilder(builderConfig)
|
||||
const effectiveStorageConfig = storageConfig ?? builderConfig.storage
|
||||
|
||||
this.logger.verbose('effectiveStorageConfig', effectiveStorageConfig)
|
||||
this.registerStorageProviderPlugin(builder, effectiveStorageConfig)
|
||||
if (storageConfig) {
|
||||
this.photoBuilderService.applyStorageConfig(builder, storageConfig)
|
||||
@@ -222,7 +229,7 @@ export class DataSyncService {
|
||||
break
|
||||
}
|
||||
default: {
|
||||
const provider = storageConfig.provider as string
|
||||
const provider = (storageConfig as StorageConfig)?.provider as string
|
||||
const registered = StorageFactory.getRegisteredProviders()
|
||||
if (!registered.includes(provider)) {
|
||||
throw new BizException(ErrorCode.COMMON_BAD_REQUEST, {
|
||||
@@ -777,7 +784,8 @@ export class DataSyncService {
|
||||
},
|
||||
builder,
|
||||
})
|
||||
} catch {
|
||||
} catch (err) {
|
||||
this.logger.error('Failed to process storage object', err)
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -964,7 +972,7 @@ export class DataSyncService {
|
||||
const storageObject = storageObjects.find((object) => object.key === record.storageKey)
|
||||
|
||||
if (!storageObject) {
|
||||
throw new BizException(ErrorCode.COMMON_CONFLICT, {
|
||||
throw new BizException(ErrorCode.IMAGE_PROCESSING_FAILED, {
|
||||
message: 'Storage object no longer exists; rerun data sync before resolving.',
|
||||
})
|
||||
}
|
||||
@@ -973,7 +981,7 @@ export class DataSyncService {
|
||||
existing: record.manifest?.data as PhotoManifestItem | undefined,
|
||||
})
|
||||
if (!processResult?.item) {
|
||||
throw new BizException(ErrorCode.COMMON_CONFLICT, { message: 'Failed to reprocess storage object.' })
|
||||
throw new BizException(ErrorCode.IMAGE_PROCESSING_FAILED, { message: 'Failed to reprocess storage object.' })
|
||||
}
|
||||
|
||||
const storageSnapshot = this.createStorageSnapshot(storageObject)
|
||||
|
||||
@@ -6,12 +6,11 @@ import type {
|
||||
PhotoProcessorOptions,
|
||||
StorageConfig,
|
||||
StorageObject,
|
||||
StorageProvider,
|
||||
} from '@afilmory/builder'
|
||||
import { AfilmoryBuilder, processPhotoWithPipeline, StorageFactory, StorageManager } from '@afilmory/builder'
|
||||
import type { Logger as BuilderLogger } from '@afilmory/builder/logger'
|
||||
import type { PhotoProcessingLoggers } from '@afilmory/builder/photo'
|
||||
import { createPhotoProcessingLoggers, setGlobalLoggers } from '@afilmory/builder/photo'
|
||||
import { AfilmoryBuilder, processPhotoWithPipeline } from '@afilmory/builder'
|
||||
import type { Logger as BuilderLogger } from '@afilmory/builder/logger/index.js'
|
||||
import type { PhotoProcessingLoggers } from '@afilmory/builder/photo/index.js'
|
||||
import { createPhotoProcessingLoggers, setGlobalLoggers } from '@afilmory/builder/photo/index.js'
|
||||
import type { _Object } from '@aws-sdk/client-s3'
|
||||
import { injectable } from 'tsyringe'
|
||||
|
||||
@@ -42,14 +41,6 @@ export class PhotoBuilderService {
|
||||
return new AfilmoryBuilder(config)
|
||||
}
|
||||
|
||||
createStorageManager(config: StorageConfig): StorageManager {
|
||||
return new StorageManager(config)
|
||||
}
|
||||
|
||||
resolveStorageProvider(config: StorageConfig): StorageProvider {
|
||||
return StorageFactory.createProvider(config)
|
||||
}
|
||||
|
||||
applyStorageConfig(builder: AfilmoryBuilder, config: StorageConfig): void {
|
||||
builder.getStorageManager().switchProvider(config)
|
||||
}
|
||||
|
||||
@@ -15,9 +15,9 @@ import type { PluginRunState } from '../plugins/manager.js'
|
||||
import { PluginManager } from '../plugins/manager.js'
|
||||
import type {
|
||||
BuilderPluginConfigEntry,
|
||||
BuilderPluginESMImporter,
|
||||
BuilderPluginEventPayloads,
|
||||
} from '../plugins/types.js'
|
||||
import { isPluginReferenceObject } from '../plugins/types.js'
|
||||
import type { StorageProviderFactory } from '../storage/factory.js'
|
||||
import { StorageFactory, StorageManager } from '../storage/index.js'
|
||||
import type { BuilderConfig } from '../types/config.js'
|
||||
@@ -555,14 +555,6 @@ export class AfilmoryBuilder {
|
||||
return
|
||||
}
|
||||
|
||||
if (isPluginReferenceObject(ref)) {
|
||||
const key = ref.resolve
|
||||
if (seen.has(key)) return
|
||||
seen.add(key)
|
||||
references.push(ref)
|
||||
return
|
||||
}
|
||||
|
||||
const pluginName = ref.name
|
||||
if (pluginName) {
|
||||
const key = `plugin:${pluginName}`
|
||||
@@ -576,7 +568,7 @@ export class AfilmoryBuilder {
|
||||
|
||||
const hasPluginWithName = (name: string): boolean => {
|
||||
return references.some((ref) => {
|
||||
if (typeof ref === 'string' || isPluginReferenceObject(ref)) {
|
||||
if (typeof ref === 'string') {
|
||||
return false
|
||||
}
|
||||
return ref.name === name
|
||||
@@ -591,14 +583,16 @@ export class AfilmoryBuilder {
|
||||
this.config.repo.enable &&
|
||||
!hasPluginWithName('afilmory:github-repo-sync')
|
||||
) {
|
||||
addReference('@afilmory/builder/plugins/github-repo-sync')
|
||||
addReference(
|
||||
() => import('@afilmory/builder/plugins/github-repo-sync.js'),
|
||||
)
|
||||
}
|
||||
|
||||
const storagePluginByProvider: Record<string, string> = {
|
||||
s3: '@afilmory/builder/plugins/storage/s3',
|
||||
github: '@afilmory/builder/plugins/storage/github',
|
||||
eagle: '@afilmory/builder/plugins/storage/eagle',
|
||||
local: '@afilmory/builder/plugins/storage/local',
|
||||
const storagePluginByProvider: Record<string, BuilderPluginESMImporter> = {
|
||||
s3: () => import('@afilmory/builder/plugins/storage/s3.js'),
|
||||
github: () => import('@afilmory/builder/plugins/storage/github.js'),
|
||||
eagle: () => import('@afilmory/builder/plugins/storage/eagle.js'),
|
||||
local: () => import('@afilmory/builder/plugins/storage/local.js'),
|
||||
}
|
||||
|
||||
const storageProvider = this.config.storage.provider
|
||||
|
||||
@@ -96,10 +96,7 @@ export async function processImageWithSharp(
|
||||
if (isBitmap(imageBuffer)) {
|
||||
try {
|
||||
// Convert the BMP image to JPEG format and create a new Sharp instance for the converted image.
|
||||
sharpInstance = await convertBmpToJpegSharpInstance(
|
||||
imageBuffer,
|
||||
loggers.image.originalLogger,
|
||||
)
|
||||
sharpInstance = await convertBmpToJpegSharpInstance(imageBuffer)
|
||||
// Update the image buffer to reflect the new JPEG data from the Sharp instance.
|
||||
processedBuffer = await sharpInstance.toBuffer()
|
||||
} catch (error) {
|
||||
@@ -109,10 +106,7 @@ export async function processImageWithSharp(
|
||||
}
|
||||
|
||||
// 获取图片元数据(复用 Sharp 实例)
|
||||
const metadata = await getImageMetadataWithSharp(
|
||||
sharpInstance,
|
||||
loggers.image.originalLogger,
|
||||
)
|
||||
const metadata = await getImageMetadataWithSharp(sharpInstance)
|
||||
if (!metadata) {
|
||||
loggers.image.error(`获取图片元数据失败:${photoKey}`)
|
||||
return null
|
||||
|
||||
@@ -5,10 +5,11 @@ import { pathToFileURL } from 'node:url'
|
||||
import type {
|
||||
BuilderPlugin,
|
||||
BuilderPluginConfigEntry,
|
||||
BuilderPluginESMImporter,
|
||||
BuilderPluginHooks,
|
||||
BuilderPluginReference,
|
||||
} from './types.js'
|
||||
import { isPluginReferenceObject } from './types.js'
|
||||
import { isPluginESMImporter } from './types.js'
|
||||
|
||||
const requireResolver = createRequire(import.meta.url)
|
||||
|
||||
@@ -26,16 +27,12 @@ interface NormalizedDescriptor {
|
||||
|
||||
function normalizeDescriptor(
|
||||
ref: BuilderPluginReference,
|
||||
): NormalizedDescriptor {
|
||||
): NormalizedDescriptor | BuilderPluginESMImporter {
|
||||
if (typeof ref === 'string') {
|
||||
return { specifier: ref }
|
||||
}
|
||||
|
||||
return {
|
||||
specifier: ref.resolve,
|
||||
name: ref.name,
|
||||
options: ref.options,
|
||||
}
|
||||
return ref
|
||||
}
|
||||
|
||||
function resolveSpecifier(
|
||||
@@ -127,8 +124,23 @@ export async function loadPlugins(
|
||||
const results: LoadedPluginDefinition[] = []
|
||||
|
||||
for (const entry of entries) {
|
||||
if (typeof entry === 'string' || isPluginReferenceObject(entry)) {
|
||||
if (typeof entry === 'string') {
|
||||
const descriptor = normalizeDescriptor(entry)
|
||||
|
||||
if (isPluginESMImporter(descriptor)) {
|
||||
const { default: pluginFactoryOrPlugin } = await descriptor()
|
||||
const plugin = await instantiatePlugin(pluginFactoryOrPlugin)
|
||||
const hooks = normalizeHooks(plugin)
|
||||
const name = plugin.name || `lazy-loaded-plugin-${results.length}`
|
||||
|
||||
results.push({
|
||||
name,
|
||||
hooks,
|
||||
pluginOptions: undefined,
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
const { resolvedPath } = resolveSpecifier(descriptor.specifier, baseDir)
|
||||
|
||||
const mod = await importModule(resolvedPath)
|
||||
|
||||
@@ -15,19 +15,10 @@ import type {
|
||||
} from '../types/manifest.js'
|
||||
import type { PhotoManifestItem, ProcessPhotoResult } from '../types/photo.js'
|
||||
|
||||
export type BuilderPluginReference =
|
||||
| string
|
||||
| {
|
||||
resolve: string
|
||||
/**
|
||||
* Optional name override for the plugin. Falls back to the resolved name.
|
||||
*/
|
||||
name?: string
|
||||
/**
|
||||
* Arbitrary configuration passed to the plugin factory.
|
||||
*/
|
||||
options?: unknown
|
||||
}
|
||||
export type BuilderPluginESMImporter = () => Promise<{
|
||||
default: (() => BuilderPlugin | Promise<BuilderPlugin>) | BuilderPlugin
|
||||
}>
|
||||
export type BuilderPluginReference = string | BuilderPluginESMImporter
|
||||
|
||||
export type BuilderPluginConfigEntry = BuilderPluginReference | BuilderPlugin
|
||||
|
||||
@@ -185,8 +176,8 @@ export type BuilderPluginFactory =
|
||||
| (() => BuilderPlugin | Promise<BuilderPlugin>)
|
||||
| ((options: unknown) => BuilderPlugin | Promise<BuilderPlugin>)
|
||||
|
||||
export function isPluginReferenceObject(
|
||||
export function isPluginESMImporter(
|
||||
value: BuilderPluginConfigEntry,
|
||||
): value is Exclude<BuilderPluginReference, string> {
|
||||
return typeof value === 'object' && value !== null && 'resolve' in value
|
||||
): value is BuilderPluginESMImporter {
|
||||
return typeof value === 'function' && value.length === 0
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user