Merge pull request #22213 from overleaf/ar-avoid-duplicate-blob-writes

Avoid duplicate blob writes

GitOrigin-RevId: 5e7a472b235bae5ef84389dbbe791189e951908a
This commit is contained in:
Andrew Rumble
2024-12-04 09:05:08 +00:00
committed by Copybot
parent f676eca2b8
commit 40b781eb0a
12 changed files with 143 additions and 9 deletions
@@ -490,6 +490,7 @@ function _getUpdates(
url: newEntity.url,
hash: newEntity.file != null ? newEntity.file.hash : undefined,
metadata: buildFileMetadataForHistory(newEntity.file),
createdBlob: newEntity.createdBlob ?? false,
})
} else if (newEntity.path !== oldEntity.path) {
// entity renamed
@@ -100,7 +100,7 @@ const FileStoreHandler = {
})
return callback(err)
}
callback(err, result.url, result.fileRef)
callback(err, result.url, result.fileRef, true)
}
)
}
@@ -303,6 +303,6 @@ module.exports = FileStoreHandler
module.exports.promises = promisifyAll(FileStoreHandler, {
multiResult: {
uploadFileFromDisk: ['url', 'fileRef'],
uploadFileFromDiskWithHistoryId: ['url', 'fileRef'],
uploadFileFromDiskWithHistoryId: ['url', 'fileRef', 'createdBlob'],
},
})
@@ -211,6 +211,7 @@ async function _copyFiles(sourceEntries, sourceProject, targetProject) {
if (sourceFile.hash != null) {
file.hash = sourceFile.hash
}
let createdBlob = false
if (file.hash != null) {
try {
await HistoryManager.promises.copyBlob(
@@ -218,6 +219,7 @@ async function _copyFiles(sourceEntries, sourceProject, targetProject) {
targetHistoryId,
file.hash
)
createdBlob = true
} catch (err) {
logger.error(
{
@@ -237,7 +239,7 @@ async function _copyFiles(sourceEntries, sourceProject, targetProject) {
targetProject._id,
file._id
)
return { file, path, url }
return { createdBlob, file, path, url }
}
)
return targetEntries
@@ -194,14 +194,14 @@ async function _createFile(project, projectPath, fsPath) {
throw new OError('missing history id')
}
const fileName = Path.basename(projectPath)
const { fileRef, url } =
const { createdBlob, fileRef, url } =
await FileStoreHandler.promises.uploadFileFromDiskWithHistoryId(
projectId,
historyId,
{ name: fileName },
fsPath
)
return { file: fileRef, path: projectPath, url }
return { createdBlob, file: fileRef, path: projectPath, url }
}
async function _notifyDocumentUpdater(project, userId, changes) {
@@ -1132,6 +1132,7 @@ describe('DocumentUpdaterHandler', function () {
hash: undefined,
ranges: undefined,
metadata: undefined,
createdBlob: false,
},
]
@@ -1184,6 +1185,7 @@ describe('DocumentUpdaterHandler', function () {
historyRangesSupport: false,
hash: '12345',
ranges: undefined,
createdBlob: false,
metadata: undefined,
},
]
@@ -1296,6 +1298,7 @@ describe('DocumentUpdaterHandler', function () {
hash: undefined,
ranges: undefined,
metadata: undefined,
createdBlob: false,
},
]
@@ -1398,6 +1401,7 @@ describe('DocumentUpdaterHandler', function () {
hash: undefined,
ranges: this.ranges,
metadata: undefined,
createdBlob: false,
},
]
@@ -1444,6 +1448,7 @@ describe('DocumentUpdaterHandler', function () {
hash: undefined,
ranges: this.ranges,
metadata: undefined,
createdBlob: false,
},
]
@@ -105,16 +105,19 @@ describe('ProjectDuplicator', function () {
]
this.fileEntries = [
{
createdBlob: false,
path: this.file0Path,
file: this.newFile0,
url: this.filestoreUrl,
},
{
createdBlob: false,
path: this.file1Path,
file: this.newFile1,
url: this.filestoreUrl,
},
{
createdBlob: true,
path: this.file2Path,
file: this.newFile2,
url: this.filestoreUrl,
@@ -58,7 +58,12 @@ describe('ProjectUploadManager', function () {
},
]
this.fileEntries = [
{ file: this.file, path: `/${this.file.name}`, url: this.fileStoreUrl },
{
file: this.file,
path: `/${this.file.name}`,
url: this.fileStoreUrl,
createdBlob: true,
},
]
this.fs = {
@@ -94,9 +99,11 @@ describe('ProjectUploadManager', function () {
}
this.FileStoreHandler = {
promises: {
uploadFileFromDiskWithHistoryId: sinon
.stub()
.resolves({ fileRef: this.file, url: this.fileStoreUrl }),
uploadFileFromDiskWithHistoryId: sinon.stub().resolves({
fileRef: this.file,
url: this.fileStoreUrl,
createdBlob: true,
}),
},
}
this.FileSystemImportManager = {