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

url-to-coze

v1.0.0

Published

将 URL 文件转存到 Coze 平台并获取新链接 - Transfer files from any URL to Coze platform

Readme

URL 文件转存到 Coze

将任意 URL 的文件(图片、文档等)转存到 Coze 平台并获取新的链接。

功能特点

  • ✅ 支持图片、文档等各种文件类型
  • ✅ 自动从 URL 下载文件
  • ✅ 完整的错误处理
  • ✅ 进度回调支持
  • ✅ 支持 Node.js 和浏览器环境
  • ✅ Promise 和 async/await 支持

安装

NPM 安装(推荐)

npm install url-to-coze

Yarn 安装

yarn add url-to-coze

要求

  • Node.js >= 18.0.0 (内置 fetch API)
  • 如果使用 Node.js < 18,需要 polyfill fetchFormData

快速开始

Node.js 环境

const { urlToCoze } = require('./urlToCoze');

// 基本使用
const newUrl = await urlToCoze('https://example.com/image.jpg');
console.log('新链接:', newUrl);

浏览器环境

<script src="urlToCoze.js"></script>
<script>
  urlToCoze('https://example.com/image.jpg')
    .then(newUrl => {
      console.log('新链接:', newUrl);
    })
    .catch(error => {
      console.error('转存失败:', error);
    });
</script>

API 文档

urlToCoze(url, options)

将 URL 文件转存到 Coze 平台并获取新链接。

参数:

  • url (string): 源文件的 URL 链接
  • options (Object, 可选):
    • onProgress (function): 进度回调函数

返回值:

  • Promise<string>: 返回新的文件链接

示例:

// 基本使用
const newUrl = await urlToCoze('https://example.com/image.jpg');

// 带进度回调
const newUrl = await urlToCoze('https://example.com/image.jpg', {
  onProgress: (status) => {
    console.log('进度:', status);
  }
});

使用示例

示例 1: 转存单个图片

const { urlToCoze } = require('./urlToCoze');

async function transferImage() {
  const imageUrl = 'https://example.com/photo.jpg';
  
  try {
    const newUrl = await urlToCoze(imageUrl);
    console.log('原始链接:', imageUrl);
    console.log('新链接:', newUrl);
  } catch (error) {
    console.error('转存失败:', error.message);
  }
}

transferImage();

示例 2: 带进度显示

async function transferWithProgress() {
  const fileUrl = 'https://example.com/document.pdf';
  
  const newUrl = await urlToCoze(fileUrl, {
    onProgress: (status) => {
      console.log('📝', status);
    }
  });
  
  console.log('✅ 转存成功!');
  console.log('新链接:', newUrl);
}

示例 3: 批量转存

async function batchTransfer() {
  const urls = [
    'https://example.com/image1.jpg',
    'https://example.com/image2.png',
    'https://example.com/document.pdf'
  ];
  
  const results = await Promise.all(
    urls.map(url => urlToCoze(url))
  );
  
  results.forEach((newUrl, index) => {
    console.log(`${urls[index]} -> ${newUrl}`);
  });
}

示例 4: 错误处理

async function transferWithErrorHandling() {
  const url = 'https://example.com/file.zip';
  
  try {
    const newUrl = await urlToCoze(url, {
      onProgress: (status) => console.log('[进度]', status)
    });
    
    console.log('[成功] 新链接:', newUrl);
    return newUrl;
    
  } catch (error) {
    console.error('[错误] 转存失败:');
    console.error('  错误信息:', error.message);
    
    // 可以添加重试逻辑
    if (error.message.includes('网络')) {
      console.log('正在重试...');
      return await urlToCoze(url);
    }
    
    throw error;
  }
}

工作流程

  1. 获取上传 Token - 从服务器获取上传凭证
  2. 下载文件 - 从源 URL 下载文件内容
  3. 上传文件 - 将文件上传到 Coze 平台
  4. 获取新链接 - 通过 Workflow 获取文件的新链接
URL → 下载 → 获取Token → 上传 → 获取新链接

支持的文件类型

理论上支持所有文件类型,包括但不限于:

  • 图片: .jpg, .png, .gif, .webp, .svg
  • 文档: .pdf, .doc, .docx, .txt
  • 压缩包: .zip, .rar, .7z
  • 视频: .mp4, .avi, .mov
  • 音频: .mp3, .wav, .flac

错误处理

所有函数都会抛出错误,建议使用 try-catch 捕获:

try {
  const newUrl = await urlToCoze(url);
} catch (error) {
  console.error('转存失败:', error.message);
  // 处理错误
}

常见错误:

  • 获取 Token 失败 - 服务器无法返回有效的上传凭证
  • 下载文件失败 - 源 URL 无法访问或下载
  • 上传文件失败 - 文件上传到 Coze 失败
  • 获取文件链接失败 - Workflow 执行失败

注意事项

  1. 确保源 URL 可以正常访问
  2. 文件大小限制取决于 Coze 平台的限制
  3. 建议添加重试机制处理网络问题
  4. 新链接可能有过期时间,请及时保存使用

完整示例

查看 example.js 文件获取更多使用示例。

许可证

MIT