fs#ReadStream TypeScript Examples

The following examples show how to use fs#ReadStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: VideoPlayer.ts    From ytdl with MIT License 6 votes vote down vote up
/**
     * Play video given a url and a stream.
     * @param url Stream URL
     * @param stream Media stream
     */
    public play(url: string, stream: ReadStream) {
        stream.destroy(); // Video player does not support streams right now
        const subprocess = spawn(this.player, [url], { stdio: 'ignore' });
        if (!subprocess) {
            throw new Error(`Unable to spawn subprocess with ${this.player}`);
        }

        subprocess.on('close', () => { process.exit(0); });
        return subprocess;
    }
Example #2
Source File: index.ts    From Aragorn with MIT License 6 votes vote down vote up
async upload(options: UploadOptions): Promise<UploadResponse> {
    try {
      const { file, fileName, directoryPath, isFromFileManage } = options;
      const fileStream = this.getStream(file);
      const { path } = this.config;
      let newFileName = '';
      if (isFromFileManage) {
        newFileName = directoryPath ? `${directoryPath}/${fileName}` : `${fileName}`;
      } else {
        newFileName = path ? `${path}/${fileName}` : `${fileName}`;
      }
      const { key }: any = await this.qiniuUpload(newFileName, fileStream as ReadStream);
      const url = this.qiniuDownload(key);
      if (url) {
        return {
          success: true,
          data: { url }
        };
      } else {
        return {
          success: false,
          desc: '上传失败'
        };
      }
    } catch (err) {
      return {
        success: false,
        desc: err.message
      };
    }
  }
Example #3
Source File: index.ts    From Aragorn with MIT License 6 votes vote down vote up
protected qiniuUpload(fileName: string, file: ReadStream) {
    const config = this.getQiniuConfig();
    const token = this.getToken();
    const formUploader = new qiniu.form_up.FormUploader(config);
    const putExtra = new qiniu.form_up.PutExtra();
    return new Promise((resolve, reject) => {
      formUploader.putStream(token, fileName, file, putExtra, function (respErr, respBody, respInfo) {
        if (respErr) {
          reject(respErr);
        }
        resolve(respBody);
      });
    });
  }
Example #4
Source File: index.ts    From Aragorn with MIT License 6 votes vote down vote up
protected postFile(fileName: string, file: ReadStream | Buffer) {
    const { zone, bucket } = this.getConfig();
    const token = this.getToken(fileName);

    return new Promise<AxiosResponse>((resolve, reject) => {
      const formData = new FormData();
      formData.append('Authorization', token);
      formData.append('FileName', fileName);
      formData.append('file', file, {
        filename: fileName
      });
      formData.getLength(async (err, length) => {
        if (err) {
          console.log('content-length 获取失败');
          reject(new Error('content-length 获取失败'));
        } else {
          try {
            const requestOpetion: AxiosRequestConfig = {
              url: `http://${bucket}.${zone}.ufileos.com`,
              method: 'POST',
              headers: {
                ...formData.getHeaders(),
                'Content-Length': length
              },
              data: formData
            };
            // 发起请求
            const res = await axios(requestOpetion);
            resolve(res);
          } catch (err) {
            console.log('上传失败', err.message);
            reject(err);
          }
        }
      });
    });
  }
Example #5
Source File: AudioPlayer.ts    From ytdl with MIT License 6 votes vote down vote up
/**
     * Play video given a url and a stream.
     * @param url Stream URL
     * @param stream Media stream
     */
    public play(url: string, stream: ReadStream) {
        this.url = url;
        this.stream = stream;
        ffmpeg(stream).toFormat('s16le').pipe(new this.Speaker());
    }
Example #6
Source File: file-manager.ts    From open-design-sdk with Apache License 2.0 6 votes vote down vote up
async readFileStream(
    filePath: string,
    options: {
      cancelToken?: CancelToken | null
    } = {}
  ): Promise<ReadStream> {
    const cancelToken = createCancelToken.race([
      options.cancelToken,
      this._destroyTokenController.token,
    ])

    const filename = this._resolvePath(filePath)
    const stream = createReadStream(filename)

    return new Promise((resolve, reject) => {
      const unregisterCanceller = cancelToken.onCancelled((reason: unknown) => {
        stream.close()
        reject(reason)
      })
      const handleError = (err: Error) => {
        unregisterCanceller?.()
        reject(err)
      }
      const handleReadble = () => {
        unregisterCanceller?.()
        resolve(stream)
      }

      stream.once('readable', handleReadble)
      stream.once('error', handleError)
    })
  }
Example #7
Source File: utils.ts    From polkadot-watcher-csv-exporter with Apache License 2.0 6 votes vote down vote up
initReadFileStream = (dirPath: string,fileName: string,logger: Logger): ReadStream => {

  const filePath = `${dirPath}/${fileName}`;
  const file = fs.createReadStream(filePath);
  file.on('error', function(err) { logger.error(err.stack) });

  return file
}
Example #8
Source File: ResourceApi.ts    From joplin-utils with MIT License 6 votes vote down vote up
/**
   * Creates a new resource
   * TODO 目前大批量上传文件仍有问题
   * Creating a new resource is special because you also need to upload the file. Unlike other API calls, this one must have the "multipart/form-data" Content-Type. The file data must be passed to the "data" form field, and the other properties to the "props" form field. An example of a valid call with cURL would be:
   * The "data" field is required, while the "props" one is not. If not specified, default values will be used.
   * @param param
   */
  async create(param: { data: ReadStream } & Partial<ResourceProperties>): Promise<ResourceGetRes> {
    const { data, ...others } = param
    return (await this.ajax.postFormData('/resources', 'post', {
      props: JSON.stringify(others),
      data: param.data,
    })) as ResourceGetRes
  }
Example #9
Source File: ResourceApi.ts    From joplin-utils with MIT License 6 votes vote down vote up
async update(
    param: RequiredField<Partial<ResourceProperties>, 'id'> & { data?: ReadStream },
  ): Promise<ResourceGetRes> {
    const { id, data, ...others } = param
    return await this.ajax.postFormData<ResourceGetRes>(`/resources/${id}`, 'put', {
      props: JSON.stringify(others),
      data: data,
    })
  }
Example #10
Source File: sep12.ts    From stellar-anchor-tests with Apache License 2.0 6 votes vote down vote up
export function makeSep12Request(requestData: any): Request {
  let requestBody: string | FormData;
  if (hasBinaryFields(requestData.data)) {
    requestBody = new FormData();
    for (const key in requestData.data) {
      if (
        typeof requestData.data[key] === "object" &&
        requestData.data[key].data instanceof ReadStream
      ) {
        const stats = statSync(requestData.data[key].data.path);
        requestBody.append(key, requestData.data[key].data, {
          knownLength: stats.size,
          contentType: requestData.data[key].contentType,
          filename: requestData.data[key].fileName,
        });
      } else if (
        typeof requestData.data[key] === "object" &&
        requestData.data[key].data instanceof Buffer
      ) {
        requestBody.append(key, requestData.data[key].data, {
          knownLength: requestData.data[key].data.length,
          contentType: requestData.data[key].contentType,
          filename: requestData.data[key].fileName,
        });
      } else {
        requestBody.append(key, requestData.data[key]);
      }
    }
  } else {
    requestBody = JSON.stringify(requestData.data);
    requestData.headers["Content-Type"] = "application/json";
  }
  return new Request(requestData.url, {
    method: "PUT",
    headers: requestData.headers,
    body: requestBody,
  });
}
Example #11
Source File: utils.ts    From polkadot-watcher-csv-exporter with Apache License 2.0 6 votes vote down vote up
closeFile = (file: WriteStream|ReadStream): Promise<void>=> {
  return new Promise(resolve => {
    file.on("close", resolve);
    file.close();
  });
}
Example #12
Source File: SwaggerController.ts    From adonis5-swagger with MIT License 5 votes vote down vote up
protected getSwaggerSpecFileContent(): ReadStream {
		const filePath = join(this.app.appRoot, this.config.get('swagger.specFilePath'))
		return fs.createReadStream(filePath)
	}
Example #13
Source File: player.ts    From ytdl with MIT License 5 votes vote down vote up
/**
     * Play media given a URL and a stream source
     */
    public abstract play(url: string, stream: ReadStream): unknown;
Example #14
Source File: AudioPlayer.ts    From ytdl with MIT License 5 votes vote down vote up
stream: ReadStream;
Example #15
Source File: update-one-file.node.spec.ts    From js-client with MIT License 5 votes vote down vote up
streamToString = (stream: ReadStream): Promise<string> => {
	const chunks: Array<Buffer> = [];
	return new Promise((resolve, reject) => {
		stream.on('data', (chunk: Buffer) => chunks.push(chunk));
		stream.on('error', reject);
		stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
	});
}
Example #16
Source File: fetch.ts    From open-design-sdk with Apache License 2.0 5 votes vote down vote up
// MultipartFilePart

export async function postMultipart<
  PathPattern extends MethodPathPatterns<'post'>,
  Operation extends paths[PathPattern]['post'],
  MultipartBody extends OperationMultipartBodyParams<Operation>
>(
  apiRoot: string,
  pathPattern: PathPattern,
  pathParams: PathParams<PathPattern>,
  data: {
    [K in Extract<RequiredKeys<MultipartBody>, string>]:
      | MultipartBody[K]
      | ReadStream
  } &
    {
      [K in Extract<OptionalKeys<MultipartBody>, string>]?:
        | MultipartBody[K]
        | ReadStream
    },
  authInfo: AuthInfo,
  options: {
    console?: Console | null
    cancelToken?: CancelToken | null
  } = {}
): Promise<Exclude<OperationResponse<Operation>, { statusCode: 500 }>> {
  const path = populatePathPattern(pathPattern, pathParams)

  const requestBody = new FormData()
  keys(data).forEach((fieldName) => {
    requestBody.append(fieldName, data[fieldName])
  })

  const res = await fetch(`${apiRoot}${path}`, {
    method: 'post',
    body: requestBody,
    headers: {
      'Authorization': `Bearer ${authInfo.token}`,
    },
    console: options.console || console,
    cancelToken: options.cancelToken || null,
  })
  options.cancelToken?.throwIfCancelled()

  const body = await res.json()
  options.cancelToken?.throwIfCancelled()

  if (res.status === 500) {
    throw new Error('Server Error')
  }

  return {
    statusCode: res.status,
    headers: res.headers,
    body,
  } as Exclude<OperationResponse<Operation>, { statusCode: 500 }>
}
Example #17
Source File: open-design-api.ts    From open-design-sdk with Apache License 2.0 5 votes vote down vote up
async importDesignFile(
    designFileStream: ReadStream,
    options: {
      designId?: DesignId | null
      format?: DesignImportFormatEnum
      cancelToken?: CancelToken | null
    } = {}
  ): Promise<ApiDesign> {
    const cancelToken = createCancelToken.race([
      options.cancelToken,
      this._destroyTokenController.token,
    ])

    const res = await this._requestQueue.add(() =>
      options.designId
        ? postMultipart(
            this._apiRoot,
            '/designs/{design_id}/versions/upload',
            { 'design_id': options.designId },
            {
              'file': designFileStream,
              ...(options.format ? { 'format': options.format } : {}),
            },
            this._getAuthInfo(),
            {
              console: this._console,
              cancelToken,
            }
          )
        : postMultipart(
            this._apiRoot,
            '/designs/upload',
            {},
            {
              'file': designFileStream,
              ...(options.format ? { 'format': options.format } : {}),
            },
            this._getAuthInfo(),
            {
              console: this._console,
              cancelToken,
            }
          )
    )

    if (res.statusCode !== 201) {
      this._console.error(
        'OpenDesignApi#importDesignFile()',
        res.statusCode,
        res.body
      )
      throw new OpenDesignApiError(res, 'Cannot import design')
    }

    const design = res.body['design']

    // NOTE: Waits for the design to become fully processed.
    return this.getDesignById(design['id'], {
      designVersionId: design['version_id'],
    })
  }
Example #18
Source File: set-one-resource-content.node.spec.ts    From js-client with MIT License 5 votes vote down vote up
streamToString = (stream: ReadStream): Promise<string> => {
	const chunks: Array<Buffer> = [];
	return new Promise((resolve, reject) => {
		stream.on('data', (chunk: Buffer) => chunks.push(chunk));
		stream.on('error', reject);
		stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
	});
}
Example #19
Source File: upload-file.ts    From mtcute with GNU Lesser General Public License v3.0 4 votes vote down vote up
/**
 * Upload a file to Telegram servers, without actually
 * sending a message anywhere. Useful when an `InputFile` is required.
 *
 * This method is quite low-level, and you should use other
 * methods like {@link sendMedia} that handle this under the hood.
 *
 * @param params  Upload parameters
 * @internal
 */
export async function uploadFile(
    this: TelegramClient,
    params: {
        /**
         * Upload file source.
         *
         * > **Note**: `fs.ReadStream` is a subclass of `stream.Readable` and contains
         * > info about file name, thus you don't need to pass them explicitly.
         */
        file: UploadFileLike

        /**
         * File name for the uploaded file. Is usually inferred from path,
         * but should be provided for files sent as `Buffer` or stream.
         *
         * When file name can't be inferred, it falls back to "unnamed"
         */
        fileName?: string

        /**
         * Total file size. Automatically inferred for Buffer, File and local files.
         *
         * When using with streams, if `fileSize` is not passed, the entire file is
         * first loaded into memory to determine file size, and used as a Buffer later.
         * This might be a major performance bottleneck, so be sure to provide file size
         * when using streams and file size is known (which often is the case).
         */
        fileSize?: number

        /**
         * File MIME type. By default is automatically inferred from magic number
         * If MIME can't be inferred, it defaults to `application/octet-stream`
         */
        fileMime?: string

        /**
         * Upload part size (in KB).
         *
         * By default, automatically selected by file size.
         * Must not be bigger than 512 and must not be a fraction.
         */
        partSize?: number

        /**
         * Function that will be called after some part has been uploaded.
         *
         * @param uploaded  Number of bytes already uploaded
         * @param total  Total file size
         */
        progressCallback?: (uploaded: number, total: number) => void
    }
): Promise<UploadedFile> {
    // normalize params
    let file = params.file
    let fileSize = -1 // unknown
    let fileName = 'unnamed'
    let fileMime = params.fileMime

    if (Buffer.isBuffer(file)) {
        fileSize = file.length
        file = bufferToStream(file)
    }

    if (typeof File !== 'undefined' && file instanceof File) {
        fileName = file.name
        fileSize = file.size
        // file is now ReadableStream
        file = file.stream()
    }

    if (typeof file === 'string') {
        if (!fs)
            throw new MtArgumentError(
                'Local paths are only supported for NodeJS!'
            )
        file = fs.createReadStream(file)
    }

    if (fs && file instanceof fs.ReadStream) {
        fileName = path.basename((file as ReadStream).path.toString())
        fileSize = await new Promise((res, rej) => {
            fs.stat(
                (file as ReadStream).path.toString(),
                (err?: any, stat?: any) => {
                    if (err) rej(err)
                    res(stat.size)
                }
            )
        })
        // fs.ReadStream is a subclass of Readable, no conversion needed
    }

    if (
        typeof ReadableStream !== 'undefined' &&
        file instanceof ReadableStream
    ) {
        file = convertWebStreamToNodeReadable(file)
    }

    if (typeof file === 'object' && 'headers' in file && 'body' in file && 'url' in file) {
        // fetch() response
        const length = parseInt(file.headers.get('content-length') || '0')
        if (!isNaN(length) && length) fileSize = length

        fileMime = file.headers.get('content-type')?.split(';')[0]

        const disposition = file.headers.get('content-disposition')
        if (disposition) {
            const idx = disposition.indexOf('filename=')

            if (idx > -1) {
                const raw = disposition.slice(idx + 9).split(';')[0]
                fileName = JSON.parse(raw)
            }
        }

        if (fileName === 'unnamed') {
            // try to infer from url
            const url = new URL(file.url)
            const name = url.pathname.split('/').pop()
            if (name && name.indexOf('.') > -1) {
                fileName = name
            }
        }

        if (!file.body)
            throw new MtArgumentError('Fetch response contains `null` body')

        if (
            typeof ReadableStream !== 'undefined' &&
            file.body instanceof ReadableStream
        ) {
            file = convertWebStreamToNodeReadable(file.body)
        } else {
            file = file.body
        }
    }

    // override file name and mime (if any)
    if (params.fileName) fileName = params.fileName

    // set file size if not automatically inferred
    if (fileSize === -1 && params.fileSize) fileSize = params.fileSize
    if (fileSize === -1) {
        // load the entire stream into memory
        const buffer = await readStreamUntilEnd(file as Readable)
        fileSize = buffer.length
        file = bufferToStream(buffer)
    }

    if (!(file instanceof Readable))
        throw new MtArgumentError(
            'Could not convert input `file` to stream!'
        )

    const partSizeKb = params.partSize ?? determinePartSize(fileSize)
    if (partSizeKb > 512)
        throw new MtArgumentError(`Invalid part size: ${partSizeKb}KB`)
    const partSize = partSizeKb * 1024

    const isBig = fileSize > 10485760 // 10 MB
    const hash = this._crypto.createMd5()

    const partCount = ~~((fileSize + partSize - 1) / partSize)
    this.log.debug(
        'uploading %d bytes file in %d chunks, each %d bytes',
        fileSize,
        partCount,
        partSize
    )

    // why is the file id generated by the client?
    // isn't the server supposed to generate it and handle collisions?
    const fileId = randomLong()
    let pos = 0

    for (let idx = 0; idx < partCount; idx++) {
        const part = await readBytesFromStream(file, partSize)
        if (!part)
            throw new MtArgumentError(
                `Unexpected EOS (there were only ${idx} parts, but expected ${partCount})`
            )

        if (!Buffer.isBuffer(part))
            throw new MtArgumentError(`Part ${idx} was not a Buffer!`)
        if (part.length > partSize)
            throw new MtArgumentError(
                `Part ${idx} had invalid size (expected ${partSize}, got ${part.length})`
            )

        if (idx === 0 && fileMime === undefined) {
            const fileType = await fromBuffer(part)
            fileMime = fileType?.mime
            if (!fileMime) {
                // either plain text or random binary gibberish
                // make an assumption based on the first 8 bytes
                // if all 8 bytes are printable ASCII characters,
                // the entire file is probably plain text
                const isPlainText = isProbablyPlainText(part.slice(0, 8))
                fileMime = isPlainText
                    ? 'text/plain'
                    : 'application/octet-stream'
            }
        }

        if (!isBig) {
            // why md5 only small files?
            // big files have more chance of corruption, but whatever
            // also isn't integrity guaranteed by mtproto?
            await hash.update(part)
        }

        pos += part.length

        // why
        const request = isBig
            ? ({
                  _: 'upload.saveBigFilePart',
                  fileId,
                  filePart: idx,
                  fileTotalParts: partCount,
                  bytes: part,
              } as tl.upload.RawSaveBigFilePartRequest)
            : ({
                  _: 'upload.saveFilePart',
                  fileId,
                  filePart: idx,
                  bytes: part,
              } as tl.upload.RawSaveFilePartRequest)

        const result = await this.call(request)
        if (!result) throw new Error(`Failed to upload part ${idx}`)

        params.progressCallback?.(pos, fileSize)
    }

    let inputFile: tl.TypeInputFile
    if (isBig) {
        inputFile = {
            _: 'inputFileBig',
            id: fileId,
            parts: partCount,
            name: fileName,
        }
    } else {
        inputFile = {
            _: 'inputFile',
            id: fileId,
            parts: partCount,
            name: fileName,
            md5Checksum: (await hash.digest()).toString('hex'),
        }
    }

    if (fileMime! in OVERRIDE_MIME) fileMime = OVERRIDE_MIME[fileMime!]

    return {
        inputFile,
        size: fileSize,
        mime: fileMime!,
    }
}