diff --git a/Makefile b/Makefile index 9ee2bbf..a0087ea 100644 --- a/Makefile +++ b/Makefile @@ -1,102 +1,53 @@ -ME = $(lastword $(MAKEFILE_LIST)) -.DEFAULT_GOAL := help -.PHONY: help -help: # prints this help - @bash -c "$$AUTOGEN_HELP_BASH" < $(ME) +.PHONY: run watch clean test ubuntu coverage lint quality fmt BINARY_NAME=tinytune VERSION=0.0.1 + COMMIT_HASH=$(shell git rev-parse --short HEAD) BUILD_TIMESTAMP=$(shell date '+%Y-%m-%dT%H:%M:%S') + LDFLAGS=-ldflags "-X 'main.Version=${VERSION}' -X 'main.CommitHash=${COMMIT_HASH}' -X 'main.BuildTimestamp=${BUILD_TIMESTAMP}' -X 'main.Mode=Production'" + CGO_LDFLAGS=-ljemalloc CGO_CFLAGS=-fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-free -.PHONY: build -build: ## build executables - echo "Building frontend assets" +GO=CGO_CFLAGS="${CGO_CFLAGS}" CGO_LDFLAGS="${CGO_LDFLAGS}" go + +LINUX_AMD64 = out/${BINARY_NAME}_linux_amd64 +WEB_ASSETS = web/assets/index.min.js + +RUN_FOLDER = ./test/ + +build: ${LINUX_AMD64} + +${WEB_ASSETS}: make -C ./web build - mkdir -p out/ - echo "Building executable" - GOARCH=amd64 \ - GOOS=linux \ - CGO_CFLAGS="${CGO_CFLAGS}" \ - CGO_LDFLAGS="${CGO_LDFLAGS}" \ - go build ${LDFLAGS} -o out/${BINARY_NAME}_linux_amd64 cmd/tinytune/tinytune.go - chmod +x out/${BINARY_NAME}_linux_amd64 - echo "Done" - -.PHONY: run + +${LINUX_AMD64}: ${WEB_ASSETS} + GOARCH=amd64 GOOS=linux ${GO} build ${LDFLAGS} -o ${LINUX_AMD64} cmd/tinytune/tinytune.go + run: ## run tinytune server - CGO_CFLAGS="${CGO_CFLAGS}" CGO_LDFLAGS="${CGO_LDFLAGS}" go run cmd/tinytune/tinytune.go ./test/ + ${GO} run cmd/tinytune/tinytune.go "${RUN_FOLDER}" -.PHONY: watch watch: ## run tinytune server and frontend in hot-reload way reflex -r '\.(html|go)$\' -s make run & make -C ./web watch -.PHONY: clean -clean: ## clean +clear: ## clean go clean rm -rf out/ - rm -f coverage*.out + make -C ./web clear -.PHONY: test test: ## run server tests go test -timeout 2m -race -failfast ./... -.PHONY: ubuntu ubuntu: ## Install deps for ubuntu (libvips, ffmpeg) sudo apt install build-essential libvips pkg-config libvips-dev libjemalloc-dev ffmpeg -y npm i --prefix ./web -.PHONY: coverage -coverage: ## coverage - make test - go tool cover -html=coverage.out - -.PHONY: lint lint: ## run server linting golangci-lint run --fix -.PHONY: quality -quality: ## check-quality - make fmt - make lint - -.PHONY: fmt fmt: ## run server prettyfier go fmt ./... -$(VERBOSE).SILENT: - - -define AUTOGEN_HELP_BASH - declare -A targets; declare -a torder - targetre='^([A-Za-z]+):.* *# *(.*)' - if [[ $$TERM && $$TERM != dumb && -t 1 ]]; then - ul=$$'\e[0;4m'; bbold=$$'\e[34;1m'; reset=$$'\e[0m' - fi - if [[ -n "$(TITLE)" ]]; then - printf "\n %sMakefile targets - $(TITLE)%s\n\n" "$$ul" "$$reset" - else - printf "\n %sMakefile targets%s\n\n" "$$ul" "$$reset" - fi - while read -r line; do - if [[ $$line =~ $$targetre ]]; then - target=$${BASH_REMATCH[1]}; help=$${BASH_REMATCH[2]} - torder+=("$$target") - targets[$$target]=$$help - if (( $${#target} > max )); then max=$${#target}; fi - fi - done - for t in "$${torder[@]}"; do - printf " %smake %-*s%s %s\n" "$$bbold" $$max "$$t" "$$reset" \ - "$${targets[$$t]}" - done - if [[ -n "$(HOMEPAGE)" ]]; then - printf "\n Homepage:\n $(HOMEPAGE)\n\n" - else - printf "\n" - fi -endef -export AUTOGEN_HELP_BASH +quality: fmt lint ## check-quality \ No newline at end of file diff --git a/README.md b/README.md index 4d001c9..561963c 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,9 @@ The latest version of the TinyTune can be found on the GitHub [releases page](ht ### Linux ``` -# check that you have ffmpeg installed -ffmpeg -v +# install dependencies + +apt install ffmpeg libvips libjemalloc2 wget https://github.com/alxarno/tinytune/releases/download/latest/tinytune_linux_amd64 diff --git a/internal/crawler_os_test.go b/internal/crawler_os_test.go index aae63c1..ec78b80 100644 --- a/internal/crawler_os_test.go +++ b/internal/crawler_os_test.go @@ -12,5 +12,5 @@ func TestCrawlerOS(t *testing.T) { files, err := NewCrawlerOS("../test").Scan("../test/index.tinytune") require.NoError(err) - require.Len(files, 20) + require.Len(files, 21) } diff --git a/pkg/index/meta.go b/pkg/index/meta.go index 02c3832..e779e83 100644 --- a/pkg/index/meta.go +++ b/pkg/index/meta.go @@ -89,7 +89,13 @@ func (m *Meta) setContentType() { videoFormats := []string{"3gp", "avi", "f4v", "flv", "hevc", "m4v", "mlv", "mov", "mp4", "m4a", "3g2", "mj2", "mpeg", "ogv", "webm", "wmv"} imageFormats := []string{"jpeg", "png", "jpg", "webp", "bmp", "gif"} - ext := strings.ToLower(filepath.Ext(string(m.AbsolutePath)))[1:] + ext := strings.ToLower(filepath.Ext(string(m.AbsolutePath))) + ext = strings.TrimPrefix(ext, ".") + + if ext == "" { + return + } + m.Extension = ext switch { diff --git a/pkg/preview/preview.go b/pkg/preview/preview.go index 43805dc..8eae428 100644 --- a/pkg/preview/preview.go +++ b/pkg/preview/preview.go @@ -9,6 +9,7 @@ import ( "sync/atomic" "time" + "github.com/alxarno/tinytune/pkg/throttle" "github.com/davidbyttow/govips/v2/vips" ) @@ -176,8 +177,12 @@ func (p Previewer) Pull(ctx context.Context, src Source) (Data, error) { if src.Size() > BigVideoSizeB { p.bigVideoQueue <- struct{}{} - err := throttle(ctx, BigVideoThrottleMaxOccupiedPercent, BigVideoThrottleMaxWaiting) - if errors.Is(err, context.Canceled) { + throttler := throttle.NewThrottler( + BigVideoThrottleMaxOccupiedPercent, + BigVideoThrottleMaxWaiting, + ) + + if errors.Is(throttler.Throttle(ctx), context.Canceled) { return defaultPreview, context.Canceled } diff --git a/pkg/preview/thottle.go b/pkg/throttle/linux.go similarity index 84% rename from pkg/preview/thottle.go rename to pkg/throttle/linux.go index dba89a7..1757a29 100644 --- a/pkg/preview/thottle.go +++ b/pkg/throttle/linux.go @@ -1,4 +1,4 @@ -package preview +package throttle import ( "bufio" @@ -69,14 +69,14 @@ func isMemoryPassCheck(maxMemoryOccupied float64) bool { return memoryOccupied < maxMemoryOccupied } -func throttle(ctx context.Context, maxMemoryOccupied float64, maxWaitingTime time.Duration) error { - if isMemoryPassCheck(maxMemoryOccupied) { +func (t throttler) Throttle(ctx context.Context) error { + if isMemoryPassCheck(t.maxMemoryOccupied) { return nil } ticker := time.NewTicker(time.Second) - timeout, cancel := context.WithTimeout(ctx, maxWaitingTime) + timeout, cancel := context.WithTimeout(ctx, t.maxWaitingTime) defer cancel() for { @@ -88,7 +88,7 @@ func throttle(ctx context.Context, maxMemoryOccupied float64, maxWaitingTime tim return nil case <-ticker.C: - if isMemoryPassCheck(maxMemoryOccupied) { + if isMemoryPassCheck(t.maxMemoryOccupied) { return nil } } diff --git a/pkg/throttle/throttle.go b/pkg/throttle/throttle.go new file mode 100644 index 0000000..b89864a --- /dev/null +++ b/pkg/throttle/throttle.go @@ -0,0 +1,20 @@ +package throttle + +import ( + "context" + "time" +) + +type Throttler interface { + Throttle(ctx context.Context) error +} + +type throttler struct { + maxMemoryOccupied float64 + maxWaitingTime time.Duration +} + +//nolint:ireturn +func NewThrottler(maxMemoryOccupied float64, maxWaitingTime time.Duration) Throttler { + return &throttler{maxMemoryOccupied, maxWaitingTime} +} diff --git a/test/NONAME b/test/NONAME new file mode 100644 index 0000000..815d171 --- /dev/null +++ b/test/NONAME @@ -0,0 +1 @@ +NONAME TEST FILE \ No newline at end of file diff --git a/web/Makefile b/web/Makefile index 9c303ab..a344917 100644 --- a/web/Makefile +++ b/web/Makefile @@ -1,46 +1,20 @@ -ME = $(lastword $(MAKEFILE_LIST)) -.DEFAULT_GOAL := help -.PHONY: help -help: # prints this help - @bash -c "$$AUTOGEN_HELP_BASH" < $(ME) +.PHONY: watch clear + +VENDOR = assets/vendor.bundle.js +INDEX =assets/index.min.js $(VERBOSE).SILENT: -.PHONY: build -build: ## build all assets (.js, .css) - npm run build-all +build: ${VENDOR} ${INDEX} ## build all assets (.js, .css) + +${VENDOR}: + npm run build-vendor + +${INDEX}: + npm run build -.PHONY: watch watch: ## build all assets in hot-reload way npm run watch -define AUTOGEN_HELP_BASH - declare -A targets; declare -a torder - targetre='^([A-Za-z]+):.* *# *(.*)' - if [[ $$TERM && $$TERM != dumb && -t 1 ]]; then - ul=$$'\e[0;4m'; bbold=$$'\e[34;1m'; reset=$$'\e[0m' - fi - if [[ -n "$(TITLE)" ]]; then - printf "\n %sMakefile targets - $(TITLE)%s\n\n" "$$ul" "$$reset" - else - printf "\n %sMakefile targets%s\n\n" "$$ul" "$$reset" - fi - while read -r line; do - if [[ $$line =~ $$targetre ]]; then - target=$${BASH_REMATCH[1]}; help=$${BASH_REMATCH[2]} - torder+=("$$target") - targets[$$target]=$$help - if (( $${#target} > max )); then max=$${#target}; fi - fi - done - for t in "$${torder[@]}"; do - printf " %smake %-*s%s %s\n" "$$bbold" $$max "$$t" "$$reset" \ - "$${targets[$$t]}" - done - if [[ -n "$(HOMEPAGE)" ]]; then - printf "\n Homepage:\n $(HOMEPAGE)\n\n" - else - printf "\n" - fi -endef -export AUTOGEN_HELP_BASH +clear: + rm -rf ${VENDOR} ${INDEX} \ No newline at end of file