VcsFileReadFacade.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
In any derivative products, you must retain the information of
owner of the original code and provide clear attribution to the project
https://dochub.info
The use of this product or its derivatives for any purpose cannot be a secret.
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.
Maintainers:
Alexander Romashin <romashin.a.va@sberbank.ru>, Sber - 2025
Contributors:
Alexander Romashin <romashin.a.va@sberbank.ru>, Sber - 2025
Alexander Romashin <romashin.a.va@sberbank.ru>, Sber - 2026
Марат Ниязматов <mrusniyazmatov@sberbank.ru>, Sber - 2026
*/
package org.archtool.service
import org.apache.tika.Tika
import org.apache.tika.io.TikaInputStream
import org.apache.tika.metadata.Metadata
import org.apache.tika.metadata.TikaCoreProperties
import org.archtool.data.dto.BranchInfoDto
import org.archtool.data.dto.PageDto
import org.archtool.helper.SeafGitClient
import org.archtool.provider.FileRequest
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.stereotype.Service
import kotlin.io.path.Path
@Service
class VcsFileReadFacade(
private val seafGitClient: SeafGitClient,
) {
suspend fun readFile(fileRequest: FileRequest, path: String): ResponseEntity<ByteArray> {
val normalizedPath = path.trimStart('/')
log.debug(
"Запрошен файл {}:{}@{}",
fileRequest.repoRef.localPath,
fileRequest.branch,
normalizedPath
)
val content = seafGitClient.readFileFromBranch(
repoRef = fileRequest.repoRef,
branchOrTagName = fileRequest.branch,
filePath = normalizedPath
)
val mediaType = detectMimeType(content, normalizedPath)
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(mediaType))
.body(content)
}
suspend fun readCommitHash(fileRequest: FileRequest): ResponseEntity<PageDto<BranchInfoDto>> {
log.debug("Запрошен хеш коммита в {}:{}", fileRequest.repoRef.localPath, fileRequest.branch)
val commitHash = seafGitClient.getLastCommitHash(fileRequest.repoRef, fileRequest.branch)
return ResponseEntity.ok()
.body(
PageDto(
size = 1,
start = 0,
limit = 1,
isLastPage = true,
nextPageStart = null,
values = listOf(
BranchInfoDto(
displayId = fileRequest.branch,
latestCommit = commitHash
)
)
)
)
}
private fun detectMimeType(content: ByteArray, path: String): String {
val tika = Tika()
val metadata = Metadata().apply {
val fileName = Path(path).fileName.toString()
if (fileName.isNotEmpty()) {
add(TikaCoreProperties.RESOURCE_NAME_KEY, fileName)
}
}
val stream = TikaInputStream.get(content)
return tika.detect(stream, metadata)
}
companion object {
private val log: Logger = LoggerFactory.getLogger(VcsFileReadFacade::class.java)
}
}