2025-01-31 01:18:15 -08:00
|
|
|
defmodule Bright.Torrents do
|
|
|
|
@moduledoc """
|
|
|
|
The Torrents context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
|
|
|
|
2025-02-01 20:00:49 -08:00
|
|
|
alias Bright.Streams.Vod
|
|
|
|
alias Bright.{Repo,Cache,Torrentfile,B2}
|
2025-01-31 01:18:15 -08:00
|
|
|
alias Bright.Torrents.Torrent
|
|
|
|
|
2025-02-01 20:00:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-31 01:18:15 -08:00
|
|
|
@doc """
|
|
|
|
Returns the list of torrent.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
2025-02-01 20:00:49 -08:00
|
|
|
iex> list_torrents()
|
2025-01-31 01:18:15 -08:00
|
|
|
[%Torrent{}, ...]
|
|
|
|
|
|
|
|
"""
|
2025-02-01 20:00:49 -08:00
|
|
|
def list_torrents do
|
2025-01-31 01:18:15 -08:00
|
|
|
Repo.all(Torrent)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single torrent.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Torrent does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_torrent!(123)
|
|
|
|
%Torrent{}
|
|
|
|
|
|
|
|
iex> get_torrent!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_torrent!(id), do: Repo.get!(Torrent, id)
|
|
|
|
|
2025-02-01 20:00:49 -08:00
|
|
|
|
2025-01-31 01:18:15 -08:00
|
|
|
@doc """
|
|
|
|
Creates a torrent.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_torrent(%{field: value})
|
|
|
|
{:ok, %Torrent{}}
|
|
|
|
|
|
|
|
iex> create_torrent(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_torrent(attrs \\ %{}) do
|
|
|
|
%Torrent{}
|
|
|
|
|> Torrent.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2025-02-01 20:00:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-31 01:18:15 -08:00
|
|
|
@doc """
|
|
|
|
Updates a torrent.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> update_torrent(torrent, %{field: new_value})
|
|
|
|
{:ok, %Torrent{}}
|
|
|
|
|
|
|
|
iex> update_torrent(torrent, %{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_torrent(%Torrent{} = torrent, attrs) do
|
|
|
|
torrent
|
|
|
|
|> Torrent.changeset(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Deletes a torrent.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_torrent(torrent)
|
|
|
|
{:ok, %Torrent{}}
|
|
|
|
|
|
|
|
iex> delete_torrent(torrent)
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_torrent(%Torrent{} = torrent) do
|
|
|
|
Repo.delete(torrent)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking torrent changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> change_torrent(torrent)
|
|
|
|
%Ecto.Changeset{data: %Torrent{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def change_torrent(%Torrent{} = torrent, attrs \\ %{}) do
|
|
|
|
Torrent.changeset(torrent, attrs)
|
|
|
|
end
|
2025-02-01 20:00:49 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# @doc """
|
|
|
|
# Generates a torrent file on disk, then uploads it to S3
|
|
|
|
# """
|
|
|
|
# def generate_torrent_file(input_path, output_path, web_seed_url, vod_id) do
|
|
|
|
# s3_asset_key = Cache.deterministic_filename("vod", attrs.vod_id, "mp4")
|
|
|
|
# output_path = Cache.generate_filename(s3_asset_key, "torrent")
|
|
|
|
# input_path = Cache.get(s3_asset_key).local_path
|
|
|
|
# tracker_url = bittorrent_tracker_url()
|
|
|
|
# source_url = URI.append_path(URI.parse(site_url()), "/vods/#{vod_id}") |> URI.to_string()
|
|
|
|
# comment = site_url()
|
|
|
|
# meta_version = 3 # hybrid BT v1 & v2
|
|
|
|
|
|
|
|
# {:ok, %{btih: btih, btmh: btmh, magnet: magnet, save_path: save_path} = torrentfile} = Torrentfile.create(input_path, output_path, tracker_url, source_url, comment, web_seed_url, meta_version)
|
|
|
|
|
|
|
|
|
|
|
|
# # upload to s3
|
|
|
|
# {:ok, asset} = B2.put(save_path, s3_asset_key)
|
|
|
|
|
|
|
|
# # {:ok, %{basename: basename, local_path: save_path, magnet_link: magnet, info_hash_v1: btih, info_hash_v2: btmh}}
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-01-31 01:18:15 -08:00
|
|
|
end
|