mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-24 21:12:38 -04:00
f6d6f01f78
GitOrigin-RevId: 4b05d6cbc291363f22b2511b5cd74dbf6c6bb7fb
91 lines
2.1 KiB
JavaScript
91 lines
2.1 KiB
JavaScript
const logger = require('@overleaf/logger')
|
|
const request = require('request')
|
|
const _ = require('underscore')
|
|
const urlValidator = require('valid-url')
|
|
const { InvalidUrlError, UrlFetchFailedError } = require('./LinkedFilesErrors')
|
|
const LinkedFilesHandler = require('./LinkedFilesHandler')
|
|
const UrlHelper = require('../Helpers/UrlHelper')
|
|
|
|
function createLinkedFile(
|
|
projectId,
|
|
linkedFileData,
|
|
name,
|
|
parentFolderId,
|
|
userId,
|
|
callback
|
|
) {
|
|
logger.info(
|
|
{ projectId, userId, url: linkedFileData.url },
|
|
'create linked file'
|
|
)
|
|
linkedFileData = _sanitizeData(linkedFileData)
|
|
_getUrlStream(projectId, linkedFileData, userId, (err, readStream) => {
|
|
if (err) {
|
|
return callback(err)
|
|
}
|
|
readStream.on('error', callback)
|
|
readStream.on('response', response => {
|
|
if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
LinkedFilesHandler.importFromStream(
|
|
projectId,
|
|
readStream,
|
|
linkedFileData,
|
|
name,
|
|
parentFolderId,
|
|
userId,
|
|
(err, file) => {
|
|
if (err) {
|
|
return callback(err)
|
|
}
|
|
callback(null, file._id)
|
|
}
|
|
) // Created
|
|
} else {
|
|
const error = new UrlFetchFailedError(
|
|
`url fetch failed: ${linkedFileData.url}`
|
|
)
|
|
error.statusCode = response.statusCode
|
|
callback(error)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
function refreshLinkedFile(
|
|
projectId,
|
|
linkedFileData,
|
|
name,
|
|
parentFolderId,
|
|
userId,
|
|
callback
|
|
) {
|
|
createLinkedFile(
|
|
projectId,
|
|
linkedFileData,
|
|
name,
|
|
parentFolderId,
|
|
userId,
|
|
callback
|
|
)
|
|
}
|
|
|
|
function _sanitizeData(data) {
|
|
return {
|
|
provider: data.provider,
|
|
url: UrlHelper.prependHttpIfNeeded(data.url),
|
|
}
|
|
}
|
|
|
|
function _getUrlStream(projectId, data, currentUserId, callback) {
|
|
callback = _.once(callback)
|
|
let { url } = data
|
|
if (!urlValidator.isWebUri(url)) {
|
|
return callback(new InvalidUrlError(`invalid url: ${url}`))
|
|
}
|
|
url = UrlHelper.wrapUrlWithProxy(url)
|
|
const readStream = request.get(url)
|
|
readStream.pause()
|
|
callback(null, readStream)
|
|
}
|
|
|
|
module.exports = { createLinkedFile, refreshLinkedFile }
|