file.js 6.9 KB

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