2019-05-29 05:21:06 -04:00
|
|
|
const AuthorizationManager = require('../Authorization/AuthorizationManager')
|
|
|
|
const CompileManager = require('../Compile/CompileManager')
|
|
|
|
const ClsiManager = require('../Compile/ClsiManager')
|
|
|
|
const ProjectFileAgent = require('./ProjectFileAgent')
|
2024-01-08 08:55:30 -05:00
|
|
|
const _ = require('lodash')
|
2019-05-29 05:21:06 -04:00
|
|
|
const {
|
2021-07-30 04:57:48 -04:00
|
|
|
CompileFailedError,
|
2019-05-29 05:21:06 -04:00
|
|
|
BadDataError,
|
|
|
|
AccessDeniedError,
|
|
|
|
} = require('./LinkedFilesErrors')
|
2023-07-06 07:17:03 -04:00
|
|
|
const { OutputFileFetchFailedError } = require('../Errors/Errors')
|
2019-05-29 05:21:06 -04:00
|
|
|
const LinkedFilesHandler = require('./LinkedFilesHandler')
|
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function _prepare(projectId, linkedFileData, userId, callback) {
|
|
|
|
_checkAuth(projectId, linkedFileData, userId, (err, allowed) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
if (!allowed) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(new AccessDeniedError())
|
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
if (!_validate(linkedFileData)) {
|
|
|
|
return callback(new BadDataError())
|
|
|
|
}
|
|
|
|
callback(null, linkedFileData)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createLinkedFile(
|
|
|
|
projectId,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parentFolderId,
|
|
|
|
userId,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
if (!ProjectFileAgent._canCreate(linkedFileData)) {
|
|
|
|
return callback(new AccessDeniedError())
|
|
|
|
}
|
|
|
|
linkedFileData = _sanitizeData(linkedFileData)
|
|
|
|
_prepare(projectId, linkedFileData, userId, (err, linkedFileData) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
_getFileStream(linkedFileData, userId, (err, readStream) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
2023-07-06 07:17:03 -04:00
|
|
|
LinkedFilesHandler.importFromStream(
|
|
|
|
projectId,
|
|
|
|
readStream,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parentFolderId,
|
|
|
|
userId,
|
|
|
|
(err, file) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, file._id)
|
2021-09-22 08:32:16 -04:00
|
|
|
}
|
2023-07-06 07:17:03 -04:00
|
|
|
)
|
2021-09-22 08:32:16 -04:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function refreshLinkedFile(
|
|
|
|
projectId,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parentFolderId,
|
|
|
|
userId,
|
|
|
|
callback
|
|
|
|
) {
|
|
|
|
_prepare(projectId, linkedFileData, userId, (err, linkedFileData) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
_compileAndGetFileStream(
|
2019-05-29 05:21:06 -04:00
|
|
|
linkedFileData,
|
2021-09-22 08:32:16 -04:00
|
|
|
userId,
|
|
|
|
(err, readStream, newBuildId) => {
|
|
|
|
if (err) {
|
2019-05-29 05:21:06 -04:00
|
|
|
return callback(err)
|
|
|
|
}
|
2023-07-06 07:17:03 -04:00
|
|
|
linkedFileData.build_id = newBuildId
|
|
|
|
LinkedFilesHandler.importFromStream(
|
|
|
|
projectId,
|
|
|
|
readStream,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parentFolderId,
|
|
|
|
userId,
|
|
|
|
(err, file) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, file._id)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2023-07-06 07:17:03 -04:00
|
|
|
)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
|
|
|
)
|
2021-09-22 08:32:16 -04:00
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function _sanitizeData(data) {
|
|
|
|
return {
|
|
|
|
provider: data.provider,
|
|
|
|
source_project_id: data.source_project_id,
|
|
|
|
source_output_file_path: data.source_output_file_path,
|
|
|
|
build_id: data.build_id,
|
2022-05-13 05:35:31 -04:00
|
|
|
clsiServerId: data.clsiServerId,
|
2021-09-22 08:32:16 -04:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function _validate(data) {
|
|
|
|
return (
|
|
|
|
(data.v1_source_doc_id != null && data.source_output_file_path != null) ||
|
|
|
|
(data.source_project_id != null &&
|
|
|
|
data.source_output_file_path != null &&
|
|
|
|
data.build_id != null)
|
|
|
|
)
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function _checkAuth(projectId, data, currentUserId, callback) {
|
|
|
|
callback = _.once(callback)
|
|
|
|
if (!_validate(data)) {
|
|
|
|
return callback(new BadDataError())
|
|
|
|
}
|
|
|
|
LinkedFilesHandler.getSourceProject(data, (err, project) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
AuthorizationManager.canUserReadProject(
|
|
|
|
currentUserId,
|
|
|
|
project._id,
|
|
|
|
null,
|
|
|
|
(err, canRead) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
callback(null, canRead)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function _getFileStream(linkedFileData, userId, callback) {
|
|
|
|
callback = _.once(callback)
|
2022-05-13 05:35:31 -04:00
|
|
|
const {
|
|
|
|
source_output_file_path: sourceOutputFilePath,
|
|
|
|
build_id: buildId,
|
|
|
|
clsiServerId,
|
|
|
|
} = linkedFileData
|
2021-09-22 08:32:16 -04:00
|
|
|
LinkedFilesHandler.getSourceProject(linkedFileData, (err, project) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
const sourceProjectId = project._id
|
2022-06-23 11:05:45 -04:00
|
|
|
CompileManager.getProjectCompileLimits(sourceProjectId, (err, limits) => {
|
|
|
|
if (err) return callback(err)
|
|
|
|
|
|
|
|
ClsiManager.getOutputFileStream(
|
|
|
|
sourceProjectId,
|
|
|
|
userId,
|
|
|
|
limits,
|
|
|
|
clsiServerId,
|
|
|
|
buildId,
|
|
|
|
sourceOutputFilePath,
|
|
|
|
(err, readStream) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, readStream)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2022-06-23 11:05:45 -04:00
|
|
|
)
|
|
|
|
})
|
2021-09-22 08:32:16 -04:00
|
|
|
})
|
|
|
|
}
|
2019-05-29 05:21:06 -04:00
|
|
|
|
2021-09-22 08:32:16 -04:00
|
|
|
function _compileAndGetFileStream(linkedFileData, userId, callback) {
|
|
|
|
callback = _.once(callback)
|
|
|
|
const { source_output_file_path: sourceOutputFilePath } = linkedFileData
|
|
|
|
LinkedFilesHandler.getSourceProject(linkedFileData, (err, project) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
const sourceProjectId = project._id
|
|
|
|
CompileManager.compile(
|
|
|
|
sourceProjectId,
|
|
|
|
userId,
|
|
|
|
{},
|
2022-05-13 05:35:31 -04:00
|
|
|
(err, status, outputFiles, clsiServerId, limits) => {
|
2021-09-22 08:32:16 -04:00
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
2021-04-14 09:17:21 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
if (status !== 'success') {
|
|
|
|
return callback(new CompileFailedError())
|
|
|
|
}
|
|
|
|
const outputFile = _.find(
|
|
|
|
outputFiles,
|
|
|
|
o => o.path === sourceOutputFilePath
|
|
|
|
)
|
|
|
|
if (outputFile == null) {
|
|
|
|
return callback(new OutputFileFetchFailedError())
|
|
|
|
}
|
|
|
|
const buildId = outputFile.build
|
|
|
|
ClsiManager.getOutputFileStream(
|
|
|
|
sourceProjectId,
|
|
|
|
userId,
|
2022-06-23 11:05:45 -04:00
|
|
|
limits,
|
2022-05-13 05:35:31 -04:00
|
|
|
clsiServerId,
|
2021-09-22 08:32:16 -04:00
|
|
|
buildId,
|
|
|
|
sourceOutputFilePath,
|
|
|
|
(err, readStream) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err)
|
|
|
|
}
|
|
|
|
callback(null, readStream, buildId)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
2019-05-29 05:21:06 -04:00
|
|
|
}
|
2021-09-22 08:32:16 -04:00
|
|
|
|
|
|
|
module.exports = { createLinkedFile, refreshLinkedFile }
|