strapi>>>面对“ enametoolong:名字太长”错误

发布于 2025-02-11 01:05:50 字数 395 浏览 2 评论 0 原文

系统信息

  • Strapi版本:4.1.12
  • 操作系统:MACOS
  • 数据库:Postgres
  • 节点版本:16.15.1
  • NPM版本:8.11.0
  • 纱线版本:1.22.11

当我尝试上传带有长名的文件时,我会在错误的位置上添加到以下错误:

错误:Enametoolong:名称太长

文件名称: lorem ipsum只是打印和排版行业的虚拟文本。自1500年代以来,Lorem Ipsum一直是该行业的标准虚拟文本,当时一家未知的打印机戴上了类型的厨房,并在此处制作一本类型的标本书A.pdf

有人可以帮助我解决这个根本原因错误?

System information

  • Strapi Version: 4.1.12
  • Operating System: MacOS
  • Database: Postgres
  • Node Version: 16.15.1
  • NPM Version: 8.11.0
  • Yarn Version: 1.22.11

When I try to upload a file with long name then I’m getting below error:

error: ENAMETOOLONG: name too long

File name: Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry’s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book here a.pdf

Can someone help me with the root cause of this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2025-02-18 01:05:50

是您的文件系统。您的示例文件名看起来恰好255个字节,但是您在其中有“奇特的报价”。它不是撇号(0x27)或backtick(0x60),而是三个字节, 0x2e 0x80 0x99 。错误是完全正确的:名称太长

您可以检查此列表,搜索字符 u+2019 ,您会看到此字节序列与您的报价字符匹配。


JavaScript的字符串函数,例如''。subStr() targue 和不是 bytes 上工作,因此,请简单地使用 filename.substr( 0,255)将不起作用。

最好的方法是使用知道如何在不打破特殊字符序列的情况下修剪UTF-8字符串的外部软件包,例如多字节报价或表情符号。

const truncate = require('truncate-utf8-bytes');
const { extname, basename } = require('path');

function trimFilenameToBytes(filename, maxBytes = 255) {
  // By extracting the file extension from the filename,
  // it'll trim "verylong.pdf" to "verylo.pdf" and not "verylong.p"
  const ext = extname(filename);
  const base = basename(filename, ext);
  const length = Buffer.byteLength(ext);
  const shorter = truncate(base, Math.max(0, maxBytes - length)) + ext;
  // Just in case the file extension's length is more than maxBytes.
  return truncate(shorter, maxBytes);
}

const filename = 'Lorem Ipsum is simply dummy \
text of the printing and typesetting industry. Lorem Ipsum has \
been the industry’s standard dummy text ever since the 1500s, \
when an unknown printer took a galley of type and scrambled it \
to make a type specimen book here a.pdf';

console.log(
  'This string is',
  filename.length,
  'characters and',
  Buffer.byteLength(filename, 'utf-8'),
  'bytes'
);

console.log(trimFilenameToBytes(filename));

// Will log the following, note how it's 2 bytes shorter:
// This string is 255 characters and 257 bytes
// Lorem Ipsum is simply dummy \
// text of the printing and typesetting industry. Lorem Ipsum has \
// been the industry’s standard dummy text ever since the 1500s, \
// when an unknown printer took a galley of type and scrambled it \
// to make a type specimen book here.pdf

It's your file system. Your example filename looks exactly 255 bytes, but you have a "fancy quote" in there. It's not an apostrophe (0x27) or backtick (0x60) but three bytes, 0x2e 0x80 0x99. The error is entirely correct: name too long.

You can check this list, search for character U+2019 and you'll see this sequence of bytes matches your quote character.


JavaScript's string functions, such as ''.substr() work on characters and not bytes, so simply using filename.substr(0, 255) will not work.

The best way is to use an external package that knows how to trim UTF-8 strings without breaking special character sequences, like your multi-byte quote or emoji.

const truncate = require('truncate-utf8-bytes');
const { extname, basename } = require('path');

function trimFilenameToBytes(filename, maxBytes = 255) {
  // By extracting the file extension from the filename,
  // it'll trim "verylong.pdf" to "verylo.pdf" and not "verylong.p"
  const ext = extname(filename);
  const base = basename(filename, ext);
  const length = Buffer.byteLength(ext);
  const shorter = truncate(base, Math.max(0, maxBytes - length)) + ext;
  // Just in case the file extension's length is more than maxBytes.
  return truncate(shorter, maxBytes);
}

const filename = 'Lorem Ipsum is simply dummy \
text of the printing and typesetting industry. Lorem Ipsum has \
been the industry’s standard dummy text ever since the 1500s, \
when an unknown printer took a galley of type and scrambled it \
to make a type specimen book here a.pdf';

console.log(
  'This string is',
  filename.length,
  'characters and',
  Buffer.byteLength(filename, 'utf-8'),
  'bytes'
);

console.log(trimFilenameToBytes(filename));

// Will log the following, note how it's 2 bytes shorter:
// This string is 255 characters and 257 bytes
// Lorem Ipsum is simply dummy \
// text of the printing and typesetting industry. Lorem Ipsum has \
// been the industry’s standard dummy text ever since the 1500s, \
// when an unknown printer took a galley of type and scrambled it \
// to make a type specimen book here.pdf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文