filetype

package module
v0.0.0-...-e51e05c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 6, 2020 License: MIT Imports: 1 Imported by: 0

README

filetype

golang package for file type checking and just need the file path param, also this package only gets minimized file content for checking logic, which is friendly to big size file.

Examples

Simple file type check
package main

import (
    "fmt"

    "github.com/ijaa/filetype"
)

func main() {

    ok, _ := filetype.Is(filetype.TypeJpeg, "./sample.jpg")
	if !ok {
		fmt.Println("this file is not jpg")
    }
    
    ok, _ := filetype.IsIn([]filetype.FileType{filetype.TypeJpeg,filetype.TypePng}, "./sample.jpg")
	if !ok {
		fmt.Println("this file is not jpg or png")
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeZip  = newFileType("zip", "application/zip", checker.Zip)
	TypeTar  = newFileType("tar", "application/x-tar", checker.Tar)
	TypeRar  = newFileType("rar", "application/x-rar-compressed", checker.Rar)
	TypeGz   = newFileType("gz", "application/gzip", checker.Gz)
	TypeBz2  = newFileType("bz2", "application/x-bzip2", checker.Bz2)
	Type7z   = newFileType("7z", "application/x-7z-compressed", checker.SevenZ)
	TypePdf  = newFileType("pdf", "application/pdf", checker.Pdf)
	TypeExe  = newFileType("exe", "application/x-msdownload", checker.Exe)
	TypeSwf  = newFileType("swf", "application/x-shockwave-flash", checker.Swf)
	TypeRpm  = newFileType("rpm", "application/x-rpm", checker.Rpm)
	TypeElf  = newFileType("elf", "application/x-executable", checker.Elf)
	TypeIso  = newFileType("iso", "application/x-iso9660-image", checker.Iso)
	TypeWasm = newFileType("wasm", "application/wasm", checker.Wasm)
)

archive related filetype

View Source
var (
	TypeMp3 = newFileType("mp3", "audio/mpeg", checker.Mp3)
	TypeM4a = newFileType("m4a", "audio/m4a", checker.M4a)
	TypeWav = newFileType("wav", "audio/x-wav", checker.Wav)
)
View Source
var (
	TypeDoc  = newFileType("doc", "application/msword", checker.Doc)
	TypeDocx = newFileType("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", checker.Docx)
	TypeXls  = newFileType("xls", "application/vnd.ms-excel", checker.Xls)
	TypeXlsx = newFileType("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", checker.Xlsx)
	TypePpt  = newFileType("ppt", "application/vnd.ms-powerpoint", checker.Ppt)
	TypePptx = newFileType("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation", checker.Pptx)
)
View Source
var (
	TypeJpeg     = newFileType("jpg", "image/jpeg", checker.Jpeg)
	TypeJpeg2000 = newFileType("jp2", "image/jp2", checker.Jpeg2000)
	TypePng      = newFileType("png", "image/png", checker.Png)
	TypeGif      = newFileType("gif", "image/gif", checker.Gif)
	TypeWebp     = newFileType("webp", "image/webp", checker.Webp)
	TypeTiff     = newFileType("tif", "image/tiff", checker.Tiff)
	TypeBmp      = newFileType("bmp", "image/bmp", checker.Bmp)
	TypePsd      = newFileType("psd", "image/vnd.adobe.photoshop", checker.Psd)
	TypeIco      = newFileType("ico", "image/x-icon", checker.Ico)
)
View Source
var (
	TypeMp4  = newFileType("mp4", "video/mp4", checker.Mp4)
	TypeAvi  = newFileType("avi", "video/x-msvideo", checker.Avi)
	TypeWmv  = newFileType("wmv", "video/x-ms-wmv", checker.Wmv)
	TypeMpeg = newFileType("mpg", "video/mpeg", checker.Mpeg)
	TypeFlv  = newFileType("flv", "video/x-flv", checker.Flv)
	Type3gp  = newFileType("3gp", "video/3gpp", checker.Match3gp)
)

Functions

func Is

func Is(ft FileType, file string) (bool, error)

Is check the file's type equals the custom FileType return error when get the file content io operation failed

func IsIn

func IsIn(fts []FileType, file string) (bool, error)

IsIn check the file's type in one of the types

Types

type FileType

type FileType struct {
	MIME      string
	Extension string
	Offset    int64                      // the magic numbers content offset
	Limit     int64                      // the magic numbers content len
	Checker   func(string) (bool, error) // magic number checker func
}

FileType file's mime and extension

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL