|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: syuilo and misskey-project |
| 3 | + * SPDX-License-Identifier: AGPL-3.0-only |
| 4 | + */ |
| 5 | + |
| 6 | +import * as fs from 'node:fs/promises'; |
| 7 | +import { Inject, Injectable } from '@nestjs/common'; |
| 8 | +import * as Redis from 'ioredis'; |
| 9 | +import { IdentifiableError } from '@/misc/identifiable-error.js'; |
| 10 | +import { Endpoint } from '@/server/api/endpoint-base.js'; |
| 11 | +import { DriveFileEntityService } from '@/core/entities/DriveFileEntityService.js'; |
| 12 | +import { DriveService } from '@/core/DriveService.js'; |
| 13 | +import { MiMeta } from '@/models/_.js'; |
| 14 | +import { DI } from '@/di-symbols.js'; |
| 15 | +import { ApiError } from '../../../error.js'; |
| 16 | + |
| 17 | +type UploadSession = { |
| 18 | + sessionId: string; |
| 19 | + userId: string; |
| 20 | + name: string; |
| 21 | + size: number; |
| 22 | + folderId: string | null; |
| 23 | + isSensitive: boolean; |
| 24 | + comment: string | null; |
| 25 | + force: boolean; |
| 26 | + totalChunks: number; |
| 27 | + tempDir: string; |
| 28 | + tempFilePath: string; |
| 29 | +}; |
| 30 | + |
| 31 | +function getSessionKey(sessionId: string): string { |
| 32 | + return `drive:chunk-upload:session:${sessionId}`; |
| 33 | +} |
| 34 | + |
| 35 | +function getChunksKey(sessionId: string): string { |
| 36 | + return `drive:chunk-upload:session:${sessionId}:chunks`; |
| 37 | +} |
| 38 | + |
| 39 | +async function cleanupUpload(redisClient: Redis.Redis, session: UploadSession): Promise<void> { |
| 40 | + await redisClient.del(getSessionKey(session.sessionId), getChunksKey(session.sessionId)); |
| 41 | + await fs.rm(session.tempDir, { recursive: true, force: true }).catch(() => {}); |
| 42 | +} |
| 43 | + |
| 44 | +export const meta = { |
| 45 | + tags: ['drive'], |
| 46 | + |
| 47 | + requireCredential: true, |
| 48 | + |
| 49 | + prohibitMoved: true, |
| 50 | + |
| 51 | + kind: 'write:drive', |
| 52 | + |
| 53 | + description: 'Commit a chunked drive file upload session.', |
| 54 | + |
| 55 | + res: { |
| 56 | + type: 'object', |
| 57 | + optional: false, nullable: false, |
| 58 | + ref: 'DriveFile', |
| 59 | + }, |
| 60 | + |
| 61 | + errors: { |
| 62 | + sessionNotFound: { |
| 63 | + message: 'Upload session not found.', |
| 64 | + code: 'SESSION_NOT_FOUND', |
| 65 | + id: '4df76536-4f72-4c80-a4f8-4be45d09d5f7', |
| 66 | + }, |
| 67 | + |
| 68 | + missingChunks: { |
| 69 | + message: 'Some chunks are missing.', |
| 70 | + code: 'MISSING_CHUNKS', |
| 71 | + id: '15c5a42d-b790-4e7c-b4b6-6a0bf8713910', |
| 72 | + }, |
| 73 | + |
| 74 | + inappropriate: { |
| 75 | + message: 'Cannot upload the file because it has been determined that it possibly contains inappropriate content.', |
| 76 | + code: 'INAPPROPRIATE', |
| 77 | + id: 'bec5bd69-fba3-43c9-b4fb-2894b66ad5d2', |
| 78 | + }, |
| 79 | + |
| 80 | + noFreeSpace: { |
| 81 | + message: 'Cannot upload the file because you have no free space of drive.', |
| 82 | + code: 'NO_FREE_SPACE', |
| 83 | + id: 'd08dbc37-a6a9-463a-8c47-96c32ab5f064', |
| 84 | + }, |
| 85 | + |
| 86 | + maxFileSizeExceeded: { |
| 87 | + message: 'Cannot upload the file because it exceeds the maximum file size.', |
| 88 | + code: 'MAX_FILE_SIZE_EXCEEDED', |
| 89 | + id: 'b9d8c348-33f0-4673-b9a9-5d4da058977a', |
| 90 | + httpStatusCode: 413, |
| 91 | + }, |
| 92 | + |
| 93 | + unallowedFileType: { |
| 94 | + message: 'Cannot upload the file because it is an unallowed file type.', |
| 95 | + code: 'UNALLOWED_FILE_TYPE', |
| 96 | + id: '4becd248-7f2c-48c4-a9f0-75edc4f9a1ea', |
| 97 | + }, |
| 98 | + }, |
| 99 | +} as const; |
| 100 | + |
| 101 | +export const paramDef = { |
| 102 | + type: 'object', |
| 103 | + properties: { |
| 104 | + sessionId: { type: 'string' }, |
| 105 | + }, |
| 106 | + required: ['sessionId'], |
| 107 | +} as const; |
| 108 | + |
| 109 | +@Injectable() |
| 110 | +export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export |
| 111 | + constructor( |
| 112 | + @Inject(DI.meta) |
| 113 | + private serverSettings: MiMeta, |
| 114 | + |
| 115 | + @Inject(DI.redis) |
| 116 | + private redisClient: Redis.Redis, |
| 117 | + |
| 118 | + private driveFileEntityService: DriveFileEntityService, |
| 119 | + private driveService: DriveService, |
| 120 | + ) { |
| 121 | + super(meta, paramDef, async (ps, me, _, _1, _2, ip, headers) => { |
| 122 | + const sessionJson = await this.redisClient.get(getSessionKey(ps.sessionId)); |
| 123 | + if (sessionJson == null) { |
| 124 | + throw new ApiError(meta.errors.sessionNotFound); |
| 125 | + } |
| 126 | + |
| 127 | + const session = JSON.parse(sessionJson) as UploadSession; |
| 128 | + if (session.userId !== me.id) { |
| 129 | + throw new ApiError(meta.errors.sessionNotFound); |
| 130 | + } |
| 131 | + |
| 132 | + const uploadedChunks = new Set(await this.redisClient.smembers(getChunksKey(ps.sessionId))); |
| 133 | + for (let index = 0; index < session.totalChunks; index++) { |
| 134 | + if (!uploadedChunks.has(String(index))) { |
| 135 | + throw new ApiError(meta.errors.missingChunks); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + const stats = await fs.stat(session.tempFilePath); |
| 140 | + if (stats.size !== session.size) { |
| 141 | + throw new ApiError(meta.errors.missingChunks); |
| 142 | + } |
| 143 | + |
| 144 | + try { |
| 145 | + const driveFile = await this.driveService.addFile({ |
| 146 | + user: me, |
| 147 | + path: session.tempFilePath, |
| 148 | + name: session.name, |
| 149 | + comment: session.comment, |
| 150 | + folderId: session.folderId, |
| 151 | + force: session.force, |
| 152 | + sensitive: session.isSensitive, |
| 153 | + requestIp: this.serverSettings.enableIpLogging ? ip : null, |
| 154 | + requestHeaders: this.serverSettings.enableIpLogging ? headers : null, |
| 155 | + }); |
| 156 | + |
| 157 | + await cleanupUpload(this.redisClient, session); |
| 158 | + |
| 159 | + return await this.driveFileEntityService.pack(driveFile, { self: true }); |
| 160 | + } catch (err) { |
| 161 | + await cleanupUpload(this.redisClient, session); |
| 162 | + if (err instanceof Error || typeof err === 'string') { |
| 163 | + console.error(err); |
| 164 | + } |
| 165 | + if (err instanceof IdentifiableError) { |
| 166 | + if (err.id === '282f77bf-5816-4f72-9264-aa14d8261a21') throw new ApiError(meta.errors.inappropriate); |
| 167 | + if (err.id === 'c6244ed2-a39a-4e1c-bf93-f0fbd7764fa6') throw new ApiError(meta.errors.noFreeSpace); |
| 168 | + if (err.id === 'f9e4e5f3-4df4-40b5-b400-f236945f7073') throw new ApiError(meta.errors.maxFileSizeExceeded); |
| 169 | + if (err.id === 'bd71c601-f9b0-4808-9137-a330647ced9b') throw new ApiError(meta.errors.unallowedFileType); |
| 170 | + } |
| 171 | + throw new ApiError(); |
| 172 | + } |
| 173 | + }); |
| 174 | + } |
| 175 | +} |
0 commit comments