flipped-aurora/gin-vue-admin · warning

key不能为空

Error message

key不能为空

What it means

This validation error is returned by the internal localPath helper, used by DeleteFile and Exists, when the provided key is the empty string. The library refuses to resolve an empty key because it would otherwise operate on the store directory itself rather than a concrete file.

Source

Thrown at server/utils/upload/local.go:110

	}

	// 使用文件锁防止并发删除
	mu.Lock()
	defer mu.Unlock()

	err = os.Remove(p)
	if err != nil {
		return errors.New("文件删除失败: " + err.Error())
	}

	return nil
}

// localPath 校验 key 并拼接出本地存储的绝对路径,复用 DeleteFile 中的路径穿越防护逻辑。
func (*Local) localPath(ctx context.Context, key string) (string, error) {
	// 检查 key 是否为空
	if key == "" {
		return "", errors.New("key不能为空")
	}

	// 验证 key 是否包含非法字符或尝试访问存储路径之外的文件
	if strings.Contains(key, "..") || strings.ContainsAny(key, `\/:*?"<>|`) {
		return "", errors.New("非法的key")
	}

	return filepath.Join(global.GVA_CONFIG.Local.StorePath, key), nil
}

// Exists 检查本地文件是否存在,"不存在"统一降级为 (false, nil)。
func (l *Local) Exists(ctx context.Context, key string) (bool, error) {
	p, err := l.localPath(ctx, key)
	if err != nil {
		return false, err
	}

	info, err := os.Stat(p)

View on GitHub (pinned to 3136500ef3)

Solutions

  1. Validate/skip empty keys in the caller before invoking DeleteFile/Exists (trim whitespace too).
  2. Clean up DB records holding empty keys so they never reach the delete path.
  3. In batch DeleteFiles, filter the key slice first: keys where strings.TrimSpace(k) != "".

Example fix

// before
err := local.DeleteFile(ctx, record.Key) // Key may be ""
// after
if strings.TrimSpace(record.Key) == "" {
    return nil // nothing to delete
}
err := local.DeleteFile(ctx, record.Key)
Defensive patterns

Strategy: validation

Validate before calling

func safeKey(key string) (string, bool) {
    key = strings.TrimSpace(key)
    return key, key != ""
}
// usage: k, ok := safeKey(record.Key); if !ok { skip }

Type guard

func validKey(key string) bool {
    return strings.TrimSpace(key) != ""
}

Try / catch

err := local.DeleteFile(ctx, key)
if err != nil && err.Error() == "key不能为空" {
    return fmt.Errorf("record %d has no file key; nothing to delete", record.ID)
}

Prevention

When it happens

Trigger: Calling DeleteFile or Exists with key="", typically from a DB record with an empty/NULL file key, a form field that wasn't filled, or a caller that didn't check the key before invoking.

Common situations: Legacy rows in the upload table created before keys were mandatory; frontend sending empty strings for optional file fields; batch delete lists containing blank entries.

Related errors


AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31). Data as JSON: /api/errors/44349d81eb72b023. Report an issue: GitHub.