mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-22 02:18:27 +00:00
Merge pull request #22239 from overleaf/jpa-file-url-helper
[web] add helper for getting file URL from history w/ filestore fallback GitOrigin-RevId: d77e6f1565fab407e1004a02c4bfdfe03a0214d7
This commit is contained in:
parent
8db1f2c9bc
commit
b9a56afe4c
4 changed files with 89 additions and 21 deletions
19
services/web/app/src/Features/History/HistoryURLHelper.js
Normal file
19
services/web/app/src/Features/History/HistoryURLHelper.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
// Pass settings to enable consistent unit tests from .js and .mjs modules
|
||||
function projectHistoryURLWithFilestoreFallback(
|
||||
Settings,
|
||||
projectId,
|
||||
historyId,
|
||||
fileRef
|
||||
) {
|
||||
const filestoreURL = `${Settings.apis.filestore.url}/project/${projectId}/file/${fileRef._id}`
|
||||
if (fileRef.hash) {
|
||||
return {
|
||||
url: `${Settings.apis.project_history.url}/project/${historyId}/blob/${fileRef.hash}`,
|
||||
fallbackURL: filestoreURL,
|
||||
}
|
||||
} else {
|
||||
return { url: filestoreURL }
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { projectHistoryURLWithFilestoreFallback }
|
|
@ -24,6 +24,7 @@ import _ from 'lodash'
|
|||
import Async from 'async'
|
||||
import Errors from '../Errors/Errors.js'
|
||||
import { promisify } from '@overleaf/promise-utils'
|
||||
import HistoryURLHelper from '../History/HistoryURLHelper.js'
|
||||
|
||||
let ReferencesHandler
|
||||
|
||||
|
@ -38,18 +39,6 @@ export default ReferencesHandler = {
|
|||
}
|
||||
},
|
||||
|
||||
_buildFileUrl(projectId, historyId, fileRef) {
|
||||
const filestoreURL = `${settings.apis.filestore.url}/project/${projectId}/file/${fileRef._id}`
|
||||
if (fileRef.hash) {
|
||||
return {
|
||||
url: `${settings.apis.project_history.url}/project/${historyId}/blob/${fileRef.hash}`,
|
||||
fallbackURL: filestoreURL,
|
||||
}
|
||||
} else {
|
||||
return { url: filestoreURL }
|
||||
}
|
||||
},
|
||||
|
||||
_findBibFileRefs(project) {
|
||||
const fileRefs = []
|
||||
function _process(folder) {
|
||||
|
@ -179,7 +168,12 @@ export default ReferencesHandler = {
|
|||
ReferencesHandler._buildDocUrl(projectId, docId)
|
||||
)
|
||||
const bibFileUrls = fileRefs.map(fileRef =>
|
||||
ReferencesHandler._buildFileUrl(projectId, historyId, fileRef)
|
||||
HistoryURLHelper.projectHistoryURLWithFilestoreFallback(
|
||||
settings,
|
||||
projectId,
|
||||
historyId,
|
||||
fileRef
|
||||
)
|
||||
)
|
||||
const sourceURLs = bibDocUrls.concat(bibFileUrls)
|
||||
return request.post(
|
||||
|
|
|
@ -6,6 +6,7 @@ const metrics = require('@overleaf/metrics')
|
|||
const Path = require('path')
|
||||
const { fetchNothing } = require('@overleaf/fetch-utils')
|
||||
const settings = require('@overleaf/settings')
|
||||
const HistoryURLHelper = require('../History/HistoryURLHelper')
|
||||
|
||||
const CollaboratorsGetter =
|
||||
require('../Collaborators/CollaboratorsGetter').promises
|
||||
|
@ -81,12 +82,13 @@ async function addFile(params) {
|
|||
folderId,
|
||||
} = params
|
||||
// Go through project-history to avoid the need for handling history-v1 authentication.
|
||||
const streamOrigin =
|
||||
settings.apis.project_history.url +
|
||||
Path.join(`/project/${historyId}/blob/${hash}`)
|
||||
const streamFallback =
|
||||
settings.apis.filestore.url +
|
||||
Path.join(`/project/${projectId}`, `/file/${fileId}`)
|
||||
const { url, fallbackURL } =
|
||||
HistoryURLHelper.projectHistoryURLWithFilestoreFallback(
|
||||
settings,
|
||||
projectId,
|
||||
historyId,
|
||||
{ _id: fileId, hash }
|
||||
)
|
||||
|
||||
await addEntity({
|
||||
projectId,
|
||||
|
@ -94,8 +96,8 @@ async function addFile(params) {
|
|||
projectName,
|
||||
rev,
|
||||
folderId,
|
||||
streamOrigin,
|
||||
streamFallback,
|
||||
streamOrigin: url,
|
||||
streamFallback: fallbackURL,
|
||||
entityId: fileId,
|
||||
entityType: 'file',
|
||||
})
|
||||
|
|
|
@ -116,6 +116,59 @@ describe('TpdsUpdateSender', function () {
|
|||
})
|
||||
|
||||
it('queues a post the file with user and file id', async function () {
|
||||
const fileId = '4545345'
|
||||
const hash = undefined
|
||||
const historyId = 91525
|
||||
const path = '/some/path/here.jpg'
|
||||
|
||||
await this.TpdsUpdateSender.promises.addFile({
|
||||
projectId,
|
||||
historyId,
|
||||
fileId,
|
||||
hash,
|
||||
path,
|
||||
projectName,
|
||||
})
|
||||
|
||||
expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
|
||||
this.enqueueUrl,
|
||||
{
|
||||
json: {
|
||||
group: userId,
|
||||
method: 'pipeStreamFrom',
|
||||
job: {
|
||||
method: 'post',
|
||||
streamOrigin: `${filestoreUrl}/project/${projectId}/file/${fileId}`,
|
||||
uri: `${thirdPartyDataStoreApiUrl}/user/${userId}/entity/${encodeURIComponent(
|
||||
projectName
|
||||
)}${encodeURIComponent(path)}`,
|
||||
headers: {},
|
||||
},
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
|
||||
this.enqueueUrl,
|
||||
{
|
||||
json: {
|
||||
group: collaberatorRef,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
expect(this.FetchUtils.fetchNothing).to.have.been.calledWithMatch(
|
||||
this.enqueueUrl,
|
||||
{
|
||||
json: {
|
||||
group: readOnlyRef,
|
||||
job: {},
|
||||
},
|
||||
}
|
||||
)
|
||||
})
|
||||
|
||||
it('queues a post the file with user and file id and hash', async function () {
|
||||
const fileId = '4545345'
|
||||
const hash = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
|
||||
const historyId = 91525
|
||||
|
|
Loading…
Add table
Reference in a new issue