file.js 5.6 KB

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