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 🙏

© 2024 – Pkg Stats / Ryan Hefner

file-blob-converter

v2.0.0

Published

Module to convert files to blob objects to be stored in DB and back again.

Downloads

107

Readme

FileBlobConverter

Project offers a clean and concise way to convert files to blobs for databases storage. The project will grant the ability to convert excel, word documents, images, etc into blob files for storage. It will also offer a method for the user convert those blobs back. The blob can be converted back, but if not converted back into it's file type, could cause potential issues.

Usage

The module can be used in multiple ways. The primary function is to convert files to and from binaryBlob ojects for storage. There are several models and interfaces, but only two public methods on a single service class.

Installation


npm i file-blob-converter --save

Models


IFile as per Browser File Object

export default interface IFile
{
    arrayBuffer: ArrayBuffer;
    lastModifiedDate: Date;
    name: string;
    size: number;
    type: string;
    webkitRelativePath: string;
}

IFileReader as per Browser File Reader

export default interface IFileReader
{
    error: any;
    onerror: any;
    onload: any;
    readyState: number;
    result: any;
    abort(): void;
    readAsArrayBuffer(file: IFile): void;
    readAsBinaryString(file: IFile): void;
    readAsText(file: IFile): void;
}

IBlob as per the Browser Blob

export default interface IBlob
{
    size: number;
    type: string;
    arrayBuffer(): ArrayBuffer;
    slice(): IBlob;
    stream(): any;
    text(): any;
}

IBlobFile (specific to this module)

export default interface IBlobFile
{
    BinaryString: string;
    FileType: string;
    Name: string;
    Size: number;
}

File Converter


This is the core repsonsibility of the module. The purpose is so that we can use this to convert files. Below is the interface, then an example of how to use the abstract class.

export default interface IFileConverter
{
    ConvertBlobToFile(blob: IBlobFile): IFile;
    ConvertFileToBlobFile(file: IFile): Promise<IBlobFile>;
}

Using the FileConverter

All examples are written in typescript

First, implement a concrete version of the abstract class.

import FileConverter from "fileblobconverter"

export default class Converter extends FileConverter
{
    private readonly _util: IUtil; //user implemented

    public constructor(util: IUtil)
    {
        super();
        _util = util;
    }

    // user implemented method
    public DoUtilThing()
    {
        // do user implemented things.
    }
}

To convert a file to a binary blob

import Converter from "../users/concrete/class/path";

// function to handle file event
public File(e: event)
{
    const c: IFileConverter = new Converter();
    const b: IBlobFile = c.ConvertFileToBlobFile(e.target.file[0]);
    axios.post("/crud/api/path", b)
        .then((data: any) => data)
        .catch((err) => logger.error(`Message: ${err}`));
}

To convert a BlobFile to a file

import Converter from "../users/concrete/class/path";

// function to download file from DB
public DownloadFile(id: number)
{
    const fileFromDb: IBlobFile = axios.get(`/crud/api/path/${id}`)
        .then((data: IBlobFile) => data)
        .catch((err) => logger.error(`Message: ${err}`));
    const c: IFileConverter = new Converter();
    const urlObj = new Blob(
        [c.ConvertBlobToFile(fileFromDb).arrayBuffer], 
        {type: c.ConvertBlobToFile(fileFromDb).type}
    );
    window.open(urlObj);
}

Change Log


  • 10-23-2020 | Abstracted FileReader away from the user, still passible on constructor.

Contribution Guide


Please refeer to the styles in the tslint file within the project. The linter, if using one that supports typescript, will tell you what the rules from tslint area. Outside of that follow the below.

  • Check the existing issues and checkout one of those first
  • If the issue doesn't exist, file an issue
  • Fork the repo and start working on it
  • Ensure that you have at least 65% code coverage
  • Create pull request