Compare commits

..

No commits in common. "main" and "v0.2.2" have entirely different histories.
main ... v0.2.2

7 changed files with 87 additions and 190 deletions

View File

@ -8,10 +8,7 @@ local CreateRelease() = {
settings: { settings: {
api_key: { from_secret: 'GITEA_API_KEY' }, api_key: { from_secret: 'GITEA_API_KEY' },
base_url: 'https://git.unlock-music.dev', base_url: 'https://git.unlock-music.dev',
files: [ files: 'dist/*',
'um-*.tar.gz',
'um-*.zip',
],
checksum: 'sha256', checksum: 'sha256',
draft: true, draft: true,
title: '${DRONE_TAG}', title: '${DRONE_TAG}',
@ -20,36 +17,25 @@ local CreateRelease() = {
local StepGoBuild(GOOS, GOARCH) = { local StepGoBuild(GOOS, GOARCH) = {
local windows = GOOS == 'windows', local filepath = 'dist/um-%s-%s.tar.gz' % [GOOS, GOARCH],
local archiveExt = if windows then 'zip' else 'tar.gz',
local filepath = 'dist/um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
local archive = if windows then [
// Ensure zip is installed
'command -v zip >/dev/null || (apt update && apt install -y zip)',
'zip -9 -j -r "%s" $DIST_DIR' % filepath,
] else [
'tar -c -C $DIST_DIR um | gzip -9 > "%s"' % filepath,
],
name: 'go build %s/%s' % [GOOS, GOARCH], name: 'go build %s/%s' % [GOOS, GOARCH],
image: 'golang:1.22', image: 'golang:1.22',
environment: { environment: {
GOOS: GOOS, GOOS: GOOS,
GOARCH: GOARCH, GOARCH: GOARCH,
GOPROXY: 'https://goproxy.io,direct', GOPROXY: "https://goproxy.io,direct",
}, },
commands: [ commands: [
'DIST_DIR=$(mktemp -d)', 'DIST_DIR=$(mktemp -d)',
'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um', 'go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags --always)" -o $DIST_DIR ./cmd/um',
'mkdir -p dist', 'mkdir -p dist',
] + archive, 'tar cz -f %s -C $DIST_DIR .' % filepath,
],
}; };
local StepUploadArtifact(GOOS, GOARCH) = { local StepUploadArtifact(GOOS, GOARCH) = {
local windows = GOOS == 'windows', local filename = 'um-%s-%s.tar.gz' % [GOOS, GOARCH],
local archiveExt = if windows then 'zip' else 'tar.gz',
local filename = 'um-%s-%s-%s.%s' % [GOOS, GOARCH, '$(git describe --tags --always)', archiveExt],
local filepath = 'dist/%s' % filename, local filepath = 'dist/%s' % filename,
local pkgname = '${DRONE_REPO_NAME}-build', local pkgname = '${DRONE_REPO_NAME}-build',
@ -85,7 +71,7 @@ local PipelineBuild(GOOS, GOARCH, RUN_TEST) = {
name: 'go test', name: 'go test',
image: 'golang:1.22', image: 'golang:1.22',
environment: { environment: {
GOPROXY: 'https://goproxy.io,direct', GOPROXY: "https://goproxy.io,direct",
}, },
commands: ['go test -v ./...'], commands: ['go test -v ./...'],
}] else [] }] else []
@ -114,7 +100,7 @@ local PipelineRelease() = {
name: 'go test', name: 'go test',
image: 'golang:1.22', image: 'golang:1.22',
environment: { environment: {
GOPROXY: 'https://goproxy.io,direct', GOPROXY: "https://goproxy.io,direct",
}, },
commands: ['go test -v ./...'], commands: ['go test -v ./...'],
}, },
@ -122,17 +108,9 @@ local PipelineRelease() = {
StepGoBuild('linux', 'arm64'), StepGoBuild('linux', 'arm64'),
StepGoBuild('linux', '386'), StepGoBuild('linux', '386'),
StepGoBuild('windows', 'amd64'), StepGoBuild('windows', 'amd64'),
StepGoBuild('windows', 'arm64'),
StepGoBuild('windows', '386'), StepGoBuild('windows', '386'),
StepGoBuild('darwin', 'amd64'), StepGoBuild('darwin', 'amd64'),
StepGoBuild('darwin', 'arm64'), StepGoBuild('darwin', 'arm64'),
{
name: 'prepare root',
image: 'golang:1.22',
commands: [
'mv dist/*.tar.gz dist/*.zip ./',
],
},
CreateRelease(), CreateRelease(),
], ],
trigger: { trigger: {

View File

@ -17,8 +17,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags - tar cz -f dist/um-linux-amd64.tar.gz -C $DIST_DIR .
--always).tar.gz"
environment: environment:
GOARCH: amd64 GOARCH: amd64
GOOS: linux GOOS: linux
@ -26,10 +25,9 @@ steps:
image: golang:1.22 image: golang:1.22
name: go build linux/amd64 name: go build linux/amd64
- commands: - commands:
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64-$(git - curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-linux-amd64.tar.gz"
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64-$(git "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-linux-amd64.tar.gz"
describe --tags --always).tar.gz" - sha256sum dist/um-linux-amd64.tar.gz
- sha256sum dist/um-linux-amd64-$(git describe --tags --always).tar.gz
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER} - echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
environment: environment:
DRONE_GITEA_SERVER: https://git.unlock-music.dev DRONE_GITEA_SERVER: https://git.unlock-music.dev
@ -55,8 +53,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip) - tar cz -f dist/um-windows-amd64.tar.gz -C $DIST_DIR .
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
environment: environment:
GOARCH: amd64 GOARCH: amd64
GOOS: windows GOOS: windows
@ -64,10 +61,9 @@ steps:
image: golang:1.22 image: golang:1.22
name: go build windows/amd64 name: go build windows/amd64
- commands: - commands:
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64-$(git - curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-windows-amd64.tar.gz"
describe --tags --always).zip" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64-$(git "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-windows-amd64.tar.gz"
describe --tags --always).zip" - sha256sum dist/um-windows-amd64.tar.gz
- sha256sum dist/um-windows-amd64-$(git describe --tags --always).zip
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER} - echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
environment: environment:
DRONE_GITEA_SERVER: https://git.unlock-music.dev DRONE_GITEA_SERVER: https://git.unlock-music.dev
@ -93,8 +89,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags - tar cz -f dist/um-darwin-amd64.tar.gz -C $DIST_DIR .
--always).tar.gz"
environment: environment:
GOARCH: amd64 GOARCH: amd64
GOOS: darwin GOOS: darwin
@ -102,10 +97,9 @@ steps:
image: golang:1.22 image: golang:1.22
name: go build darwin/amd64 name: go build darwin/amd64
- commands: - commands:
- curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64-$(git - curl --fail --include --user "um-release-bot:$GITEA_API_KEY" --upload-file "dist/um-darwin-amd64.tar.gz"
describe --tags --always).tar.gz" "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64-$(git "$DRONE_GITEA_SERVER/api/packages/${DRONE_REPO_NAMESPACE}/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}/um-darwin-amd64.tar.gz"
describe --tags --always).tar.gz" - sha256sum dist/um-darwin-amd64.tar.gz
- sha256sum dist/um-darwin-amd64-$(git describe --tags --always).tar.gz
- echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER} - echo $DRONE_GITEA_SERVER/${DRONE_REPO_NAMESPACE}/-/packages/generic/${DRONE_REPO_NAME}-build/${DRONE_BUILD_NUMBER}
environment: environment:
DRONE_GITEA_SERVER: https://git.unlock-music.dev DRONE_GITEA_SERVER: https://git.unlock-music.dev
@ -137,8 +131,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-amd64-$(git describe --tags - tar cz -f dist/um-linux-amd64.tar.gz -C $DIST_DIR .
--always).tar.gz"
environment: environment:
GOARCH: amd64 GOARCH: amd64
GOOS: linux GOOS: linux
@ -150,8 +143,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-arm64-$(git describe --tags - tar cz -f dist/um-linux-arm64.tar.gz -C $DIST_DIR .
--always).tar.gz"
environment: environment:
GOARCH: arm64 GOARCH: arm64
GOOS: linux GOOS: linux
@ -163,7 +155,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-linux-386-$(git describe --tags --always).tar.gz" - tar cz -f dist/um-linux-386.tar.gz -C $DIST_DIR .
environment: environment:
GOARCH: "386" GOARCH: "386"
GOOS: linux GOOS: linux
@ -175,8 +167,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip) - tar cz -f dist/um-windows-amd64.tar.gz -C $DIST_DIR .
- zip -9 -j -r "dist/um-windows-amd64-$(git describe --tags --always).zip" $DIST_DIR
environment: environment:
GOARCH: amd64 GOARCH: amd64
GOOS: windows GOOS: windows
@ -188,21 +179,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip) - tar cz -f dist/um-windows-386.tar.gz -C $DIST_DIR .
- zip -9 -j -r "dist/um-windows-arm64-$(git describe --tags --always).zip" $DIST_DIR
environment:
GOARCH: arm64
GOOS: windows
GOPROXY: https://goproxy.io,direct
image: golang:1.22
name: go build windows/arm64
- commands:
- DIST_DIR=$(mktemp -d)
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist
- command -v zip >/dev/null || (apt update && apt install -y zip)
- zip -9 -j -r "dist/um-windows-386-$(git describe --tags --always).zip" $DIST_DIR
environment: environment:
GOARCH: "386" GOARCH: "386"
GOOS: windows GOOS: windows
@ -214,8 +191,7 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-amd64-$(git describe --tags - tar cz -f dist/um-darwin-amd64.tar.gz -C $DIST_DIR .
--always).tar.gz"
environment: environment:
GOARCH: amd64 GOARCH: amd64
GOOS: darwin GOOS: darwin
@ -227,18 +203,13 @@ steps:
- go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags - go build -v -trimpath -ldflags="-w -s -X main.AppVersion=$(git describe --tags
--always)" -o $DIST_DIR ./cmd/um --always)" -o $DIST_DIR ./cmd/um
- mkdir -p dist - mkdir -p dist
- tar -c -C $DIST_DIR um | gzip -9 > "dist/um-darwin-arm64-$(git describe --tags - tar cz -f dist/um-darwin-arm64.tar.gz -C $DIST_DIR .
--always).tar.gz"
environment: environment:
GOARCH: arm64 GOARCH: arm64
GOOS: darwin GOOS: darwin
GOPROXY: https://goproxy.io,direct GOPROXY: https://goproxy.io,direct
image: golang:1.22 image: golang:1.22
name: go build darwin/arm64 name: go build darwin/arm64
- commands:
- mv dist/*.tar.gz dist/*.zip ./
image: golang:1.22
name: prepare root
- image: plugins/gitea-release - image: plugins/gitea-release
name: create release name: create release
settings: settings:
@ -247,9 +218,7 @@ steps:
base_url: https://git.unlock-music.dev base_url: https://git.unlock-music.dev
checksum: sha256 checksum: sha256
draft: true draft: true
files: files: dist/*
- um-*.tar.gz
- um-*.zip
title: ${DRONE_TAG} title: ${DRONE_TAG}
trigger: trigger:
event: event:

4
.gitignore vendored
View File

@ -1,7 +1,3 @@
.idea .idea
/dist /dist
*.exe
/um-*.tar.gz
/um-*.zip

View File

@ -6,9 +6,6 @@ Original: Web Edition https://git.unlock-music.dev/um/web
- [Release Download](https://git.unlock-music.dev/um/cli/releases/latest) - [Release Download](https://git.unlock-music.dev/um/cli/releases/latest)
- [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/) - [Latest Build](https://git.unlock-music.dev/um/-/packages/generic/cli-build/)
> **WARNING**
> 在本站 fork 不会起到备份的作用,只会浪费服务器储存空间。如无必要请勿 fork 该仓库。
## Features ## Features
- [x] All Algorithm Supported By `unlock-music/web` - [x] All Algorithm Supported By `unlock-music/web`
@ -25,13 +22,3 @@ Original: Web Edition https://git.unlock-music.dev/um/web
- Drag the encrypted file to `um.exe` (Tested on Windows) - Drag the encrypted file to `um.exe` (Tested on Windows)
- Run: `./um [-o <output dir>] [-i] <input dir/file>` - Run: `./um [-o <output dir>] [-i] <input dir/file>`
- Use `./um -h` to show help menu - Use `./um -h` to show help menu
## Update CI pipeline
1. Install [Drone CI binary](https://docs.drone.io/cli/install/)
2. Edit `.drone.jsonnet`
3. Update drone CI pipeline:
```sh
drone jsonnet --format --stream
```

View File

@ -18,32 +18,25 @@ type DecoderParams struct {
} }
type NewDecoderFunc func(p *DecoderParams) Decoder type NewDecoderFunc func(p *DecoderParams) Decoder
type DecoderFactory struct { type decoderItem struct {
noop bool noop bool
Suffix string decoder NewDecoderFunc
Create NewDecoderFunc
} }
var DecoderRegistry []DecoderFactory var DecoderRegistry = make(map[string][]decoderItem)
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) { func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
DecoderRegistry = append(DecoderRegistry, DecoderRegistry[ext] = append(DecoderRegistry[ext],
DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")}) decoderItem{noop: noop, decoder: dispatchFunc})
} }
func GetDecoder(filename string, skipNoop bool) []DecoderFactory { func GetDecoder(filename string, skipNoop bool) (rs []NewDecoderFunc) {
var result []DecoderFactory ext := strings.ToLower(strings.TrimLeft(filepath.Ext(filename), "."))
// Some extensions contains multiple dots, e.g. ".kgm.flac", hence iterate for _, dec := range DecoderRegistry[ext] {
// all decoders for each extension.
name := strings.ToLower(filepath.Base(filename))
for _, dec := range DecoderRegistry {
if !strings.HasSuffix(name, dec.Suffix) {
continue
}
if skipNoop && dec.noop { if skipNoop && dec.noop {
continue continue
} }
result = append(result, dec) rs = append(rs, dec.decoder)
} }
return result return
} }

View File

@ -61,7 +61,4 @@ func init() {
common.RegisterDecoder("kgma", false, NewDecoder) common.RegisterDecoder("kgma", false, NewDecoder)
// Viper // Viper
common.RegisterDecoder("vpr", false, NewDecoder) common.RegisterDecoder("vpr", false, NewDecoder)
// Kugou Android
common.RegisterDecoder("kgm.flac", false, NewDecoder)
common.RegisterDecoder("vpr.flac", false, NewDecoder)
} }

View File

@ -8,7 +8,6 @@ import (
"io" "io"
"os" "os"
"os/signal" "os/signal"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
@ -34,7 +33,7 @@ import (
"unlock-music.dev/cli/internal/utils" "unlock-music.dev/cli/internal/utils"
) )
var AppVersion = "v0.2.7" var AppVersion = "v0.2.2"
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
@ -75,30 +74,16 @@ func main() {
func printSupportedExtensions() { func printSupportedExtensions() {
var exts []string var exts []string
extSet := make(map[string]int) for ext := range common.DecoderRegistry {
for _, factory := range common.DecoderRegistry {
ext := strings.TrimPrefix(factory.Suffix, ".")
if n, ok := extSet[ext]; ok {
extSet[ext] = n + 1
} else {
extSet[ext] = 1
}
}
for ext := range extSet {
exts = append(exts, ext) exts = append(exts, ext)
} }
sort.Strings(exts) sort.Strings(exts)
for _, ext := range exts { for _, ext := range exts {
fmt.Printf("%s: %d\n", ext, extSet[ext]) fmt.Printf("%s: %d\n", ext, len(common.DecoderRegistry[ext]))
} }
} }
func appMain(c *cli.Context) (err error) { func appMain(c *cli.Context) (err error) {
cwd, err := os.Getwd()
if err != nil {
return err
}
if c.Bool("supported-ext") { if c.Bool("supported-ext") {
printSupportedExtensions() printSupportedExtensions()
return nil return nil
@ -107,7 +92,10 @@ func appMain(c *cli.Context) (err error) {
if input == "" { if input == "" {
switch c.Args().Len() { switch c.Args().Len() {
case 0: case 0:
input = cwd input, err = os.Getwd()
if err != nil {
return err
}
case 1: case 1:
input = c.Args().Get(0) input = c.Args().Get(0)
default: default:
@ -116,20 +104,22 @@ func appMain(c *cli.Context) (err error) {
} }
output := c.String("output") output := c.String("output")
if output == "" {
var err error
output, err = os.Getwd()
if err != nil {
return err
}
if input == output {
return errors.New("input and output path are same")
}
}
inputStat, err := os.Stat(input) inputStat, err := os.Stat(input)
if err != nil { if err != nil {
return err return err
} }
if output == "" {
// Default to where the input is
if inputStat.IsDir() {
output = input
} else {
output = path.Dir(input)
}
}
outputStat, err := os.Stat(output) outputStat, err := os.Stat(output)
if err != nil { if err != nil {
if errors.Is(err, os.ErrNotExist) { if errors.Is(err, os.ErrNotExist) {
@ -152,7 +142,6 @@ func appMain(c *cli.Context) (err error) {
} }
proc := &processor{ proc := &processor{
inputDir: input,
outputDir: output, outputDir: output,
skipNoopDecoder: c.Bool("skip-noop"), skipNoopDecoder: c.Bool("skip-noop"),
removeSource: c.Bool("remove-source"), removeSource: c.Bool("remove-source"),
@ -161,8 +150,8 @@ func appMain(c *cli.Context) (err error) {
} }
if inputStat.IsDir() { if inputStat.IsDir() {
watchDir := c.Bool("watch") wacthDir := c.Bool("watch")
if !watchDir { if !wacthDir {
return proc.processDir(input) return proc.processDir(input)
} else { } else {
return proc.watchDir(input) return proc.watchDir(input)
@ -174,7 +163,6 @@ func appMain(c *cli.Context) (err error) {
} }
type processor struct { type processor struct {
inputDir string
outputDir string outputDir string
skipNoopDecoder bool skipNoopDecoder bool
@ -242,32 +230,29 @@ func (p *processor) processDir(inputDir string) error {
if err != nil { if err != nil {
return err return err
} }
var lastError error = nil
for _, item := range items { for _, item := range items {
filePath := filepath.Join(inputDir, item.Name())
if item.IsDir() { if item.IsDir() {
if err = p.processDir(filePath); err != nil {
lastError = err
}
continue continue
} }
if err := p.processFile(filePath); err != nil { filePath := filepath.Join(inputDir, item.Name())
lastError = err allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
if len(allDec) == 0 {
logger.Info("skipping while no suitable decoder", zap.String("source", item.Name()))
continue
}
if err := p.process(filePath, allDec); err != nil {
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
} }
} }
if lastError != nil {
return fmt.Errorf("last error: %w", lastError)
}
return nil return nil
} }
func (p *processor) processFile(filePath string) error { func (p *processor) processFile(filePath string) error {
allDec := common.GetDecoder(filePath, p.skipNoopDecoder) allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
if len(allDec) == 0 { if len(allDec) == 0 {
return errors.New("skipping while no suitable decoder") logger.Fatal("skipping while no suitable decoder")
} }
if err := p.process(filePath, allDec); err != nil { if err := p.process(filePath, allDec); err != nil {
@ -285,19 +270,7 @@ func (p *processor) processFile(filePath string) error {
return nil return nil
} }
func (p *processor) findDecoder(decoders []common.DecoderFactory, params *common.DecoderParams) (*common.Decoder, *common.DecoderFactory, error) { func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) error {
for _, factory := range decoders {
dec := factory.Create(params)
err := dec.Validate()
if err == nil {
return &dec, &factory, nil
}
logger.Warn("try decode failed", zap.Error(err))
}
return nil, nil, errors.New("no any decoder can resolve the file")
}
func (p *processor) process(inputFile string, allDec []common.DecoderFactory) error {
file, err := os.Open(inputFile) file, err := os.Open(inputFile)
if err != nil { if err != nil {
return err return err
@ -305,16 +278,26 @@ func (p *processor) process(inputFile string, allDec []common.DecoderFactory) er
defer file.Close() defer file.Close()
logger := logger.With(zap.String("source", inputFile)) logger := logger.With(zap.String("source", inputFile))
pDec, decoderFactory, err := p.findDecoder(allDec, &common.DecoderParams{ decParams := &common.DecoderParams{
Reader: file, Reader: file,
Extension: filepath.Ext(inputFile), Extension: filepath.Ext(inputFile),
FilePath: inputFile, FilePath: inputFile,
Logger: logger, Logger: logger,
})
if err != nil {
return err
} }
dec := *pDec
var dec common.Decoder
for _, decFunc := range allDec {
dec = decFunc(decParams)
if err := dec.Validate(); err == nil {
break
} else {
logger.Warn("try decode failed", zap.Error(err))
dec = nil
}
}
if dec == nil {
return errors.New("no any decoder can resolve the file")
}
params := &ffmpeg.UpdateMetadataParams{} params := &ffmpeg.UpdateMetadataParams{}
@ -371,19 +354,13 @@ func (p *processor) process(inputFile string, allDec []common.DecoderFactory) er
} }
} }
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile)) inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
if err != nil { outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt)
return fmt.Errorf("get relative dir failed: %w", err)
}
inFilename := strings.TrimSuffix(filepath.Base(inputFile), decoderFactory.Suffix)
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
if !p.overwriteOutput { if !p.overwriteOutput {
_, err := os.Stat(outPath) _, err := os.Stat(outPath)
if err == nil { if err == nil {
logger.Warn("output file already exist, skip", zap.String("destination", outPath)) return fmt.Errorf("output file %s is already exist", outPath)
return nil
} else if !errors.Is(err, os.ErrNotExist) { } else if !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("stat output file failed: %w", err) return fmt.Errorf("stat output file failed: %w", err)
} }