|
| 1 | +import { Readable } from 'node:stream' |
| 2 | +import { URL } from 'node:url' |
| 3 | +import { nanoid } from '@mx-space/compiled' |
| 4 | +import { |
| 5 | + BadRequestException, |
| 6 | + Injectable, |
| 7 | + Logger, |
| 8 | + NotFoundException, |
| 9 | +} from '@nestjs/common' |
| 10 | +import type { DocumentType } from '@typegoose/typegoose' |
| 11 | +import { alphabet } from '~/constants/other.constant' |
| 12 | +import { HttpService } from '~/processors/helper/helper.http.service' |
| 13 | +import { InjectModel } from '~/transformers/model.transformer' |
| 14 | +import { validateImageBuffer } from '~/utils/image.util' |
| 15 | +import { ConfigsService } from '../configs/configs.service' |
| 16 | +import { FileService } from '../file/file.service' |
| 17 | +import type { FileType } from '../file/file.type' |
| 18 | +import { LinkModel, LinkState } from './link.model' |
| 19 | + |
| 20 | +const { customAlphabet } = nanoid |
| 21 | +const AVATAR_TYPE: FileType = 'avatar' |
| 22 | + |
| 23 | +const ALLOWED_IMAGE_MIME_TYPES = new Set([ |
| 24 | + 'image/jpeg', |
| 25 | + 'image/jpg', |
| 26 | + 'image/png', |
| 27 | + 'image/webp', |
| 28 | + 'image/gif', |
| 29 | + 'image/x-icon', |
| 30 | +]) |
| 31 | + |
| 32 | +const ALLOWED_IMAGE_EXTENSIONS = new Set([ |
| 33 | + '.jpg', |
| 34 | + '.jpeg', |
| 35 | + '.png', |
| 36 | + '.webp', |
| 37 | + '.gif', |
| 38 | + '.ico', |
| 39 | +]) |
| 40 | + |
| 41 | +@Injectable() |
| 42 | +export class LinkAvatarService { |
| 43 | + private readonly logger: Logger |
| 44 | + |
| 45 | + constructor( |
| 46 | + @InjectModel(LinkModel) |
| 47 | + private readonly linkModel: MongooseModel<LinkModel>, |
| 48 | + private readonly configsService: ConfigsService, |
| 49 | + private readonly fileService: FileService, |
| 50 | + private readonly http: HttpService, |
| 51 | + ) { |
| 52 | + this.logger = new Logger(LinkAvatarService.name) |
| 53 | + } |
| 54 | + |
| 55 | + async convertToInternal( |
| 56 | + link: string | DocumentType<LinkModel>, |
| 57 | + ): Promise<boolean> { |
| 58 | + const doc = |
| 59 | + typeof link === 'string' ? await this.linkModel.findById(link) : link |
| 60 | + if (!doc) { |
| 61 | + if (typeof link === 'string') { |
| 62 | + throw new NotFoundException() |
| 63 | + } |
| 64 | + return false |
| 65 | + } |
| 66 | + |
| 67 | + const avatar = doc.avatar |
| 68 | + if (!avatar || !this.isExternalAvatar(avatar)) { |
| 69 | + return false |
| 70 | + } |
| 71 | + |
| 72 | + const { url: configUrl, friendLinkOptions } = |
| 73 | + await this.configsService.waitForConfigReady() |
| 74 | + |
| 75 | + if (!friendLinkOptions.enableAvatarInternalization) { |
| 76 | + return false |
| 77 | + } |
| 78 | + |
| 79 | + const { webUrl } = configUrl |
| 80 | + |
| 81 | + const refererHeader = (() => { |
| 82 | + try { |
| 83 | + if (doc.url) { |
| 84 | + return new URL(doc.url).origin |
| 85 | + } |
| 86 | + } catch (error: any) { |
| 87 | + this.logger.warn( |
| 88 | + `解析友链 ${doc._id} 的站点地址失败: ${error?.message || String(error)}`, |
| 89 | + ) |
| 90 | + } |
| 91 | + return webUrl |
| 92 | + })() |
| 93 | + |
| 94 | + const response = await this.http.axiosRef.get<ArrayBuffer>(avatar, { |
| 95 | + responseType: 'arraybuffer', |
| 96 | + timeout: 10_000, |
| 97 | + headers: { |
| 98 | + 'user-agent': |
| 99 | + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36', |
| 100 | + referer: refererHeader, |
| 101 | + }, |
| 102 | + }) |
| 103 | + |
| 104 | + const buffer = Buffer.from(response.data as any) |
| 105 | + const contentType = (response.headers['content-type'] || |
| 106 | + response.headers['Content-Type']) as string | undefined |
| 107 | + const normalizedContentType = this.normalizeMimeType(contentType) |
| 108 | + |
| 109 | + if ( |
| 110 | + !normalizedContentType || |
| 111 | + !this.isAllowedMimeType(normalizedContentType) |
| 112 | + ) { |
| 113 | + this.logger.warn( |
| 114 | + `友链 ${doc._id} 头像响应类型 ${contentType || 'unknown'} 不在受支持图片范围,跳过内链转换`, |
| 115 | + ) |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + const validation = validateImageBuffer({ |
| 120 | + originUrl: avatar, |
| 121 | + buffer, |
| 122 | + allowedExtensions: ALLOWED_IMAGE_EXTENSIONS, |
| 123 | + allowedMimeTypes: ALLOWED_IMAGE_MIME_TYPES, |
| 124 | + }) |
| 125 | + |
| 126 | + if (!validation.ok) { |
| 127 | + throw new BadRequestException(validation.reason) |
| 128 | + } |
| 129 | + |
| 130 | + const { ext } = validation |
| 131 | + |
| 132 | + const filename = customAlphabet(alphabet)(18) + ext.toLowerCase() |
| 133 | + |
| 134 | + await this.fileService.writeFile( |
| 135 | + AVATAR_TYPE, |
| 136 | + filename, |
| 137 | + Readable.from(buffer), |
| 138 | + ) |
| 139 | + |
| 140 | + const internalUrl = await this.fileService.resolveFileUrl( |
| 141 | + AVATAR_TYPE, |
| 142 | + filename, |
| 143 | + ) |
| 144 | + |
| 145 | + doc.avatar = internalUrl |
| 146 | + await doc.save() |
| 147 | + |
| 148 | + this.logger.log(`友链 ${doc._id} 头像已转换为内部链接`) |
| 149 | + |
| 150 | + return true |
| 151 | + } |
| 152 | + |
| 153 | + async migratePassedLinks(): Promise<{ |
| 154 | + updatedCount: number |
| 155 | + updatedIds: string[] |
| 156 | + }> { |
| 157 | + const { friendLinkOptions } = await this.configsService.waitForConfigReady() |
| 158 | + if (!friendLinkOptions.enableAvatarInternalization) { |
| 159 | + return { |
| 160 | + updatedCount: 0, |
| 161 | + updatedIds: [], |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + const links = await this.linkModel |
| 166 | + .find({ |
| 167 | + state: LinkState.Pass, |
| 168 | + avatar: { $exists: true, $ne: null }, |
| 169 | + }) |
| 170 | + .lean() |
| 171 | + |
| 172 | + const updatedIds: string[] = [] |
| 173 | + |
| 174 | + for (const link of links) { |
| 175 | + try { |
| 176 | + if (this.isExternalAvatar(link.avatar as string)) { |
| 177 | + const converted = await this.convertToInternal(String(link._id)) |
| 178 | + if (converted) { |
| 179 | + updatedIds.push(String(link._id)) |
| 180 | + } |
| 181 | + } |
| 182 | + } catch (error: any) { |
| 183 | + this.logger.error( |
| 184 | + `迁移友链头像失败: ${link._id} - ${error?.message || String(error)}`, |
| 185 | + ) |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + return { |
| 190 | + updatedCount: updatedIds.length, |
| 191 | + updatedIds, |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + private isExternalAvatar(avatar: string | undefined | null): boolean { |
| 196 | + if (!avatar) return false |
| 197 | + try { |
| 198 | + new URL(avatar) |
| 199 | + } catch { |
| 200 | + return false |
| 201 | + } |
| 202 | + if (avatar.includes('/objects/avatar/')) { |
| 203 | + return false |
| 204 | + } |
| 205 | + |
| 206 | + return true |
| 207 | + } |
| 208 | + |
| 209 | + private normalizeMimeType(mime?: string): string | undefined { |
| 210 | + if (!mime) return undefined |
| 211 | + return mime.split(';')[0].trim().toLowerCase() |
| 212 | + } |
| 213 | + |
| 214 | + private isAllowedMimeType(mime: string): boolean { |
| 215 | + return ALLOWED_IMAGE_MIME_TYPES.has(mime) |
| 216 | + } |
| 217 | +} |
0 commit comments