|
|
@@ -10,12 +10,11 @@ import fs from 'node:fs'
|
|
|
import OError from '@overleaf/o-error'
|
|
|
import logger from '@overleaf/logger'
|
|
|
import crypto from 'node:crypto'
|
|
|
-import _ from 'lodash'
|
|
|
import Settings from '@overleaf/settings'
|
|
|
-import request from 'request'
|
|
|
+import { fetchStream } from '@overleaf/fetch-utils'
|
|
|
import { Transform, pipeline } from 'node:stream'
|
|
|
import { FileTooLargeError } from '../Features/Errors/Errors.js'
|
|
|
-import { promisify } from '@overleaf/promise-utils'
|
|
|
+import { callbackify, promisify } from '@overleaf/promise-utils'
|
|
|
|
|
|
export class SizeLimitedStream extends Transform {
|
|
|
constructor(options) {
|
|
|
@@ -146,47 +145,33 @@ const FileWriter = {
|
|
|
callback(null, fsPath)
|
|
|
})
|
|
|
},
|
|
|
+}
|
|
|
|
|
|
- writeUrlToDisk(identifier, url, options, callback) {
|
|
|
- if (typeof options === 'function') {
|
|
|
- callback = options
|
|
|
- options = {}
|
|
|
- }
|
|
|
- if (callback == null) {
|
|
|
- callback = function () {}
|
|
|
- }
|
|
|
- options = options || {}
|
|
|
- callback = _.once(callback)
|
|
|
-
|
|
|
- const stream = request.get(url)
|
|
|
- const errorHandler = function (err) {
|
|
|
- logger.warn(
|
|
|
- { err, identifier, url },
|
|
|
- '[writeUrlToDisk] something went wrong with writing to disk'
|
|
|
- )
|
|
|
- callback(err)
|
|
|
- }
|
|
|
- stream.on('error', errorHandler)
|
|
|
- stream.on('response', function (response) {
|
|
|
- stream.removeListener('error', errorHandler)
|
|
|
- if (response.statusCode >= 200 && response.statusCode < 300) {
|
|
|
- FileWriter.writeStreamToDisk(identifier, stream, options, callback)
|
|
|
- } else {
|
|
|
- const err = new OError('bad response from url', {
|
|
|
- statusCode: response.statusCode,
|
|
|
- })
|
|
|
- logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
|
|
|
- return callback(err)
|
|
|
- }
|
|
|
+async function writeUrlToDisk(identifier, url, options = {}) {
|
|
|
+ let stream
|
|
|
+ try {
|
|
|
+ stream = await fetchStream(url)
|
|
|
+ } catch (error) {
|
|
|
+ const err = new OError('bad response from url', {
|
|
|
+ statusCode: error.response?.status,
|
|
|
})
|
|
|
- },
|
|
|
+ logger.warn({ err, identifier, url }, `[writeUrlToDisk] ${err.message}`)
|
|
|
+ throw err
|
|
|
+ }
|
|
|
+ return await FileWriter.promises.writeStreamToDisk(
|
|
|
+ identifier,
|
|
|
+ stream,
|
|
|
+ options
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
+FileWriter.writeUrlToDisk = callbackify(writeUrlToDisk)
|
|
|
+
|
|
|
FileWriter.promises = {
|
|
|
writeLinesToDisk: promisify(FileWriter.writeLinesToDisk),
|
|
|
writeContentToDisk: promisify(FileWriter.writeContentToDisk),
|
|
|
writeStreamToDisk: promisify(FileWriter.writeStreamToDisk),
|
|
|
- writeUrlToDisk: promisify(FileWriter.writeUrlToDisk),
|
|
|
+ writeUrlToDisk,
|
|
|
}
|
|
|
|
|
|
export default FileWriter
|