aboutsummaryrefslogtreecommitdiff
path: root/tool/src/main.rs
blob: 6384822bdb0b0fcb851ef7fba2f0bf7bfdf249e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2023 metamuffin <metamuffin.org>
*/
#![feature(file_create_new)]

pub mod migrate;

use base64::Engine;
use clap::{Parser, Subcommand, ValueEnum};
use jellyclient::{Instance, LoginDetails};
use jellycommon::{config::GlobalConfig, Node, NodeKind, NodePrivate, NodePublic};
use log::{info, warn};
use migrate::migrate;
use rand::random;
use std::{fmt::Debug, fs::File, io::Write, path::PathBuf};

#[derive(Parser)]
struct Args {
    #[arg(short = 'N', long)]
    dry: bool,
    #[clap(subcommand)]
    action: Action,
}

#[derive(Subcommand)]
enum Action {
    /// Initialize a new jellything instance
    Init {
        /// Base path of the instance, must either be absolute or relative to the servers pwd
        base_path: PathBuf,
        #[arg(short, long)]
        brand: String,
        #[arg(short, long)]
        hostname: String,
    },
    // /// Imports a movie, video or series given media and metadata sources
    // New {
    //     /// Relative path to the node's parent(!).
    //     path: PathBuf,
    //     /// Search the node by title on TMDB
    //     #[arg(short = 't', long)]
    //     tmdb_search: Option<String>,
    //     /// Search the node by id on TMDB
    //     #[arg(short = 'T', long)]
    //     tmdb_id: Option<String>,
    //     #[arg(long)]
    //     /// Prefix the inferred id with something to avoid collisions
    //     ident_prefix: Option<String>,
    //     /// Copies media into the library
    //     #[arg(long)]
    //     copy: bool,
    //     /// Moves media into the library (potentially destructive operation)
    //     #[arg(long)]
    //     r#move: bool,
    //     /// Marks node as a video
    //     #[arg(long)]
    //     video: bool,
    //     /// Marks node as a series
    //     #[arg(short, long)]
    //     series: bool,
    //     /// Path to the media of the node, required for non-series
    //     #[arg(short, long)]
    //     input: Option<PathBuf>,
    //     /// Ignore attachments (dont use them as cover)
    //     #[arg(long)]
    //     ignore_attachments: bool,
    //     /// Ignore metadate (no title, description and tagline from input)
    //     #[arg(long)]
    //     ignore_metadata: bool,
    //     /// Skip any action that appears to be run already.
    //     #[arg(long)]
    //     skip_existing: bool,
    //     /// Sets the title
    //     #[arg(long)]
    //     title: Option<String>,
    // },
    Migrate {
        database: PathBuf,
        mode: MigrateMode,
        save_location: PathBuf,
    },
    Reimport {
        /// Path to global jellything config
        config: PathBuf,
        /// Custom hostname, the config's is used by default
        #[arg(long)]
        hostname: Option<String>,
        /// Disable TLS. Dont use this.
        #[arg(long)]
        no_tls: bool,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, ValueEnum)]
enum MigrateMode {
    Import,
    Export,
}

fn main() -> anyhow::Result<()> {
    env_logger::builder()
        .filter_level(log::LevelFilter::Info)
        .parse_env("LOG")
        .init();
    let args = Args::parse();

    match args.action {
        Action::Init {
            base_path: path,
            brand,
            hostname,
        } => {
            info!("creating new instance...");
            std::fs::create_dir_all(path.join("library"))?;
            std::fs::create_dir_all(path.join("cache"))?;
            std::fs::create_dir_all(path.join("assets"))?;
            std::fs::create_dir_all(path.join("media"))?;
            File::create_new(path.join("assets/front.htm"))?
                .write_fmt(format_args!("<h1>My very own jellything instance</h1>"))?;

            // TODO: dont fill that
            serde_yaml::to_writer(
                File::create_new(path.join("config.yaml"))?,
                &GlobalConfig {
                    brand: brand.clone(),
                    hostname,
                    slogan: "Creative slogan here".to_string(),
                    asset_path: path.join("assets"),
                    cache_path: path.join("cache"),
                    library_path: path.join("library"),
                    database_path: path.join("database"),
                    temp_path: "/tmp".into(),
                    cookie_key: Some(
                        base64::engine::general_purpose::STANDARD
                            .encode([(); 32].map(|_| random())),
                    ),
                    session_key: Some(
                        base64::engine::general_purpose::STANDARD
                            .encode([(); 32].map(|_| random())),
                    ),
                    admin_username: "admin".to_string(),
                    admin_password: "hackme".to_string(),
                    login_expire: 10,
                    ..Default::default()
                },
            )?;
            serde_json::to_writer(
                File::create_new(path.join("library/directory.json"))?,
                &Node {
                    public: NodePublic {
                        kind: Some(NodeKind::Collection),
                        title: Some("My Library".to_string()),
                        ..Default::default()
                    },
                    private: NodePrivate {
                        ..Default::default()
                    },
                },
            )?;
            info!("{brand:?} is ready!");
            warn!("please change the admin password.");
            Ok(())
        }
        // a @ Action::New { .. } => import(a, args.dry),
        a @ Action::Migrate { .. } => migrate(a),
        Action::Reimport {
            config,
            hostname,
            no_tls,
        } => tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .unwrap()
            .block_on(async move {
                let config = serde_yaml::from_reader::<_, GlobalConfig>(File::open(config)?)?;

                let inst = Instance::new(hostname.unwrap_or(config.hostname.clone()), !no_tls);
                info!("login");
                let session = inst
                    .login(LoginDetails {
                        drop_permissions: None,
                        expire: None,
                        password: config.admin_password,
                        username: config.admin_username,
                    })
                    .await?;

                session.reimport().await?;
                Ok(())
            }),
    }
}

fn ok_or_warn<T, E: Debug>(r: Result<T, E>) -> Option<T> {
    match r {
        Ok(t) => Some(t),
        Err(e) => {
            warn!("{e:?}");
            None
        }
    }
}