2020-02-19 11:14:28 +00:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-02-19 11:14:14 +00:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-02-19 11:14:37 +00:00
|
|
|
const UrlFetcher = require('./UrlFetcher')
|
2021-07-12 16:47:21 +00:00
|
|
|
const Settings = require('@overleaf/settings')
|
2020-02-19 11:14:37 +00:00
|
|
|
const fs = require('fs')
|
2021-10-06 08:11:59 +00:00
|
|
|
const Path = require('path')
|
|
|
|
const { callbackify } = require('util')
|
2024-01-17 09:22:46 +00:00
|
|
|
const Metrics = require('./Metrics')
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
const PENDING_DOWNLOADS = new Map()
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
function getProjectDir(projectId) {
|
|
|
|
return Path.join(Settings.path.clsiCacheDir, projectId)
|
|
|
|
}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
function getCachePath(projectId, url, lastModified) {
|
|
|
|
// The url is a filestore URL.
|
|
|
|
// It is sufficient to look at the path and mtime for uniqueness.
|
|
|
|
const mtime = (lastModified && lastModified.getTime()) || 0
|
|
|
|
const key = new URL(url).pathname.replace(/\//g, '-') + '-' + mtime
|
|
|
|
return Path.join(getProjectDir(projectId), key)
|
|
|
|
}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2024-01-17 09:22:46 +00:00
|
|
|
async function clearProject(projectId, options) {
|
|
|
|
const timer = new Metrics.Timer('url_cache', {
|
|
|
|
status: options?.reason || 'unknown',
|
|
|
|
path: 'delete',
|
|
|
|
})
|
2024-01-08 10:43:41 +00:00
|
|
|
await fs.promises.rm(getProjectDir(projectId), {
|
|
|
|
force: true,
|
|
|
|
recursive: true,
|
|
|
|
})
|
2024-01-17 09:22:46 +00:00
|
|
|
timer.done()
|
2021-10-06 08:11:59 +00:00
|
|
|
}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
async function createProjectDir(projectId) {
|
|
|
|
await fs.promises.mkdir(getProjectDir(projectId), { recursive: true })
|
|
|
|
}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
async function downloadUrlToFile(projectId, url, destPath, lastModified) {
|
|
|
|
const cachePath = getCachePath(projectId, url, lastModified)
|
|
|
|
try {
|
2024-01-17 09:22:46 +00:00
|
|
|
const timer = new Metrics.Timer('url_cache', {
|
|
|
|
status: 'cache-hit',
|
|
|
|
path: 'copy',
|
|
|
|
})
|
2021-10-06 08:11:59 +00:00
|
|
|
await fs.promises.copyFile(cachePath, destPath)
|
2024-01-17 09:22:46 +00:00
|
|
|
// the metric is only updated if the file is present in the cache
|
|
|
|
timer.done()
|
2021-10-06 08:11:59 +00:00
|
|
|
return
|
|
|
|
} catch (e) {
|
|
|
|
if (e.code !== 'ENOENT') {
|
|
|
|
throw e
|
2020-02-19 11:14:37 +00:00
|
|
|
}
|
2021-10-06 08:11:59 +00:00
|
|
|
}
|
2024-01-17 09:22:46 +00:00
|
|
|
// time the download
|
|
|
|
{
|
|
|
|
const timer = new Metrics.Timer('url_cache', {
|
|
|
|
status: 'cache-miss',
|
|
|
|
path: 'download',
|
|
|
|
})
|
|
|
|
try {
|
|
|
|
await download(url, cachePath)
|
|
|
|
} finally {
|
|
|
|
timer.done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// time the file copy
|
|
|
|
{
|
|
|
|
const timer = new Metrics.Timer('url_cache', {
|
|
|
|
status: 'cache-miss',
|
|
|
|
path: 'copy',
|
|
|
|
})
|
|
|
|
await fs.promises.copyFile(cachePath, destPath)
|
|
|
|
timer.done()
|
|
|
|
}
|
2021-10-06 08:11:59 +00:00
|
|
|
}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
async function download(url, cachePath) {
|
|
|
|
let pending = PENDING_DOWNLOADS.get(cachePath)
|
|
|
|
if (pending) {
|
|
|
|
return pending
|
|
|
|
}
|
2018-07-30 15:22:04 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
pending = UrlFetcher.promises.pipeUrlToFileWithRetry(url, cachePath)
|
|
|
|
PENDING_DOWNLOADS.set(cachePath, pending)
|
|
|
|
try {
|
|
|
|
await pending
|
|
|
|
} finally {
|
|
|
|
PENDING_DOWNLOADS.delete(cachePath)
|
|
|
|
}
|
|
|
|
}
|
2014-02-12 17:27:43 +00:00
|
|
|
|
2021-10-06 08:11:59 +00:00
|
|
|
module.exports = {
|
|
|
|
clearProject: callbackify(clearProject),
|
|
|
|
createProjectDir: callbackify(createProjectDir),
|
|
|
|
downloadUrlToFile: callbackify(downloadUrlToFile),
|
|
|
|
promises: {
|
|
|
|
clearProject,
|
|
|
|
createProjectDir,
|
|
|
|
downloadUrlToFile,
|
2021-07-13 11:04:48 +00:00
|
|
|
},
|
2020-02-19 11:14:37 +00:00
|
|
|
}
|