add file conversion

master
nixo 2019-05-21 16:43:27 +02:00
parent 6c14dbb592
commit 7776fcd05c
2 changed files with 37 additions and 3 deletions

View File

@ -311,9 +311,16 @@ function search3(req; dl = println)
query = HTTP.URIs.queryparams(req[:query])
q = get(query, "query", "")
isempty(q) && return missing_parameter("query")
if length(q) > 2 && q[end-1:end] == "!t"
if req[:user][:user].upload && length(q) > 2 && q[end-1:end] == "!t"
@info "This is the special torrent mode!"
dl(string(strip(q[1:end-2])))
list = dl(string(strip(q[1:end-2])))
list == nothing return @subsonic(nothing)
(xdoc, xroot) = subsonic()
results = new_child(xroot, "searchResult3")
# TODO: Push the results so that we can see what download just started
# push!(results, list)
return subsonic_return(xdoc)
end
songCount = parse(Int, get(query, "songCount", "20"))
# TODO:
@ -506,18 +513,44 @@ function getCoverArt(req::Dict)
return sendfile(Beets.albums[n].cover)
end
function giveconverted(file, bitrate, format)
data = convert(file, bitrate = bitrate, format = format)
headers = Dict{String,String}()
suffix = format
mime = suffix in keys(Mux.mimetypes) ? Mux.mimetypes[suffix] : suffix
headers["Content-Type"] = mime
headers["Content-Length"] = length(data)
return Dict(:body => data, :headers => headers)
end
function convert(infile; bitrate = 64, format = "oga")
pipeline(`ffmpeg -i $infile -y -c:a libvorbis -b:a $(bitrate)k -f $format pipe:1`,
stderr=devnull) |> read
end
canstream(u::User) = u.stream
"Streams a given media file."
function stream(req::Dict)
query = HTTP.URIs.queryparams(req[:query])
canstream(req[:login][:user]) || return unuthorized()
id = get(query, "id", "")
isempty(id) && return missing_parameter("id")
bitrate = try parse(Int, get(query, "maxBitRate", "0"))
catch e
isa(e, ArgumentError) && 0
@error e
end
format = get(query, "format", "oga")
songs = Beets.songs()
m = findfirst(x -> (x.uuid == id), songs)
m === nothing && return not_found("id")
return sendfile(songs[m].path)
output = bitrate == 0 ? sendfile(songs[m].path) :
giveconverted(songs[m].path, bitrate, format)
return output
end
function sendfile(path; suffix = nothing)

View File

@ -11,6 +11,7 @@ function torrentdl(query::AbstractString)
TransmissionRPC.getauth(rpc)
todl = RuTrackers.getdiscography(me, query)
TransmissionRPC.add.(Ref(rpc), RuTrackers.download.(Ref(me), todl))
todl
end
dispatch = stack(