File size: 12,091 Bytes
28316e4
9e212e3
 
28316e4
9e212e3
 
 
 
 
 
 
28316e4
 
9e212e3
 
 
28316e4
 
 
 
9e212e3
28316e4
 
 
 
 
 
 
9e212e3
 
28316e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e212e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28316e4
 
 
 
 
 
 
 
9e212e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28316e4
9e212e3
 
28316e4
9e212e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28316e4
9e212e3
 
28316e4
9e212e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28316e4
9e212e3
 
 
 
 
 
 
 
 
 
28316e4
 
9e212e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28316e4
9e212e3
 
28316e4
 
9e212e3
 
 
 
 
28316e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9e212e3
 
 
 
 
 
 
 
28316e4
9e212e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28316e4
 
 
 
 
 
 
 
 
9e212e3
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import { NextFunction, Request, Router, Response } from 'express';
import multer from 'multer';
import { authenticateUser, AuthenticatedRequest } from '../middleware/auth';
import { deleteInputFile, uploadInputFileFromPath } from '../../db/storage';
import {
  createJobWithTicket,
  getJobBySubmissionRequestId,
  getUserProfile,
} from '../../db/tickets';
import { logger } from '../../utils/logger';
import { createHash, randomUUID } from 'crypto';
import { createReadStream, promises as fsPromises } from 'fs';
import * as os from 'os';

const router = Router();

/**
 * Keep large uploads out of the worker heap. The route removes every temporary
 * file after validation/storage, including early returns and failed requests.
 */
const upload = multer({
  storage: multer.diskStorage({
    destination: os.tmpdir(),
    filename: (_req, file, callback) => {
      callback(null, `relv-upload-${randomUUID()}${getFileExtension(file.originalname)}`);
    },
  }),
  limits: { fileSize: 100 * 1024 * 1024 }, // User uploads are capped at 100 MB
});

function receiveUpload(
  req: Request,
  res: Response,
  next: NextFunction,
): void {
  upload.single('file')(req, res, (error: unknown) => {
    if (error instanceof multer.MulterError && error.code === 'LIMIT_FILE_SIZE') {
      res.status(413).json({ error: 'File exceeds the 100MB upload limit' });
      return;
    }
    if (error) {
      next(error);
      return;
    }
    next();
  });
}

/** Allowed file extensions for Turnitin submissions */
const ALLOWED_EXTENSIONS = new Set([
  '.docx',
  '.xlsx',
  '.pptx',
  '.ps',
  '.pdf',
  '.html',
  '.rtf',
  '.odt',
  '.hwp',
  '.txt',
]);
const ALLOWED_MODES = new Set(['upload', 'resubmit']);
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;

const DEFAULT_FILTERS: Record<string, unknown> = {
  excludeBibliography: false,
  excludeQuotes: false,
  excludeCitations: false,
  excludeSmallMatches: true,
  smallMatchMode: 'words',
  smallMatchThreshold: 8,
};

function getFileExtension(filename: string): string {
  const lastDot = filename.lastIndexOf('.');
  return lastDot >= 0 ? filename.slice(lastDot).toLowerCase() : '';
}

async function sha256File(filePath: string): Promise<string> {
  const hash = createHash('sha256');
  for await (const chunk of createReadStream(filePath)) {
    hash.update(chunk as Buffer);
  }
  return hash.digest('hex');
}

function existingJobMatchesRequest(
  existingJob: {
    assignment_target_id: string;
    mode: string;
    input_file_name: string;
    input_file_size: number | null;
    input_file_sha256: string | null;
  },
  request: {
    assignmentTargetId: string;
    mode: string;
    inputFileName: string;
    inputFileSize: number;
    inputFileSha256: string;
  },
): boolean {
  return (
    existingJob.assignment_target_id === request.assignmentTargetId &&
    existingJob.mode === request.mode &&
    existingJob.input_file_name === request.inputFileName &&
    existingJob.input_file_size === request.inputFileSize &&
    existingJob.input_file_sha256 === request.inputFileSha256
  );
}

/**
 * POST /api/submit
 * Accepts a file upload + metadata, validates, stores the file,
 * creates a job (deducting a ticket), and returns the job ID.
 */
router.post(
  '/api/submit',
  authenticateUser,
  receiveUpload,
  async (req, res: Response): Promise<void> => {
    const authReq = req as AuthenticatedRequest;
    const temporaryUploadPath = authReq.file?.path;

    try {
      // 1. Validate required fields
      const {
        assignment_target_id,
        mode,
        filters: filtersRaw,
        submission_request_id: bodySubmissionRequestId,
      } = authReq.body;

      if (!assignment_target_id || !mode) {
        res.status(400).json({
          error: 'Missing required fields: assignment_target_id, mode',
        });
        return;
      }

      if (!ALLOWED_MODES.has(String(mode))) {
        res.status(400).json({
          error: 'Invalid mode. Allowed: upload, resubmit',
        });
        return;
      }

      const suppliedRequestId = String(
        bodySubmissionRequestId || authReq.get('Idempotency-Key') || '',
      ).trim();
      const submissionRequestId = suppliedRequestId || randomUUID();

      if (!UUID_PATTERN.test(submissionRequestId)) {
        res.status(400).json({ error: 'Invalid submission request ID' });
        return;
      }

      // Parse filters (may come as stringified JSON)
      let filters: Record<string, unknown>;
      try {
        const parsedFilters =
          typeof filtersRaw === 'string'
            ? JSON.parse(filtersRaw)
            : filtersRaw && typeof filtersRaw === 'object'
              ? filtersRaw
              : {};
        filters = { ...DEFAULT_FILTERS, ...parsedFilters };
      } catch {
        res.status(400).json({ error: 'Invalid filters JSON' });
        return;
      }

      // 2. Validate file exists
      if (!authReq.file) {
        res.status(400).json({ error: 'File is required' });
        return;
      }
      const uploadedFile = authReq.file;

      // 3. Validate file type
      const ext = getFileExtension(uploadedFile.originalname);
      if (!ALLOWED_EXTENSIONS.has(ext)) {
        res.status(400).json({
          error: `Unsupported file type: ${ext}. Allowed: ${[...ALLOWED_EXTENSIONS].join(', ')}`,
        });
        return;
      }

      // 4. Validate filters. Legacy Turnitin supports small-match words,
      // percent, or off; modern Turnitin uses word threshold only.
      const smallMatchMode =
        filters.smallMatchMode === 'percent' ||
        filters.smallMatchMode === 'off' ||
        filters.smallMatchMode === 'words'
          ? filters.smallMatchMode
          : 'words';
      filters.smallMatchMode = smallMatchMode;

      if (filters.excludeSmallMatches === true && smallMatchMode !== 'off') {
        let threshold = Number(filters.smallMatchThreshold) || 8;
        const maxThreshold = smallMatchMode === 'percent' ? 100 : 40;
        threshold = Math.max(1, Math.min(maxThreshold, threshold));
        filters.smallMatchThreshold = threshold;
      } else {
        filters.excludeSmallMatches = false;
        filters.smallMatchMode = 'off';
        filters.smallMatchThreshold = null;
      }

      // 5. Fingerprint the payload before any state-changing operation.
      const inputFileSha256 = await sha256File(uploadedFile.path);

      const existingJob = await getJobBySubmissionRequestId(
        authReq.userId,
        submissionRequestId,
      );

      if (existingJob) {
        if (!existingJobMatchesRequest(existingJob, {
          assignmentTargetId: String(assignment_target_id),
          mode: String(mode),
          inputFileName: uploadedFile.originalname,
          inputFileSize: uploadedFile.size,
          inputFileSha256,
        })) {
          res.status(409).json({
            error: 'Submission request ID was already used for a different file or configuration',
          });
          return;
        }

        const profile = await getUserProfile(authReq.userId);
        logger.info('Idempotent submit replay returned existing job', {
          jobId: existingJob.id,
          userId: authReq.userId,
          submissionRequestId,
        });
        res.status(200).json({
          jobId: existingJob.id,
          ticketBalance: profile?.ticket_balance ?? 0,
          idempotentReplay: true,
        });
        return;
      }

      // A deterministic request/hash path makes concurrent retries upload the
      // same bytes to the same object before the database invariant resolves them.
      const storagePath = await uploadInputFileFromPath(
        authReq.userId,
        `${submissionRequestId}/${inputFileSha256}`,
        uploadedFile.originalname,
        uploadedFile.path,
        true,
      );

      // 6. Atomically create one job/ticket ledger entry, or return the job
      // already created by another Space handling this exact request.
      const creation = await (async () => {
        try {
          return await createJobWithTicket({
            userId: authReq.userId,
            assignmentTargetId: assignment_target_id,
            mode,
            filters,
            inputFileName: uploadedFile.originalname,
            inputStoragePath: storagePath,
            inputFileSize: uploadedFile.size,
            inputFileSha256,
            submissionRequestId,
          });
        } catch (creationError) {
          // The RPC response may fail after the transaction commits. Confirm
          // database state before deleting the staged object.
          let recoveredJob;
          try {
            recoveredJob = await getJobBySubmissionRequestId(
              authReq.userId,
              submissionRequestId,
            );
          } catch (recoveryError) {
            logger.warn('Could not verify job creation after RPC failure; preserving input object', {
              userId: authReq.userId,
              submissionRequestId,
              error:
                recoveryError instanceof Error
                  ? recoveryError.message
                  : String(recoveryError),
            });
            throw creationError;
          }

          if (recoveredJob) {
            if (!existingJobMatchesRequest(recoveredJob, {
              assignmentTargetId: String(assignment_target_id),
              mode: String(mode),
              inputFileName: uploadedFile.originalname,
              inputFileSize: uploadedFile.size,
              inputFileSha256,
            })) {
              await deleteInputFile(storagePath).catch(() => {});
              throw new Error('Idempotency key recovered a different job payload');
            }
            logger.warn('Recovered committed job after job-creation response failure', {
              jobId: recoveredJob.id,
              userId: authReq.userId,
              submissionRequestId,
            });
            return { jobId: recoveredJob.id, created: false };
          }

          await deleteInputFile(storagePath).catch((cleanupError: unknown) => {
            logger.warn('Failed to roll back staged input after job creation failure', {
              storagePath,
              error:
                cleanupError instanceof Error
                  ? cleanupError.message
                  : String(cleanupError),
            });
          });
          throw creationError;
        }
      })();

      // Fetch updated ticket balance
      const profile = await getUserProfile(authReq.userId);
      const ticketBalance = profile?.ticket_balance ?? 0;

      logger.info(creation.created ? 'Job submitted successfully' : 'Idempotent submit race resolved', {
        jobId: creation.jobId,
        userId: authReq.userId,
        fileName: uploadedFile.originalname,
        submissionRequestId,
      });

      // 7. Return success
      res.status(creation.created ? 201 : 200).json({
        jobId: creation.jobId,
        ticketBalance,
        idempotentReplay: !creation.created,
      });
    } catch (err: unknown) {
      const message = err instanceof Error ? err.message : String(err);

      // Handle specific error cases from the RPC
      if (message.includes('insufficient') || message.includes('ticket')) {
        res.status(402).json({ error: 'Insufficient ticket balance' });
        return;
      }

      if (message.includes('Idempotency key')) {
        res.status(409).json({ error: 'Submission request ID conflict' });
        return;
      }

      logger.error('Submit endpoint error', {
        userId: authReq.userId,
        error: message,
      });

      res.status(500).json({ error: 'Internal server error' });
    } finally {
      if (temporaryUploadPath) {
        await fsPromises.unlink(temporaryUploadPath).catch((error: unknown) => {
          logger.warn('Failed to remove temporary upload file', {
            temporaryUploadPath,
            error: error instanceof Error ? error.message : String(error),
          });
        });
      }
    }
  },
);

export default router;