file.js 5.8 KB

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