定制 nassiry/filesize-handler 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nassiry/filesize-handler

Composer 安装命令:

composer require nassiry/filesize-handler

包简介

A PHP package for handling file size with binary, decimal & locale units support.

README 文档

README

PHP FileSizeHandler

Packagist Downloads Packagist Version PHP License

The FileSizeHandler class is a utility designed to fetch and format file sizes across various file storage systems. It offers cross-platform compatibility with support for both SI (decimal) and IEC (binary) standards, making it HDD-agnostic and highly flexible.

This package addresses the discrepancies in file size measurement (binary vs decimal units) and provides flexible APIs for developers.

Features

  1. Supports Local, Remote, and FTP Files:
    • Easily calculate file sizes from multiple sources.
  2. Binary and Decimal Calculations:
    • Switch between binary (KiB, MiB, etc.) and decimal (KB, MB, etc.) bases with simple methods.
  3. Fluent Interface:
    • Intuitive method chaining for base selection and size formatting.
  4. Extensibility:
    • Ready for integration with cloud storage systems like S3 and Google Cloud.
  5. i18n Support:
    • Format file size with custom localized units.
    • Dynamically provide translations for units.
  6. Chaining and Echo Support:
    • Supports method chaining for cleaner code.
    • Directly echo the handler instance to get the formatted size.
  7. Custom Source Support:
    • Implement FileSourceInterface and register your custom source.

Table Of Contents

  1. Why Was This Class Created?
  2. Useful Links
  3. Requirements
  4. Installation
  5. Usage
  6. Binary vs Decimal Units
  7. API Reference
  8. Extensions
  9. Contributing
  10. License

Why Was This Class Created?

When working with file sizes, differences in measurement units across systems can cause confusion:

  • Cross-OS Consistency: File size interpretations vary between operating systems (Windows, Linux, macOS) and hardware (HDD/SSD manufacturers often use decimal, while OS file systems may use binary).
  • Unified Interface: Simplifies handling file size retrieval from diverse sources.
  • Extensibility: Easily extend functionality by integrating additional storage systems via a simple, well-defined interface.

Useful Links

  1. Wikipedia: Binary Prefixes
    • Comprehensive explanation of binary (KiB, MiB) vs. decimal (KB, MB) prefixes.
  2. NIST: Prefixes for Binary Multiples
    • Official standards for binary-based units.
  3. IBM: Units of Measurement for Storage Data
    • Insights into storage measurement across platforms.
  4. Google: Byte Units
    • Guidelines for using decimal and binary units consistently.
  5. Metric View: What are binary prefixes?
    • Provides an overview of binary prefixes and their usage

Requirements

  • PHP: Version 8.0 or higher
  • PHP Extension - ext-intl: Required

Installation

The recommended way use Composer to install the package:

composer require nassiry/filesize-handler

Usage

Local File

By default, the library supports fetching file sizes for local files.

use Nassiry\FileSizeUtility\FileSizeHandler;

$handler = FileSizeHandler::create()
    ->local('/path/to/file')
    ->binary();

// Get the formatted size
echo $handler->format(); // Example output: "1.23 MiB"

// Or use directly with echo
echo FileSizeHandler::create()
    ->local('/path/to/file')
    ->binary()
    ->format();

Extending the Library

To add support for a new file source, implement the FileSourceInterface:

use Nassiry\FileSizeUtility\Sources\FileSourceInterface;

class CustomCloudSource implements FileSourceInterface
{
    public function __construct(private string $apiKey, private string $filePath) {}

    public function getSizeInBytes(): int
    {
        // Implement API logic to get file size
        return 123456789;
    }
}

$customSource = new CustomCloudSource('api-key', '/path/to/file');

// Once implemented, register your custom source using:
$handler = FileSizeHandler::create()
    ->from($customSource)
    ->binary();
    
echo $handler->format(); // Example output: "1.23 MiB"

For more information on extending the library, check the official extensions available.

Binary vs Decimal Units

Switching Units

$handler = FileSizeHandler::create()
    ->local('/path/to/file.txt');

// Default: Binary (1024-based)
echo $handler->format(); // Output: "1.23 MiB"

// Switch to Decimal (1000-based)
echo $handler->decimal()->format(); // Output: "1.30 MB"

Dynamic Locale and Unit Customization

$customUnits = [
    'binary_units' => ['o', 'Kio', 'Mio', 'Gio', 'Tio', 'Pio', 'Eio', 'Zio', 'Yio'],
    'decimal_units' => ['o', 'Ko', 'Mo', 'Go', 'To', 'Po', 'Eo', 'Zo', 'Yo'],
];

$handler = FileSizeHandler::create('fr_FR', $customUnits)
    ->local('/path/to/file')
    ->binary();

echo $handler->format(1); // Example output: "1,2 Mio"

Key Points:

Two-letter country codes are based on the ISO 3166-1 alpha-2 standard and are used in locale naming conventions.

  • Examples of Supported Locales: en_US, fr_FR, de_DE.
  • The Default: units and locale are set to en_US

API Reference

FileSizeHandler::create(string $locale = 'en_US', array $units = [])

Initializes a new FileSizeHandler instance.

Source Methods

  • local(string $filePath): self
    Creates an instance for a local file.

  • from(FileSourceInterface $source): self
    Allows integration of a custom file source by implementing the FileSourceInterface.

Configuration Methods

  • binary(): self
    Switches unit calculation to binary (1024-based).

  • decimal(): self
    Switches unit calculation to decimal (1000-based).

Output Methods

  • bytes(): int
    Returns the file size in bytes.

  • format(int $precision = 2): string
    Returns the formatted file size with the specified precision.

Extensions

FileSizeHandler is extensible via optional extensions for advanced integrations. These extensions add support for remote and cloud-based file sources. Install the extensions via Composer.

Available Extensions:

Extension Description Installation Command Documentation
FTP Support for FTP file size retrieval. composer require nassiry/filesize-handler-ftp-extension See Full Documentation
Amazon S3 Fetch file sizes from Amazon S3 buckets. composer require nassiry/filesize-handler-s3-extension See Full Documentation
Google Cloud Retrieve file sizes from Google Cloud Storage. composer require nassiry/filesize-handler-googlecloud-extension See Full Documentation
Remote Support for files accessible via HTTP/HTTPS. composer require nassiry/filesize-handler-remote-extension See Full Documentation

Contributing

Feel free to submit issues or pull requests to improve the package. Contributions are welcome!

Changelog

See CHANGELOG for release details.

⚠️ Deprecated Methods

As of version 1.1.0, the following methods are deprecated and will be removed in version 2.0.0:

Deprecated Use Instead
baseBinary() binary()
baseDecimal() decimal()
formattedSize() format()
sizeInBytes() bytes()

License

This package is open-source software licensed under the MIT license.

nassiry/filesize-handler 适用场景与选型建议

nassiry/filesize-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 01 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「file-size」 「php-file-zie」 「php-size-binary」 「php-size-decimal」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nassiry/filesize-handler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nassiry/filesize-handler 我们能提供哪些服务?
定制开发 / 二次开发

基于 nassiry/filesize-handler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 36
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 22
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-07