postgres.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const { Blob } = require('overleaf-editor-core')
  2. const assert = require('../assert')
  3. const knex = require('../knex')
  4. /**
  5. * Set up the initial data structures for a project
  6. */
  7. async function initialize(projectId) {
  8. // Nothing to do for Postgres
  9. }
  10. /**
  11. * Return blob metadata for the given project and hash
  12. */
  13. async function findBlob(projectId, hash) {
  14. projectId = parseInt(projectId, 10)
  15. assert.integer(projectId, 'bad projectId')
  16. assert.blobHash(hash, 'bad hash')
  17. const binaryHash = hashToBuffer(hash)
  18. const record = await knex('project_blobs')
  19. .select('hash_bytes', 'byte_length', 'string_length')
  20. .where({
  21. project_id: projectId,
  22. hash_bytes: binaryHash,
  23. })
  24. .first()
  25. return recordToBlob(record)
  26. }
  27. /**
  28. * Read multiple blob metadata records by hexadecimal hashes.
  29. *
  30. * @param {Array.<string>} hashes hexadecimal SHA-1 hashes
  31. * @return {Promise.<Array.<Blob?>>} no guarantee on order
  32. */
  33. async function findBlobs(projectId, hashes) {
  34. projectId = parseInt(projectId, 10)
  35. assert.integer(projectId, 'bad projectId')
  36. assert.array(hashes, 'bad hashes: not array')
  37. hashes.forEach(function (hash) {
  38. assert.blobHash(hash, 'bad hash')
  39. })
  40. const binaryHashes = hashes.map(hashToBuffer)
  41. const records = await knex('project_blobs')
  42. .select('hash_bytes', 'byte_length', 'string_length')
  43. .where('project_id', projectId)
  44. .whereIn('hash_bytes', binaryHashes)
  45. const blobs = records.map(recordToBlob)
  46. return blobs
  47. }
  48. /**
  49. * Add a blob's metadata to the blobs table after it has been uploaded.
  50. */
  51. async function insertBlob(projectId, blob) {
  52. projectId = parseInt(projectId, 10)
  53. assert.integer(projectId, 'bad projectId')
  54. await knex('project_blobs')
  55. .insert(blobToRecord(projectId, blob))
  56. .onConflict(['project_id', 'hash_bytes'])
  57. .ignore()
  58. }
  59. /**
  60. * Deletes all blobs for a given project
  61. */
  62. async function deleteBlobs(projectId) {
  63. projectId = parseInt(projectId, 10)
  64. assert.integer(projectId, 'bad projectId')
  65. await knex('project_blobs').where('project_id', projectId).delete()
  66. }
  67. function blobToRecord(projectId, blob) {
  68. return {
  69. project_id: projectId,
  70. hash_bytes: hashToBuffer(blob.hash),
  71. byte_length: blob.getByteLength(),
  72. string_length: blob.getStringLength(),
  73. }
  74. }
  75. function recordToBlob(record) {
  76. if (!record) return
  77. return new Blob(
  78. hashFromBuffer(record.hash_bytes),
  79. record.byte_length,
  80. record.string_length
  81. )
  82. }
  83. function hashToBuffer(hash) {
  84. if (!hash) return
  85. return Buffer.from(hash, 'hex')
  86. }
  87. function hashFromBuffer(buffer) {
  88. if (!buffer) return
  89. return buffer.toString('hex')
  90. }
  91. module.exports = {
  92. initialize,
  93. findBlob,
  94. findBlobs,
  95. insertBlob,
  96. deleteBlobs,
  97. }