Skip to content

Commit

Permalink
Merge pull request #2 from deadblue/develop
Browse files Browse the repository at this point in the history
Bump version to 1.1.0.
  • Loading branch information
deadblue authored Nov 1, 2024
2 parents 466e5b8 + 6aeaee2 commit 892c4d5
Show file tree
Hide file tree
Showing 19 changed files with 426 additions and 210 deletions.
7 changes: 5 additions & 2 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
version: 2

project_name: dlna115

before:
Expand All @@ -6,6 +8,7 @@ before:

builds:
- main: ./cmd/main
binary: dlna115
ldflags:
- -s -w
- -X github.com/deadblue/dlna115/pkg/version.version={{.Version}}
Expand Down Expand Up @@ -37,11 +40,11 @@ checksum:
name_template: "checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-next"
version_template: "{{ incpatch .Version }}-next"

release:
prerelease: auto
mode: keep-existing

changelog:
skip: true
disable: true
41 changes: 22 additions & 19 deletions app/login/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,33 @@ import (
"github.com/deadblue/elevengo/option"
)

const (
defaultPlatform = "linux"
)

func (c *Command) Init(args []string) (err error) {
// Parse args
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Usage = func() {}
var platform string
fs.StringVar(&platform, "platform", defaultPlatform, "")
fs.StringVar(&platform, "p", defaultPlatform, "")
fs.StringVar(&c.secret, "secret", "", "")
fs.StringVar(&c.secret, "s", "", "")
var platform string
fs.StringVar(&platform, "platform", "", "")
fs.StringVar(&platform, "p", "", "")
if err = fs.Parse(args); err != nil {
return
}

// Save args
c.opts = option.Qrcode()
switch platform {
case "mac":
c.platform = option.QrcodeLoginMac
case "windows":
c.platform = option.QrcodeLoginWindows
default:
c.platform = option.QrcodeLoginLinux
case "android":
c.opts.LoginAndroid()
case "ios":
c.opts.LoginIos()
case "tv":
c.opts.LoginTv()
case "wechat":
c.opts.LoginWechatMiniApp()
case "alipay":
c.opts.LoginAlipayMiniApp()
case "qandroid":
c.opts.LoginQandroid()
}
if fs.NArg() > 0 {
c.saveFile = fs.Arg(0)
Expand All @@ -40,22 +42,23 @@ func (c *Command) Init(args []string) (err error) {
}

const usageTemplate = `
Usage: %s [-p paltform] [-s secret-key] [save-file]
Usage: %s [-s secret-key] [-p platform] [save-file]
Description:
%s
Arguments:
-p, -platform <platform>
Simulte login on given platform.
Supported platform: linux/mac/windows, default is linux.
-s, -secret <secret-key>
Secret key to encrypt credential, keep it secret!
-p, -platform <login-platform>
Simulate login on specific platform.
Supported: web/android/ios/tv/wechat/alipay/qandroid
Default: web
save-file
File to save credential.
Example:
%s -p linux -s sesame credential.txt
%s -s sesame credential.txt
`

Expand Down
4 changes: 2 additions & 2 deletions app/login/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const (
)

type Command struct {
// Platform to login
platform option.QrcodeLoginOption
// Login options
opts *option.QrcodeOptions
// Secret to encrypt cookie
secret string
// File to save cookie
Expand Down
4 changes: 2 additions & 2 deletions app/login/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func (c *Command) Run() (err error) {
agent := elevengo.Default()
session := &elevengo.QrcodeSession{}
if err = agent.QrcodeStart(session, c.platform); err != nil {
if err = agent.QrcodeStart(session); err != nil {
log.Fatalf("Start login session failed: %s", err)
}

Expand All @@ -35,7 +35,7 @@ func (c *Command) Run() (err error) {
// QRCode expired or canceled
if err != nil {
// Request new QRCode
err = agent.QrcodeStart(session, c.platform)
err = agent.QrcodeStart(session)
if err == nil {
// Replace old QRCode
lineCount := strings.Count(qrAscii, "\n") + 1
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
module github.com/deadblue/dlna115

require (
github.com/deadblue/elevengo v0.6.2
github.com/deadblue/elevengo v0.7.4
github.com/deadblue/qrascii v0.0.1
github.com/google/uuid v1.3.1
golang.org/x/net v0.21.0
golang.org/x/sys v0.17.0
github.com/grafov/m3u8 v0.12.0
golang.org/x/net v0.30.0
golang.org/x/sys v0.26.0
gopkg.in/yaml.v3 v3.0.1
)

require filippo.io/nistec v0.0.3 // indirect

go 1.21
go 1.23
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
filippo.io/nistec v0.0.3 h1:h336Je2jRDZdBCLy2fLDUd9E2unG32JLwcJi0JQE9Cw=
filippo.io/nistec v0.0.3/go.mod h1:84fxC9mi+MhC2AERXI4LSa8cmSVOzrFikg6hZ4IfCyw=
github.com/deadblue/elevengo v0.6.2 h1:Q1e6REkSX5/bIEB1wt6CrCyBVbWwr5gTF5Z8a8ys5vc=
github.com/deadblue/elevengo v0.6.2/go.mod h1:E/4UJxjtZECiITsAkSVv4tR9TQa1P42s8qyTp7ghxK0=
github.com/deadblue/elevengo v0.7.4 h1:oVjnBLrmKQGdvoNeTw1oNhD3NBlT01v41dC6Ls460kM=
github.com/deadblue/elevengo v0.7.4/go.mod h1:rTKHkJuPwdRjRbDpfeARXxfvDHbPf7PLUvIfeTqQ9vE=
github.com/deadblue/qrascii v0.0.1 h1:N1JEM3Pz5Rti2WdjDIWMyFoR6LUld2+aO2xGT2/Vpdg=
github.com/deadblue/qrascii v0.0.1/go.mod h1:wmV7In0C2lHsIjpfx3FNpCAL0P3VJNZRFxSURd++LwA=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
github.com/grafov/m3u8 v0.12.0 h1:T6iTwTsSEtMcwkayef+FJO8kj+Sglr4Lh81Zj8Ked/4=
github.com/grafov/m3u8 v0.12.0/go.mod h1:nqzOkfBiZJENr52zTVd/Dcl03yzphIMbJqkXGu+u080=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
10 changes: 4 additions & 6 deletions pkg/util/cache.go → pkg/cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package util
package cache

import (
"sync"
Expand All @@ -11,18 +11,17 @@ type entry[V any] struct {
}

type TTLCache[V any] struct {
ttl time.Duration
mutex sync.RWMutex
cache map[string]entry[V]
}

func (c *TTLCache[V]) Put(key string, value V) {
func (c *TTLCache[V]) Put(key string, value V, expiration time.Duration) {
c.mutex.Lock()
defer c.mutex.Unlock()

now := time.Now()
c.cache[key] = entry[V]{
expiry: now.Add(c.ttl).Unix(),
expiry: now.Add(expiration).Unix(),
value: value,
}
}
Expand All @@ -48,9 +47,8 @@ func (c *TTLCache[V]) Remove(key string) {
delete(c.cache, key)
}

func NewCache[V any](ttl time.Duration) *TTLCache[V] {
func New[V any]() *TTLCache[V] {
return &TTLCache[V]{
ttl: ttl,
cache: make(map[string]entry[V]),
}
}
25 changes: 15 additions & 10 deletions pkg/mediaserver/service/contentdirectory/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,27 @@ import (

func (s *Service) HandleView(rw http.ResponseWriter, req *http.Request) {
filePath := req.URL.Path[_ViewUrlLen:]
offset, length := parseRequestRange(req.Header.Get("Range"))

// Check range header
isRange, offset, length := false, int64(0), int64(-1)
if rh := req.Header.Get("Range"); rh != "" {
isRange = true
offset, length = parseRequestRange(rh)
}
// Fetch content
content, err := s.ss.Fetch(filePath, offset, length)
if err != nil {
http.NotFound(rw, req)
return
}
defer content.Body.Close()

rw.Header().Set("Accept-Ranges", "bytes")
rw.Header().Set("Content-Type", content.MimeType)
rw.Header().Set("Content-Length", strconv.FormatInt(content.BodySize, 10))
if offset != 0 || length != -1 {
rw.Header().Set("Content-Range", fmt.Sprintf(
"bytes %d-%d/%d",
offset, offset+content.BodySize-1, content.FileSize,
// Send response
headers := rw.Header()
headers.Set("Content-Type", content.MimeType)
headers.Set("Content-Length", strconv.FormatInt(content.BodySize, 10))
if cr := content.Range; isRange && cr != nil {
headers.Set("Accept-Ranges", "bytes")
headers.Set("Content-Range", fmt.Sprintf(
"bytes %d-%d/%d", cr.Start, cr.End, cr.Total,
))
rw.WriteHeader(http.StatusPartialContent)
} else {
Expand Down
26 changes: 17 additions & 9 deletions pkg/storage/impl/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package impl
import (
"fmt"
"log"
"path/filepath"
"strings"

"github.com/deadblue/dlna115/pkg/storage"
Expand Down Expand Up @@ -51,12 +52,7 @@ func (s *Service) browseRoot() (items []storage.Item) {

func (s *Service) createItemList(it elevengo.Iterator[elevengo.File]) []storage.Item {
items := make([]storage.Item, 0)
var err error
for ; err == nil; err = it.Next() {
file := &elevengo.File{}
if it.Get(file) != nil {
continue
}
for _, file := range it.Items() {
if file.IsDirectory {
items = append(items, s.createDir(file))
} else if file.IsVideo && file.VideoDefinition > 0 {
Expand All @@ -83,7 +79,7 @@ func (s *Service) createVideoFile(file *elevengo.File) (item *storage.VideoFile)
item.Name = file.Name
item.Size = file.Size
item.MimeType = util.GetMimeType(file.Name)
item.URLPath = s.generatePath(file)
item.URLPath = s.generateFilePath(file)
item.Duration = file.MediaDuration
// GUESS resoltion from video definition
switch file.VideoDefinition {
Expand Down Expand Up @@ -111,7 +107,7 @@ func (s *Service) createAudioFile(file *elevengo.File) (item *storage.AudioFile)
item.Name = file.Name
item.Size = file.Size
item.MimeType = util.GetMimeType(file.Name)
item.URLPath = s.generatePath(file)
item.URLPath = s.generateFilePath(file)
item.Duration = file.MediaDuration
// Dummy values which we can not get form 115
item.AudioChannels = 2
Expand All @@ -125,6 +121,18 @@ func (s *Service) createImageFile(file *elevengo.File) (item *storage.ImageFile)
item.Name = file.Name
item.Size = file.Size
item.MimeType = util.GetMimeType(file.Name)
item.URLPath = s.generatePath(file)
item.URLPath = s.generateFilePath(file)
return
}

func (s *Service) generateFilePath(file *elevengo.File) string {
fetchType := _FetchTypeFile
if file.IsVideo && !s.opts.DisableHLS {
fetchType = _FetchTypeHls
}
fileExt := (filepath.Ext(file.Name))[1:]
return fmt.Sprintf(
"%s-%s/%s.%s",
fetchType, fileExt, file.PickCode, fileExt,
)
}
2 changes: 2 additions & 0 deletions pkg/storage/impl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ var (
errInvalidPath = errors.New("invalid request path")

errInvalidExt = errors.New("invalid request extension")

errSegmentNotFoud = errors.New("segment not found")
)
Loading

0 comments on commit 892c4d5

Please sign in to comment.