file.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. 'use strict'
  2. const _ = require('lodash')
  3. const assert = require('check-types').assert
  4. const OError = require('@overleaf/o-error')
  5. const FileData = require('./file_data')
  6. const HashFileData = require('./file_data/hash_file_data')
  7. const StringFileData = require('./file_data/string_file_data')
  8. /**
  9. * @typedef {import("./blob")} Blob
  10. * @typedef {import("./types").BlobStore} BlobStore
  11. * @typedef {import("./types").StringFileRawData} StringFileRawData
  12. * @typedef {import("./types").CommentRawData} CommentRawData
  13. * @typedef {import("./operation/text_operation")} TextOperation
  14. * @typedef {{filterTrackedDeletes?: boolean}} FileGetContentOptions
  15. */
  16. class NotEditableError extends OError {
  17. constructor() {
  18. super('File is not editable')
  19. }
  20. }
  21. /**
  22. * A file in a {@link Snapshot}. A file has both data and metadata. There
  23. * are several classes of data that represent the various types of file
  24. * data that are supported, namely text and binary, and also the various
  25. * states that a file's data can be in, namely:
  26. *
  27. * 1. Hash only: all we know is the file's hash; this is how we encode file
  28. * content in long term storage.
  29. * 2. Lazily loaded: the hash of the file, its length, and its type are known,
  30. * but its content is not loaded. Operations are cached for application
  31. * later.
  32. * 3. Eagerly loaded: the content of a text file is fully loaded into memory
  33. * as a string.
  34. * 4. Hollow: only the byte and/or UTF-8 length of the file are known; this is
  35. * used to allow for validation of operations when editing collaboratively
  36. * without having to keep file data in memory on the server.
  37. */
  38. class File {
  39. /**
  40. * Blob hash for an empty file.
  41. *
  42. * @type {String}
  43. */
  44. static EMPTY_FILE_HASH = 'e69de29bb2d1d6434b8b29ae775ad8c2e48c5391'
  45. static NotEditableError = NotEditableError
  46. /**
  47. * @param {FileData} data
  48. * @param {Object} [metadata]
  49. */
  50. constructor(data, metadata) {
  51. assert.instance(data, FileData, 'File: bad data')
  52. this.data = data
  53. this.setMetadata(metadata || {})
  54. }
  55. static fromRaw(raw) {
  56. if (!raw) return null
  57. return new File(FileData.fromRaw(raw), raw.metadata)
  58. }
  59. /**
  60. * @param {string} hash
  61. * @param {string} [rangesHash]
  62. * @param {Object} [metadata]
  63. * @return {File}
  64. */
  65. static fromHash(hash, rangesHash, metadata) {
  66. return new File(new HashFileData(hash, rangesHash), metadata)
  67. }
  68. /**
  69. * @param {string} string
  70. * @param {Object} [metadata]
  71. * @return {File}
  72. */
  73. static fromString(string, metadata) {
  74. return new File(new StringFileData(string), metadata)
  75. }
  76. /**
  77. * @param {number} [byteLength]
  78. * @param {number} [stringLength]
  79. * @param {Object} [metadata]
  80. * @return {File}
  81. */
  82. static createHollow(byteLength, stringLength, metadata) {
  83. return new File(FileData.createHollow(byteLength, stringLength), metadata)
  84. }
  85. /**
  86. * @param {Blob} blob
  87. * @param {Blob} [blob]
  88. * @param {Object} [metadata]
  89. * @return {File}
  90. */
  91. static createLazyFromBlobs(blob, rangesBlob, metadata) {
  92. return new File(FileData.createLazyFromBlobs(blob, rangesBlob), metadata)
  93. }
  94. toRaw() {
  95. const rawFileData = this.data.toRaw()
  96. storeRawMetadata(this.metadata, rawFileData)
  97. return rawFileData
  98. }
  99. /**
  100. * Hexadecimal SHA-1 hash of the file's content, if known.
  101. *
  102. * @return {string | null | undefined}
  103. */
  104. getHash() {
  105. return this.data.getHash()
  106. }
  107. /**
  108. * Hexadecimal SHA-1 hash of the ranges content (comments + tracked changes),
  109. * if known.
  110. *
  111. * @return {string | null | undefined}
  112. */
  113. getRangesHash() {
  114. return this.data.getRangesHash()
  115. }
  116. /**
  117. * The content of the file, if it is known and if this file has UTF-8 encoded
  118. * content.
  119. *
  120. * @param {FileGetContentOptions} [opts]
  121. * @return {string | null | undefined}
  122. */
  123. getContent(opts = {}) {
  124. return this.data.getContent(opts)
  125. }
  126. /**
  127. * Whether this file has string content and is small enough to be edited using
  128. * {@link TextOperation}s.
  129. *
  130. * @return {boolean | null | undefined} null if it is not currently known
  131. */
  132. isEditable() {
  133. return this.data.isEditable()
  134. }
  135. /**
  136. * The length of the file's content in bytes, if known.
  137. *
  138. * @return {number | null | undefined}
  139. */
  140. getByteLength() {
  141. return this.data.getByteLength()
  142. }
  143. /**
  144. * The length of the file's content in characters, if known.
  145. *
  146. * @return {number | null | undefined}
  147. */
  148. getStringLength() {
  149. return this.data.getStringLength()
  150. }
  151. /**
  152. * Return the metadata object for this file.
  153. *
  154. * @return {Object}
  155. */
  156. getMetadata() {
  157. return this.metadata
  158. }
  159. /**
  160. * Set the metadata object for this file.
  161. *
  162. * @param {Object} metadata
  163. */
  164. setMetadata(metadata) {
  165. assert.object(metadata, 'File: bad metadata')
  166. this.metadata = metadata
  167. }
  168. /**
  169. * Edit this file, if possible.
  170. *
  171. * @param {TextOperation} textOperation
  172. */
  173. edit(textOperation) {
  174. if (!this.data.isEditable()) throw new File.NotEditableError()
  175. this.data.edit(textOperation)
  176. }
  177. /**
  178. * Get the comments for this file.
  179. *
  180. * @return {CommentRawData[]}
  181. */
  182. getComments() {
  183. return this.data.getComments()
  184. }
  185. /**
  186. * Clone a file.
  187. *
  188. * @return {File} a new object of the same type
  189. */
  190. clone() {
  191. return File.fromRaw(this.toRaw())
  192. }
  193. /**
  194. * Convert this file's data to the given kind. This may require us to load file
  195. * size or content from the given blob store, so this is an asynchronous
  196. * operation.
  197. *
  198. * @param {string} kind
  199. * @param {BlobStore} blobStore
  200. * @return {Promise.<File>} for this
  201. */
  202. async load(kind, blobStore) {
  203. const data = await this.data.load(kind, blobStore)
  204. this.data = data
  205. return this
  206. }
  207. /**
  208. * Store the file's content in the blob store and return a raw file with
  209. * the corresponding hash. As a side effect, make this object consistent with
  210. * the hash.
  211. *
  212. * @param {BlobStore} blobStore
  213. * @return {Promise<Object>} a raw HashFile
  214. */
  215. async store(blobStore) {
  216. const raw = await this.data.store(blobStore)
  217. storeRawMetadata(this.metadata, raw)
  218. return raw
  219. }
  220. }
  221. function storeRawMetadata(metadata, raw) {
  222. if (!_.isEmpty(metadata)) {
  223. raw.metadata = _.cloneDeep(metadata)
  224. }
  225. }
  226. module.exports = File