diff --git a/JlSonic/api.jl b/JlSonic/api.jl index deb9bcd..58c58fb 100644 --- a/JlSonic/api.jl +++ b/JlSonic/api.jl @@ -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) diff --git a/router.jl b/router.jl index 34217cb..0b22422 100644 --- a/router.jl +++ b/router.jl @@ -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(