file.js 7.0 KB

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