npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

yy-file-upload

v1.0.0

Published

文件上传工具,支持预签名URL上传

Readme

yy-file-upload

文件上传工具,支持预签名URL上传。

安装

npm install yy-file-upload

使用

基础用法

import { uploadFile, getFileType, formatFileSize } from 'yy-file-upload';

// 上传文件
const result = await uploadFile({
  file: fileObject,
  getUploadInfo: async (params) => {
    // 调用后端API获取上传信息
    const response = await fetch('/api/upload/info', {
      method: 'POST',
      body: JSON.stringify(params)
    });
    return await response.json();
  },
  folder: 'knowledge',
  onProgress: (percent) => {
    console.log(`上传进度: ${percent}%`);
  },
  onStatusUpdate: async ({ object_key, status }) => {
    // 更新上传状态
    await fetch('/api/upload/status', {
      method: 'POST',
      body: JSON.stringify({ object_key, status })
    });
  }
});

if (result.success) {
  console.log('上传成功:', result.download_url);
}

直接上传到预签名URL

import { uploadToPresignedUrl } from 'yy-file-upload';

const result = await uploadToPresignedUrl({
  file: fileObject,
  uploadUrl: 'https://example.com/upload',
  fields: {
    'Content-Type': 'image/png'
  },
  onProgress: (percent) => {
    console.log(`上传进度: ${percent}%`);
  }
});

文件类型检查

import { getFileType, formatFileSize } from 'yy-file-upload';

const fileInfo = getFileType(file);
console.log(fileInfo.type); // 'image', 'audio', 'video', 'pdf', 'document', 'file'
console.log(fileInfo.isImage); // true/false

const size = formatFileSize(file.size);
console.log(size); // '1.5 MB'

API

uploadFile(options)

通用文件上传函数。

参数:

  • options.file (File) - 文件对象
  • options.getUploadInfo (Function) - 获取上传信息的函数
  • options.folder (string) - 文件夹名称,默认 'knowledge'
  • options.expiryType (number) - 有效期类型,默认 1
  • options.onProgress (Function) - 进度回调
  • options.onStatusUpdate (Function) - 状态更新回调

返回值:

  • Promise<Object> - 上传结果

uploadToPresignedUrl(options)

上传文件到预签名URL。

参数:

  • options.file (File) - 文件对象
  • options.uploadUrl (string) - 预签名上传URL
  • options.fields (Object) - 表单字段(可选)
  • options.onProgress (Function) - 进度回调(可选)

返回值:

  • Promise<Object> - 上传结果

getFileType(file)

检查文件类型。

参数:

  • file (File|string) - 文件对象或文件名

返回值:

  • Object - 文件类型信息

formatFileSize(bytes, decimals)

格式化文件大小。

参数:

  • bytes (number) - 字节数
  • decimals (number) - 小数位数,默认 2

返回值:

  • string - 格式化后的文件大小

特性

  • ✅ 支持预签名URL上传
  • ✅ 上传进度回调
  • ✅ 文件类型自动识别
  • ✅ 文件大小格式化
  • ✅ 状态更新支持
  • ✅ TypeScript 支持