Merge pull request #21891 from overleaf/jpa-consistent-getObjectSize-type

[object-persistor] gcs: return a number from getObjectSize

GitOrigin-RevId: f3e0e3269e789b0077c82014c42a4ca63cd84b39
This commit is contained in:
Jakob Ackermann 2024-11-15 09:44:55 +01:00 committed by Copybot
parent b36bbdbd90
commit bd855044af
3 changed files with 27 additions and 5 deletions

View file

@ -180,7 +180,7 @@ module.exports = class GcsPersistor extends AbstractPersistor {
.bucket(bucketName)
.file(key)
.getMetadata()
return metadata.size
return parseInt(metadata.size, 10)
} catch (err) {
throw PersistorHelper.wrapError(
err,
@ -289,7 +289,10 @@ module.exports = class GcsPersistor extends AbstractPersistor {
)
}
return files.reduce((acc, file) => Number(file.metadata.size) + acc, 0)
return files.reduce(
(acc, file) => parseInt(file.metadata.size, 10) + acc,
0
)
}
async checkIfObjectExists(bucketName, key) {

View file

@ -45,11 +45,11 @@ describe('GcsPersistorTests', function () {
files = [
{
metadata: { size: 11, md5Hash: '/////wAAAAD/////AAAAAA==' },
metadata: { size: '11', md5Hash: '/////wAAAAD/////AAAAAA==' },
delete: sinon.stub(),
},
{
metadata: { size: 22, md5Hash: '/////wAAAAD/////AAAAAA==' },
metadata: { size: '22', md5Hash: '/////wAAAAD/////AAAAAA==' },
delete: sinon.stub(),
},
]
@ -302,7 +302,7 @@ describe('GcsPersistorTests', function () {
})
it('should return the object size', function () {
expect(size).to.equal(files[0].metadata.size)
expect(size).to.equal(11)
})
it('should pass the bucket and key to GCS', function () {

View file

@ -1354,6 +1354,25 @@ describe('Filestore', function () {
})
})
describe('getObjectSize', function () {
it('should return a number', async function () {
const buf = Buffer.from('hello')
const fileId = new ObjectId().toString()
const fileUrl = `${filestoreUrl}/project/${projectId}/file/${fileId}`
const res = await fetch(fileUrl, {
method: 'POST',
body: Stream.Readable.from([buf]),
})
if (!res.ok) throw new Error(res.statusText)
expect(
await app.persistor.getObjectSize(
Settings.filestore.stores.user_files,
`${projectId}/${fileId}`
)
).to.equal(buf.byteLength)
})
})
describe('checkIfObjectExists', function () {
it('should return false when the object does not exist', async function () {
expect(