vfs

package module
v1.3.3 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 35 Imported by: 0

README

VFS Service

Release Build Status

Examples

Hash upload

curl --upload-file image.jpg http://localhost:9999/upload/hash

How to run
createdb vfs
psql -f docs/vfs.sql vfs
mkdir testdata
make run
Upload image
wget -O image.jpg https://media.myshows.me/shows/e/22/e22c3ab75b956c6c1c1fca8182db7efb.jpg
export AUTHTOKEN=`curl http://localhost:9999/auth-token`    
curl --upload-file image.jpg  -H  "AuthorizationJWT: ${AUTHTOKEN}" http://localhost:9999/upload/hash
open http://localhost:9999/media/6/4a/64a9f060983200709061894cc5f69f83.jpg

Upload modes

HTTP Params:

  • ns: namespace, default is ""
  • ext: file extension, default is "jpg". jpeg or <empty> will convert to jpg.
Hash Upload via HTTP PUT
  • Default namespace and default extension (jpg): curl --upload-file image.jpg http://localhost:9999/upload/hash
  • Specific namespace (test): curl --upload-file image.jpg http://localhost:9999/upload/hash?ns=test
  • Specific namespace (test) with file extension: curl --upload-file image.gif http://localhost:9999/upload/hash?ns=test&ext=gif
Hash Upload via HTTP POST
  • Default namespace and default extension (jpg): curl -F 'Filedata=@image.jpg' http://localhost:9999/upload/hash
  • Specific namespace (test): curl -F 'Filedata=@image.jpg' http://localhost:9999/upload/hash?ns=test
  • Specific namespace (test) with file extension: curl -F 'Filedata=@image.gif' http://localhost:9999/upload/hash?ns=test&ext=gif

Documentation

Index

Examples

Constants

View Source
const (
	DefaultHashExtension = "jpg"
	NamespacePublic      = ""
)
View Source
const AtomTime = "02.01.2006 15:04"
View Source
const (
	DefaultNamespace = "default"
)

Variables

View Source
var (
	ErrInternal     = httpAsRpcError(http.StatusInternalServerError)
	ErrNotFound     = httpAsRpcError(http.StatusNotFound)
	ErrInvalidSort  = zenrpc.NewStringError(http.StatusBadRequest, "invalid sort field")
	ErrInvalidInput = zenrpc.NewStringError(http.StatusBadRequest, "invalid user input")
)
View Source
var (
	ErrInvalidNamespace = errors.New("invalid namespace")
	ErrInvalidExtension = errors.New("invalid extension")
	ErrInvalidMimeType  = errors.New("invalid mime type")
)
View Source
var RPC = struct {
	Service struct{ GetFolder, GetFolderBranch, GetFiles, CountFiles, MoveFiles, DeleteFiles, SetFilePhysicalName, SearchFolderByFileId, SearchFolderByFile, GetFavorites, ManageFavorites, CreateFolder, DeleteFolder, MoveFolder, RenameFolder, HelpUpload, UrlByHash, UrlByHashList, DeleteHash string }
}{
	Service: struct{ GetFolder, GetFolderBranch, GetFiles, CountFiles, MoveFiles, DeleteFiles, SetFilePhysicalName, SearchFolderByFileId, SearchFolderByFile, GetFavorites, ManageFavorites, CreateFolder, DeleteFolder, MoveFolder, RenameFolder, HelpUpload, UrlByHash, UrlByHashList, DeleteHash string }{
		GetFolder:            "getfolder",
		GetFolderBranch:      "getfolderbranch",
		GetFiles:             "getfiles",
		CountFiles:           "countfiles",
		MoveFiles:            "movefiles",
		DeleteFiles:          "deletefiles",
		SetFilePhysicalName:  "setfilephysicalname",
		SearchFolderByFileId: "searchfolderbyfileid",
		SearchFolderByFile:   "searchfolderbyfile",
		GetFavorites:         "getfavorites",
		ManageFavorites:      "managefavorites",
		CreateFolder:         "createfolder",
		DeleteFolder:         "deletefolder",
		MoveFolder:           "movefolder",
		RenameFolder:         "renamefolder",
		HelpUpload:           "helpupload",
		UrlByHash:            "urlbyhash",
		UrlByHashList:        "urlbyhashlist",
		DeleteHash:           "deletehash",
	},
}

Functions

func InternalError

func InternalError(err error) *zenrpc.Error

Types

type Config

type Config struct {
	MaxFileSize      int64
	Path             string
	WebPath          string
	PreviewPath      string
	Database         *pg.Options
	Namespaces       []string
	Extensions       []string
	MimeTypes        []string
	UploadFormName   string
	SaltedFilenames  bool
	SkipFolderVerify bool
}

type File

type File struct {
	ID          int        `json:"id"`
	Name        string     `json:"name"`
	Path        string     `json:"path"`
	PreviewPath string     `json:"previewpath"`
	RelPath     string     `json:"relpath"`
	Size        int        `json:"size"`
	SizeH       []string   `json:"sizeH"`
	Date        string     `json:"date"`
	Type        string     `json:"type"`
	Extension   string     `json:"extension"`
	Params      FileParams `json:"params"`
	Shortpath   string     `json:"shortpath"`
	Width       *int       `json:"width"`
	Height      *int       `json:"height"`
}

func NewFile

func NewFile(in *db.VfsFile, webpath, previewpath string) *File

type FileHash

type FileHash struct {
	Hash string
	Ext  string
}
Example
package main

import (
	"fmt"

	"github.com/vmkteam/vfs"
)

func main() {
	fh := vfs.NewFileHash("6698364ea6730f327a26bb8a6d3da3be", "")
	fmt.Println(fh.Dir())
	fmt.Println(fh.File())

	// automatic rewrite jpeg extension to jpg
	fh2 := vfs.NewFileHash("6698364ea6730f327a26bb8a6d3da3be", "jpeg")
	fmt.Println(fh2.File())

}
Output:

6/69
6/69/6698364ea6730f327a26bb8a6d3da3be.jpg
6/69/6698364ea6730f327a26bb8a6d3da3be.jpg

func NewFileHash

func NewFileHash(hash, ext string) FileHash

func (FileHash) Dir

func (h FileHash) Dir() string

func (FileHash) File

func (h FileHash) File() string

type FileParams

type FileParams struct {
	Width  int `json:"width,omitempty"`
	Height int `json:"height,omitempty"`
}

type Folder

type Folder struct {
	ID       int      `json:"id"`
	Name     string   `json:"name"`
	ParentID *int     `json:"parentId"`
	Folders  []Folder `json:"folders,omitempty"`
}

func NewFolder

func NewFolder(in *db.VfsFolder) *Folder

func NewFullFolder

func NewFullFolder(in *db.VfsFolder, childFolders []db.VfsFolder) *Folder

type HashIndexer

type HashIndexer struct {
	// contains filtered or unexported fields
}

func NewHashIndexer

func NewHashIndexer(dbc db.DB, repo *db.VfsRepo, vfs VFS, totalWorkers int, batchSize uint64) *HashIndexer

func (HashIndexer) IndexFile

func (hi HashIndexer) IndexFile(ns, relFilepath string) (HashInfo, error)

func (HashIndexer) Preview

func (hi HashIndexer) Preview() http.HandlerFunc

func (HashIndexer) ProcessQueue

func (hi HashIndexer) ProcessQueue(ctx context.Context) error

ProcessQueue gets not indexed data from vfsHashes, index and saves data to db.

func (HashIndexer) ScanFiles

func (hi HashIndexer) ScanFiles(ctx context.Context) (r ScanResults, err error)

ScanFiles reads media folder, detects namespaces & files and loads files into vfsHashes.

func (HashIndexer) ScanFilesHandler

func (hi HashIndexer) ScanFilesHandler(w http.ResponseWriter, _ *http.Request)

func (HashIndexer) Start

func (hi HashIndexer) Start()

func (HashIndexer) Stop

func (hi HashIndexer) Stop()

type HashInfo

type HashInfo struct {
	Hash      string
	Extension string
	Width     int
	Height    int
	FileSize  int64
	BlurHash  string
}

type HelpUploadItem

type HelpUploadItem struct {
	URL    string   `json:"url"`
	Params []string `json:"params"`
}

type HelpUploadResponse

type HelpUploadResponse struct {
	Temp  HelpUploadItem `json:"temp"`
	Queue HelpUploadItem `json:"queue"`
}

type ScanFilesResponse

type ScanFilesResponse struct {
	ScanResults `json:",omitempty"`
	Error       string `json:"error,omitempty"` // error message
}

type ScanResults

type ScanResults struct {
	Scanned  uint64        `json:"scanned"`
	Added    uint64        `json:"added"`
	Duration time.Duration `json:"duration"`
}

type Service

type Service struct {
	zenrpc.Service
	// contains filtered or unexported fields
}

func NewService

func NewService(repo db.VfsRepo, vfs VFS, dbc *pg.DB) Service

func (Service) CountFiles

func (s Service) CountFiles(ctx context.Context, folderId int, query *string) (int, error)

CountFiles returns count of files.

func (Service) CreateFolder

func (s Service) CreateFolder(ctx context.Context, rootFolderId int, name string) (bool, error)

CreateFolder create virtual folder.

func (Service) DeleteFiles

func (s Service) DeleteFiles(ctx context.Context, fileIds []int64) (bool, error)

DeleteFiles remove files.

func (Service) DeleteFolder

func (s Service) DeleteFolder(ctx context.Context, folderId int) (bool, error)

DeleteFolder removes Folder.

func (Service) DeleteHash added in v1.3.0

func (s Service) DeleteHash(ctx context.Context, namespace, hash string) (bool, error)

DeleteHash delete file by namespace and hash.

func (Service) GetFavorites

func (s Service) GetFavorites(ctx context.Context) ([]Folder, error)

GetFavorites return favorites list.

func (Service) GetFiles

func (s Service) GetFiles(ctx context.Context, folderId int, query *string, sortField string, isDescending bool, page, pageSize int) ([]File, error)

GetFiles returns list of files.

func (Service) GetFolder

func (s Service) GetFolder(ctx context.Context, rootFolderId int) (*Folder, error)

GetFolder returns Folder with sub folders.

func (Service) GetFolderBranch

func (s Service) GetFolderBranch(ctx context.Context, folderId int) ([]Folder, error)

GetFolderBranch returns Folder branch.

func (Service) HelpUpload

func (s Service) HelpUpload() HelpUploadResponse

HelpUpload returns a uploader help info.

func (Service) Invoke

func (s Service) Invoke(ctx context.Context, method string, params json.RawMessage) zenrpc.Response

Invoke is as generated code from zenrpc cmd

func (Service) ManageFavorites

func (s Service) ManageFavorites(ctx context.Context, folderId int, isInFavorites bool) (bool, error)

ManageFavorites manage favorite virtual folders.

func (Service) MoveFiles

func (s Service) MoveFiles(ctx context.Context, fileIds []int64, destinationFolderId int) (bool, error)

MoveFiles move files to destination folder.

func (Service) MoveFolder

func (s Service) MoveFolder(ctx context.Context, folderId, destinationFolderId int) (bool, error)

MoveFolder move Folder to destination folder.

func (Service) RenameFolder

func (s Service) RenameFolder(ctx context.Context, folderId int, name string) (bool, error)

RenameFolder change Folder name.

func (Service) SMD

func (Service) SMD() smd.ServiceInfo

func (Service) SearchFolderByFile

func (s Service) SearchFolderByFile(ctx context.Context, filename string) (*Folder, error)

SearchFolderByFile return Folder by File name.

func (Service) SearchFolderByFileId

func (s Service) SearchFolderByFileId(ctx context.Context, fileId int) (*Folder, error)

SearchFolderByFileId return Folder by File id.

func (Service) SetFilePhysicalName

func (s Service) SetFilePhysicalName(ctx context.Context, fileId int, name string) (bool, error)

SetFilePhysicalName renames File on server.

func (Service) UrlByHash

func (s Service) UrlByHash(_ context.Context, hash, namespace, mediaType string) (string, error)

UrlByHash get Url by hash, namespace and media type

func (Service) UrlByHashList

func (s Service) UrlByHashList(ctx context.Context, hashList []string, namespace, mediaType string) ([]UrlByHashListResponse, error)

UrlByHashList get Urls by hash list, with namespace and media type

type UploadResponse

type UploadResponse struct {
	Code      int    `json:"-"`                 // http status code
	Error     string `json:"error,omitempty"`   // error message
	Hash      string `json:"hash,omitempty"`    // for hash
	WebPath   string `json:"webPath,omitempty"` // for hash
	FileID    int    `json:"id,omitempty"`      // vfs file id
	Extension string `json:"ext,omitempty"`     // vfs file ext
	Name      string `json:"name,omitempty"`    // vfs file name
	Size      int64  `json:"-"`
}

type UrlByHashListResponse

type UrlByHashListResponse struct {
	Hash    string `json:"hash"`
	WebPath string `json:"webPath"`
}

type VFS

type VFS struct {
	// contains filtered or unexported fields
}

func New

func New(cfg Config) (VFS, error)

func (VFS) FullDir

func (v VFS) FullDir(ns string, h FileHash) string

func (VFS) FullFile

func (v VFS) FullFile(ns string, h FileHash) string

func (VFS) HashUpload

func (v VFS) HashUpload(r io.Reader, ns, ext string) (*FileHash, error)

func (VFS) HashUploadHandler

func (v VFS) HashUploadHandler(repo *db.VfsRepo) http.HandlerFunc

func (VFS) IsValidExtension

func (v VFS) IsValidExtension(ext string) bool

func (VFS) IsValidMimeType added in v1.2.0

func (v VFS) IsValidMimeType(mType string) bool

func (VFS) IsValidNamespace

func (v VFS) IsValidNamespace(ns string) bool

func (VFS) Move

func (v VFS) Move(ns, currentPath, newPath string) error

func (VFS) Path

func (v VFS) Path(ns, path string) string

func (VFS) PreviewPath

func (v VFS) PreviewPath(ns string) string

func (VFS) Upload

func (v VFS) Upload(r io.Reader, relFilename, ns string) error

func (VFS) UploadHandler

func (v VFS) UploadHandler(repo db.VfsRepo) http.HandlerFunc

func (VFS) WebHashPath

func (v VFS) WebHashPath(ns string, h FileHash) string

func (VFS) WebHashPathWithType

func (v VFS) WebHashPathWithType(ns, fType string, h FileHash) string

func (VFS) WebPath

func (v VFS) WebPath(ns string) string

Directories

Path Synopsis
cmd
nolint
nolint

Jump to

Keyboard shortcuts

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