feat: 媒体存储改为模板ID/档案编号层级并按人类可读命名归位(出库图独立追溯码子目录)
This commit is contained in:
@@ -23,6 +23,7 @@ interface MediaItem {
|
||||
key: string;
|
||||
name?: string;
|
||||
type?: string;
|
||||
url?: string;
|
||||
}
|
||||
|
||||
interface TraceNodeSnapshot {
|
||||
@@ -83,6 +84,80 @@ export class ArtworkService {
|
||||
return `ARTVERIS-${String(id).padStart(6, '0')}`;
|
||||
}
|
||||
|
||||
// 媒体目录层级:{模板ID}/{档案编号}/(追溯码出库图再下一级 {追溯码}/,见 outbound)
|
||||
private mediaDir(templateId: number, archiveNo: string): string {
|
||||
return `${templateId}/${archiveNo}`;
|
||||
}
|
||||
|
||||
// 把作品的既有媒体(主图/节点图/证书原件+水印件)归位到 {模板ID}/{档案编号}/,
|
||||
// 文件名人类可读:作品图_{时间戳}_{序号} / 节点-{标题}_... / 证书-{名称}_...;
|
||||
// 同步更新 DB 引用,返回 oldKey->newKey 映射(供调用方改写快照/DTO 中的旧引用)。
|
||||
private async relocateArtworkMedia(artworkId: number): Promise<Map<string, string>> {
|
||||
const keyMap = new Map<string, string>();
|
||||
const artwork = await this.prisma.artwork.findFirst({
|
||||
where: { id: artworkId, deletedAt: null },
|
||||
include: { traceNodes: true, certificates: true },
|
||||
});
|
||||
if (!artwork?.archiveNo) return keyMap;
|
||||
const dir = this.mediaDir(artwork.templateId, artwork.archiveNo);
|
||||
|
||||
const images = await this.storage.relocateMedia((artwork.images ?? []) as unknown as MediaItem[], dir, '作品图');
|
||||
images.keyMap.forEach((v, k) => keyMap.set(k, v));
|
||||
if (images.changed) {
|
||||
await this.prisma.artwork.update({
|
||||
where: { id: artworkId },
|
||||
data: { images: images.items as unknown as Prisma.InputJsonValue },
|
||||
});
|
||||
}
|
||||
|
||||
for (const node of artwork.traceNodes) {
|
||||
const media = await this.storage.relocateMedia(
|
||||
(node.mediaFiles ?? []) as unknown as MediaItem[],
|
||||
dir,
|
||||
`节点-${this.storage.sanitizeSegment(node.title)}`,
|
||||
);
|
||||
media.keyMap.forEach((v, k) => keyMap.set(k, v));
|
||||
if (media.changed) {
|
||||
await this.prisma.traceNode.update({
|
||||
where: { id: node.id },
|
||||
data: { mediaFiles: media.items as unknown as Prisma.InputJsonValue },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const cert of artwork.certificates) {
|
||||
const origin = await this.storage.relocateMedia(
|
||||
[{ key: cert.originFileUrl }],
|
||||
dir,
|
||||
`证书-${this.storage.sanitizeSegment(cert.name)}`,
|
||||
);
|
||||
const newOrigin = origin.items[0]?.key ?? cert.originFileUrl;
|
||||
let newWatermark = cert.watermarkFileUrl;
|
||||
if (origin.changed) {
|
||||
keyMap.set(cert.originFileUrl, newOrigin);
|
||||
// 水印件跟随原件命名(derivedKey 加 _wm 后缀、同目录)
|
||||
if (cert.watermarkFileUrl) {
|
||||
newWatermark = await this.storage.moveKey(cert.watermarkFileUrl, this.storage.derivedKey(newOrigin, 'wm'));
|
||||
if (newWatermark !== cert.watermarkFileUrl) keyMap.set(cert.watermarkFileUrl, newWatermark);
|
||||
}
|
||||
await this.prisma.certificate.update({
|
||||
where: { id: cert.id },
|
||||
data: { originFileUrl: newOrigin, watermarkFileUrl: newWatermark },
|
||||
});
|
||||
}
|
||||
}
|
||||
return keyMap;
|
||||
}
|
||||
|
||||
// 用 oldKey->newKey 映射改写媒体数组引用
|
||||
private remapMedia(items: MediaItem[] | undefined, keyMap: Map<string, string>): MediaItem[] | undefined {
|
||||
if (!items?.length || !keyMap.size) return items;
|
||||
return items.map((item) => {
|
||||
const next = item?.key ? keyMap.get(item.key) : undefined;
|
||||
return next ? { ...item, key: next, ...(item.url ? { url: this.storage.getUrl(next) } : {}) } : item;
|
||||
});
|
||||
}
|
||||
|
||||
async create(dto: CreateArtworkDto, userId: number) {
|
||||
const tpl = await this.prisma.template.findFirst({
|
||||
where: { id: dto.templateId, deletedAt: null },
|
||||
@@ -94,7 +169,7 @@ export class ArtworkService {
|
||||
dto.traceNodes === undefined ? DEFAULT_TRACE_NODES : this.normalizeTraceNodeDto(dto.traceNodes);
|
||||
const certificates = this.normalizeCertificateDto(dto.certificates ?? []);
|
||||
|
||||
const artwork = await this.prisma.$transaction(async (tx) => {
|
||||
const { created: artwork, revisionId } = await this.prisma.$transaction(async (tx) => {
|
||||
const created = await tx.artwork.create({
|
||||
data: {
|
||||
templateId: dto.templateId,
|
||||
@@ -136,7 +211,7 @@ export class ArtworkService {
|
||||
const archiveNo = this.buildArchiveNo(created.id);
|
||||
await tx.artwork.update({ where: { id: created.id }, data: { archiveNo } });
|
||||
const snapshot = this.snapshotFromRecord(created);
|
||||
await tx.artworkRevision.create({
|
||||
const revision = await tx.artworkRevision.create({
|
||||
data: {
|
||||
artworkId: created.id,
|
||||
title: status === ArtworkStatus.DRAFT ? '创建草稿' : '提交初次审核',
|
||||
@@ -147,9 +222,24 @@ export class ArtworkService {
|
||||
submittedBy: userId,
|
||||
},
|
||||
});
|
||||
return created;
|
||||
return { created, revisionId: revision.id };
|
||||
});
|
||||
|
||||
// 媒体归位到 {模板ID}/{档案编号}/(档案编号在事务内生成,只能事后归位),并同步初始快照引用
|
||||
const keyMap = await this.relocateArtworkMedia(artwork.id);
|
||||
if (keyMap.size) {
|
||||
const refreshed = await this.prisma.artwork.findFirst({
|
||||
where: { id: artwork.id },
|
||||
include: { traceNodes: true, certificates: true },
|
||||
});
|
||||
if (refreshed) {
|
||||
await this.prisma.artworkRevision.update({
|
||||
where: { id: revisionId },
|
||||
data: { afterSnapshot: this.snapshotFromRecord(refreshed) as unknown as Prisma.InputJsonObject },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await this.enqueuePendingCertificateWatermarks(artwork.id);
|
||||
return this.get(artwork.id);
|
||||
}
|
||||
@@ -269,6 +359,45 @@ export class ArtworkService {
|
||||
|
||||
async update(id: number, dto: UpdateArtworkDto, user: AuthUser) {
|
||||
const userId = user.id;
|
||||
const artwork = await this.ensureExists(id);
|
||||
// 先把既有媒体归位(含历史日期目录的旧文件),拿到 old->new 映射改写 DTO 引用;
|
||||
// 再把 DTO 里新上传的媒体(仍在通用上传目录)归位,保证快照/落库全部使用最终 key
|
||||
const keyMap = await this.relocateArtworkMedia(id);
|
||||
const dir = artwork.archiveNo ? this.mediaDir(artwork.templateId, artwork.archiveNo) : null;
|
||||
|
||||
let dtoImages = this.remapMedia(dto.images as MediaItem[] | undefined, keyMap);
|
||||
if (dtoImages && dir) {
|
||||
dtoImages = (await this.storage.relocateMedia(dtoImages, dir, '作品图')).items;
|
||||
}
|
||||
let dtoNodes = dto.traceNodes ? this.normalizeTraceNodeDto(dto.traceNodes) : undefined;
|
||||
if (dtoNodes && dir) {
|
||||
dtoNodes = await Promise.all(
|
||||
dtoNodes.map(async (node) => {
|
||||
const media = this.remapMedia(node.mediaFiles, keyMap) ?? [];
|
||||
const relocated = await this.storage.relocateMedia(media, dir, `节点-${this.storage.sanitizeSegment(node.title)}`);
|
||||
return { ...node, mediaFiles: relocated.items };
|
||||
}),
|
||||
);
|
||||
}
|
||||
let dtoCerts = dto.certificates ? this.normalizeCertificateDto(dto.certificates) : undefined;
|
||||
if (dtoCerts && dir) {
|
||||
dtoCerts = await Promise.all(
|
||||
dtoCerts.map(async (cert) => {
|
||||
let originKey = keyMap.get(cert.originKey) ?? cert.originKey;
|
||||
if (!originKey.startsWith(`${dir}/`)) {
|
||||
const relocated = await this.storage.relocateMedia(
|
||||
[{ key: originKey }],
|
||||
dir,
|
||||
`证书-${this.storage.sanitizeSegment(cert.name)}`,
|
||||
);
|
||||
originKey = relocated.items[0]?.key ?? originKey;
|
||||
}
|
||||
const watermarkKey = cert.watermarkKey ? keyMap.get(cert.watermarkKey) ?? cert.watermarkKey : cert.watermarkKey;
|
||||
return { ...cert, originKey, watermarkKey };
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const current = await this.getSnapshot(id);
|
||||
const nextStatus = dto.status === ArtworkStatus.DRAFT ? ArtworkStatus.ACTIVE : dto.status ?? current.artwork.status;
|
||||
const afterSnapshot: ArtworkSnapshot = {
|
||||
@@ -278,13 +407,11 @@ export class ArtworkService {
|
||||
artist: dto.artist ?? current.artwork.artist,
|
||||
status: nextStatus,
|
||||
dynamicFields: dto.dynamicFields ?? current.artwork.dynamicFields,
|
||||
images: (dto.images ?? current.artwork.images) as MediaItem[],
|
||||
images: (dtoImages ?? current.artwork.images) as MediaItem[],
|
||||
},
|
||||
traceNodes: dto.traceNodes ? this.normalizeTraceNodeDto(dto.traceNodes) : current.traceNodes,
|
||||
certificates: dto.certificates ? this.normalizeCertificateDto(dto.certificates) : current.certificates,
|
||||
traceNodes: dtoNodes ?? current.traceNodes,
|
||||
certificates: dtoCerts ?? current.certificates,
|
||||
};
|
||||
|
||||
const artwork = await this.ensureExists(id);
|
||||
const shouldApplyDirectly = artwork.status !== ArtworkStatus.ACTIVE || artwork.auditStatus !== AuditStatus.APPROVED;
|
||||
const shouldAutoApprove = user.role === RoleType.SUPER_ADMIN;
|
||||
if (!this.hasSnapshotChanged(current, afterSnapshot)) {
|
||||
|
||||
Reference in New Issue
Block a user