FileUploadControllerImpl.kt
/*
Copyright (C) 2026 Sber
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.archtool.archloader.controller
import org.springframework.security.oauth2.jwt.Jwt
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import org.archtool.archloader.controller.api.FileUploadController
import org.archtool.archloader.controller.api.dto.CheckAuthResponse
import org.archtool.archloader.controller.api.dto.StatusResponse
import org.archtool.archloader.controller.api.dto.UploadResponse
import org.archtool.archloader.exception.UserDataException
import org.archtool.archloader.service.TaskStoreService
import org.archtool.archloader.service.zip.income.TaskCreationService
import org.archtool.archloader.utils.ChecksumUtils
import org.archtool.archloader.utils.SeafTokenParser
import org.archtool.archloader.utils.UuidUtils
import org.springframework.http.ResponseEntity
/**
* Контроллер для загрузки архитектурных артефактов в виде ZIP-архивов.
*/
@RestController
class FileUploadControllerImpl(
private val taskCreationService: TaskCreationService,
private val taskStoreService: TaskStoreService,
private val checksumUtils: ChecksumUtils,
private val seafTokenParser: SeafTokenParser,
) : FileUploadController {
override fun uploadZipFile(
jwt: Jwt,
file: MultipartFile,
checksum: String,
): UploadResponse {
// Проверка расширения
val fileName = file.originalFilename
if (fileName == null || !fileName.lowercase().endsWith(".zip")) {
throw UserDataException("Только ZIP-файлы разрешены. Получено: ${fileName ?: "без имени"}")
}
file.inputStream.use { fileInputStream ->
checksumUtils.validateChecksum(
inputStream = fileInputStream,
expectedChecksum = checksum,
context = "Проверка checksum файла клиента при получении запроса"
)
}
val fromJwt = seafTokenParser.fromJwt(jwt)
val newTask = taskCreationService.createNewTask(file = file, seafJwt = fromJwt, checksum = checksum)
return UploadResponse(
taskId = newTask.id,
message = "ZIP-файл '${file.originalFilename}' успешно принят на обработку",
success = true
)
}
override fun getStatus(
taskIdRaw: String,
): StatusResponse {
val taskId = UuidUtils.tryParseUuid(taskIdRaw)
?: throw UserDataException("Номер задачи в запросе должен быть валидный uuid")
val taskStatus = taskStoreService.getTaskStatus(taskId)
if (taskStatus == null) {
throw UserDataException("Неизвестный номер задачи, либо старая (мы уже удалили её)")
}
return taskStatus
}
override fun checkAuth(): ResponseEntity<CheckAuthResponse> {
return ResponseEntity.ok().body(CheckAuthResponse.SUCCESS)
}
}