file.js 6.1 KB

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