file.js 6.6 KB

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