1.1.1 • Published 6 years ago

upload-checker v1.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

UploadChecker

Check and constrain type/size/resolution while uploading files in pure front-end way.

Test status Coverage

Demo

You can view the live demo here.

Browsers Supporting

Upload checker support all browsers that support Blob URLs, you can check them here:

http://caniuse.com/#feat=bloburls

Installation

npm install upload-checker

Data structures

If using typescript, you can check the 'src/types.ts' to view and use those in your code in this way:

import {TFiles, IFileInfo} from 'upload-checker';

Following tables show all types and interfaces:

Types

NameValueDescription
TFileFileA HTML file element.
TFileTypestringType of file, like 'image/png'.
TFileTypesTFileType[]Array of TFile.
TImageConstraintKey'maxBytesPerPixel' | 'maxSize' | 'maxWidth'All constraints for image files.
TVideoConstraintKey'maxBytesPerPixelPerSecond' | 'maxSize' | 'maxWidth' | 'maxDuration'All constraints for video files.
TError'type' | 'width' | 'size' | 'duration' | 'bytes' | 'unknown'Error types.

IFileInfo

NameRequiredTypeDescription
TypeTFileTypeType of file.
widthxnumberWidth of image or video.
heightxnumberHeight of image or video.
sizexnumberSize (width x height) of image or video.
durationxnumberDuration of video.

ICheckError

NameRequiredTypeDescription
nameTErrorType of error.
currentValuenumber | string[] | stringCurrent value of wrong constraint.
limitValuenumber | string[] | stringMax value of wrong constraint.
stackstringError stack.
messagestringError message.

ICheckResponse

NameRequiredTypeDescription
fileTFileCurrent file.
infoIFileInfoInfo of file.
errorxICheckErrorError of checking if something wrong.

API

Upload checker provides rich api for different requirements. You can import theme in two ways:

import {checkType} from 'upload-checker';

or

import {checkType} from 'upload-checker/dist/TypeChecker';

In the second way, only module TypeChecker will be packed to your source file.

checkType

(file: TFile,types: TFileTypes) => Promise\<ICheckResponse>

In module upload-checker/TypeChecker.

checkType(file, types)
.then(res => {......})
.catch(res => {......});

TypeChecker

An class for storing types' constraints then could be reused with check method.

In module upload-checker/TypeChecker.

MethodTypeDescription
constructor(types: TFileTypes = []) => voidConstructor function, if types is empty, all file types will be allowed.
setTypes(types: TFileTypes) => voidSet types of checker.
check(file: TFile) => Promise\<ICheckResponse>Check file with current types.
const checker = new TypeChecker(['image/png']);
checker.setTypes(['image/jpeg']);
checker.check(file)
.then(res => {......})
.catch(res => {......});

checkImage

(file: TFile, maxBytesPerPixel: number, maxSize: number, maxWidth?: number) => Promise

In module upload-checker/ImageChecker.

checkImage(file, maxBytesPerPixel, maxSize, maxWidth)
.then(res => {......})
.catch(res => {......});

ImageChecker

An class for storing image's constraints then could be reused with check method.

In module upload-checker/ImageChecker.

MethodTypeDescription
constructor(maxBytesPerPixel: number, maxSize: number, maxWidth?: number) => voidConstructor function, if maxattr is 0, checker will not check that.
setAttr(key: TImageConstraintKey, value: number) => voidSet attr of checker.
check(file: TFile) => Promise\<ICheckResponse>Check file with current constraint.
const checker = new ImageChecker(.5, 1280 * 720);
checker.setAttr('maxWidth', 1280);
checker.check(file)
.then(res => {......})
.catch(res => {......});

checkVideo

(file: TFile, maxBytesPerPixelPerSecond: number, maxDuration: number, maxSize: number, maxWidth?: number) => Promise\<ICheckResponse>

In module upload-checker/VideoChecker.

checkVideo(file, maxBytesPerPixelPerSecond, maxDuration, maxSize, maxWidth)
.then(res => {......})
.catch(res => {......});

VideoChecker

An class for storing video's constraints then could be reused with check method.

In module upload-checker/VideoChecker.

MethodTypeDescription
constructor(maxBytesPerPixelPerSecond: number, maxDuration: number, maxSize: number, maxWidth?: number) => voidConstructor function, if maxattr is 0, checker will not check that.
setTypes(types: TFileTypes) => voidSet attr of checker.
check(file: TFile) => Promise\<ICheckResponse>Check file with current constraint.
const checker = new VideoChecker(.5, 10, 1280 * 720);
checker.setAttr('maxWidth', 1280);
checker.check(file)
.then(res => {......})
.catch(res => {......});

UploadChecker

A react component for better usage.

Props

NameTypeDescription
TypesIFileTypesSame as parameters of constructor of TypeChecker.
multiplebooleanCould user select multiple files.
onDrop(res: ICheckResponse) => voidA callback will be called after file is checked.
imageConstraintSame as parameters of constructor of ImageChecker.Constraints for image files.
videoConstraintSame as parameters of constructor of VideoChecker.Constraints for video files.
childrenJSX.Element | stringChildren element.
classNamestringClassName for root element.
styleanyStyle for root element.

All others props will be passed to input.

Contribute

Development

Run:

npm run dev

then open localhost:4444.

Unit test

Run:

npm run unittest

then find reports in the report folder.

Build

Run:

npm run build

License

Copyright © 2017, 戴天宇, Tianyu Dai (dtysky < dtysky@outlook.com >). All Rights Reserved. This project is free software and released under the MIT License.