@r_masseater/masseater-utils 0.9.0 → 0.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/native/desktop-shell/Cargo.toml +38 -0
- package/native/desktop-shell/src/app/builder.rs +39 -0
- package/native/desktop-shell/src/app/mod.rs +3 -0
- package/native/desktop-shell/src/entities/access_token.rs +77 -0
- package/native/desktop-shell/src/entities/mod.rs +1 -0
- package/native/desktop-shell/src/features/mod.rs +4 -0
- package/native/desktop-shell/src/features/record_telemetry/appender.rs +127 -0
- package/native/desktop-shell/src/features/record_telemetry/forwarder.rs +183 -0
- package/native/desktop-shell/src/features/record_telemetry/invoke.rs +80 -0
- package/native/desktop-shell/src/features/record_telemetry/mod.rs +231 -0
- package/native/desktop-shell/src/features/serve_screen/mod.rs +135 -0
- package/native/desktop-shell/src/features/serve_tools/mod.rs +37 -0
- package/native/desktop-shell/src/features/update_app/mod.rs +53 -0
- package/native/desktop-shell/src/lib.rs +17 -0
- package/native/desktop-shell/src/shared/lib/bearer.rs +49 -0
- package/native/desktop-shell/src/shared/lib/host.rs +1 -0
- package/native/desktop-shell/src/shared/lib/mod.rs +5 -0
- package/native/desktop-shell/src/shared/mod.rs +2 -0
- package/native/desktop-shell/src/shared/ui/log.rs +15 -0
- package/native/desktop-shell/src/shared/ui/menu.rs +37 -0
- package/native/desktop-shell/src/shared/ui/mod.rs +7 -0
- package/native/desktop-shell/src/shared/ui/window.rs +49 -0
- package/native/media-notes/Cargo.toml +36 -0
- package/native/media-notes/src/entities/media_file.rs +89 -0
- package/native/media-notes/src/entities/media_notes.rs +31 -0
- package/native/media-notes/src/entities/mod.rs +3 -0
- package/native/media-notes/src/entities/source/credit.rs +129 -0
- package/native/media-notes/src/entities/source/link.rs +194 -0
- package/native/media-notes/src/entities/source/mod.rs +5 -0
- package/native/media-notes/src/features/mod.rs +2 -0
- package/native/media-notes/src/features/open_media/mod.rs +88 -0
- package/native/media-notes/src/features/share_mcp_access.rs +15 -0
- package/native/media-notes/src/lib.rs +12 -0
- package/native/media-notes/src/shared/db/mod.rs +3 -0
- package/native/media-notes/src/shared/db/store.rs +276 -0
- package/native/media-notes/src/shared/lib/failure.rs +55 -0
- package/native/media-notes/src/shared/lib/mcp.rs +34 -0
- package/native/media-notes/src/shared/lib/mod.rs +5 -0
- package/native/media-notes/src/shared/mod.rs +3 -0
- package/native/media-notes/src/shared/protocol.rs +69 -0
- package/package.json +5 -1
- package/skills/build-tauri-app/SKILL.md +4 -2
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
use std::fmt::Write;
|
|
2
|
+
use std::fs::File;
|
|
3
|
+
use std::io::{BufRead, BufReader};
|
|
4
|
+
use std::path::Path;
|
|
5
|
+
|
|
6
|
+
use sha2::{Digest, Sha256};
|
|
7
|
+
|
|
8
|
+
use crate::shared::lib::{Failure, Outcome};
|
|
9
|
+
use crate::shared::protocol::{Media, Opened, Site, Source};
|
|
10
|
+
|
|
11
|
+
fn hex(bytes: &[u8]) -> String {
|
|
12
|
+
bytes.iter().fold(String::new(), |mut text, byte| {
|
|
13
|
+
let _ = write!(text, "{byte:02x}");
|
|
14
|
+
text
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
pub fn digest(path: &Path) -> Outcome<String> {
|
|
19
|
+
let mut reader = BufReader::new(File::open(path)?);
|
|
20
|
+
let mut hasher = Sha256::new();
|
|
21
|
+
loop {
|
|
22
|
+
let chunk = reader.fill_buf()?;
|
|
23
|
+
if chunk.is_empty() {
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
hasher.update(chunk);
|
|
27
|
+
let read = chunk.len();
|
|
28
|
+
reader.consume(read);
|
|
29
|
+
}
|
|
30
|
+
Ok(hex(&hasher.finalize()))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
pub fn opened_file(path: &Path) -> Outcome<Opened> {
|
|
34
|
+
let title = path
|
|
35
|
+
.file_name()
|
|
36
|
+
.and_then(|name| name.to_str())
|
|
37
|
+
.ok_or_else(|| Failure::new("the file has no name"))?
|
|
38
|
+
.to_owned();
|
|
39
|
+
Ok(Opened {
|
|
40
|
+
media: Media {
|
|
41
|
+
source: Source {
|
|
42
|
+
site: Site::File,
|
|
43
|
+
id: digest(path)?,
|
|
44
|
+
},
|
|
45
|
+
title,
|
|
46
|
+
artist: String::new(),
|
|
47
|
+
},
|
|
48
|
+
location: path.to_string_lossy().into_owned(),
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
#[cfg(test)]
|
|
53
|
+
mod tests {
|
|
54
|
+
use std::fs;
|
|
55
|
+
|
|
56
|
+
use super::{digest, opened_file};
|
|
57
|
+
use crate::shared::protocol::Site;
|
|
58
|
+
|
|
59
|
+
#[test]
|
|
60
|
+
fn digests_the_file_as_sha256() {
|
|
61
|
+
let directory = tempfile::tempdir().expect("temporary directory");
|
|
62
|
+
let file = directory.path().join("abc.txt");
|
|
63
|
+
fs::write(&file, b"abc").expect("file");
|
|
64
|
+
assert_eq!(
|
|
65
|
+
digest(&file).expect("digest"),
|
|
66
|
+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#[test]
|
|
71
|
+
fn names_a_file_by_its_contents() {
|
|
72
|
+
let directory = tempfile::tempdir().expect("temporary directory");
|
|
73
|
+
let first = directory.path().join("cut-v1.mp4");
|
|
74
|
+
let renamed = directory.path().join("cut-final.mp4");
|
|
75
|
+
fs::write(&first, b"frames").expect("first file");
|
|
76
|
+
fs::write(&renamed, b"frames").expect("renamed file");
|
|
77
|
+
let opened = opened_file(&first).expect("opened");
|
|
78
|
+
assert_eq!(opened.media.source.site, Site::File);
|
|
79
|
+
assert_eq!(
|
|
80
|
+
opened.media.source.id,
|
|
81
|
+
"594cfd2607d4f09e1ce6241203e418cacfed545063792bc5eab7afaadbf0c4e0"
|
|
82
|
+
);
|
|
83
|
+
assert_eq!(opened.media.title, "cut-v1.mp4");
|
|
84
|
+
assert_eq!(
|
|
85
|
+
opened_file(&renamed).expect("renamed").media.source,
|
|
86
|
+
opened.media.source
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
use std::path::Path;
|
|
2
|
+
|
|
3
|
+
use crate::shared::lib::Outcome;
|
|
4
|
+
use crate::shared::protocol::{McpAccess, Site};
|
|
5
|
+
|
|
6
|
+
pub struct MediaNotes {
|
|
7
|
+
pub client: reqwest::Client,
|
|
8
|
+
pub sites: &'static [Site],
|
|
9
|
+
pub mcp: McpAccess,
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
pub struct Setting<'a> {
|
|
13
|
+
pub name: &'static str,
|
|
14
|
+
pub version: &'static str,
|
|
15
|
+
pub data: &'a Path,
|
|
16
|
+
pub sites: &'static [Site],
|
|
17
|
+
pub port: u16,
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
pub fn media_notes(setting: &Setting<'_>) -> Outcome<MediaNotes> {
|
|
21
|
+
Ok(MediaNotes {
|
|
22
|
+
client: reqwest::Client::builder()
|
|
23
|
+
.user_agent(format!("{}/{}", setting.name, setting.version))
|
|
24
|
+
.build()?,
|
|
25
|
+
sites: setting.sites,
|
|
26
|
+
mcp: McpAccess {
|
|
27
|
+
url: desktop_shell::mcp_url(setting.port),
|
|
28
|
+
token: desktop_shell::mcp_token(setting.data)?,
|
|
29
|
+
},
|
|
30
|
+
})
|
|
31
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
use std::sync::LazyLock;
|
|
2
|
+
|
|
3
|
+
use regex::Regex;
|
|
4
|
+
use serde::Deserialize;
|
|
5
|
+
|
|
6
|
+
use super::link::page_of;
|
|
7
|
+
use crate::shared::lib::{Failure, Outcome};
|
|
8
|
+
use crate::shared::protocol::{Media, Site, Source};
|
|
9
|
+
|
|
10
|
+
const YOUTUBE_OEMBED: &str = "https://www.youtube.com/oembed";
|
|
11
|
+
const SOUNDCLOUD_OEMBED: &str = "https://soundcloud.com/oembed";
|
|
12
|
+
const NICONICO_INFO: &str = "https://ext.nicovideo.jp/api/getthumbinfo/";
|
|
13
|
+
const NICONICO_FOUND: &str = r#"<nicovideo_thumb_response status="ok">"#;
|
|
14
|
+
|
|
15
|
+
static NICONICO_TITLE: LazyLock<Regex> =
|
|
16
|
+
LazyLock::new(|| Regex::new(r"<title>([^<]*)</title>").expect("valid title pattern"));
|
|
17
|
+
static NICONICO_ARTIST: LazyLock<Regex> = LazyLock::new(|| {
|
|
18
|
+
Regex::new(r"<(?:user_nickname|ch_name)>([^<]*)</").expect("valid artist pattern")
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
#[derive(Deserialize)]
|
|
22
|
+
struct OEmbed {
|
|
23
|
+
title: String,
|
|
24
|
+
author_name: String,
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
struct Credit {
|
|
28
|
+
title: String,
|
|
29
|
+
artist: String,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
fn unescaped(text: &str) -> String {
|
|
33
|
+
text.replace("<", "<")
|
|
34
|
+
.replace(">", ">")
|
|
35
|
+
.replace(""", "\"")
|
|
36
|
+
.replace("'", "'")
|
|
37
|
+
.replace("&", "&")
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
fn captured(pattern: &Regex, text: &str) -> String {
|
|
41
|
+
pattern
|
|
42
|
+
.captures(text)
|
|
43
|
+
.and_then(|found| found.get(1))
|
|
44
|
+
.map_or_else(String::new, |found| unescaped(found.as_str()))
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
fn niconico_credit(text: &str) -> Outcome<Credit> {
|
|
48
|
+
if !text.contains(NICONICO_FOUND) {
|
|
49
|
+
return Err(Failure::new("niconico has no such video"));
|
|
50
|
+
}
|
|
51
|
+
Ok(Credit {
|
|
52
|
+
title: captured(&NICONICO_TITLE, text),
|
|
53
|
+
artist: captured(&NICONICO_ARTIST, text),
|
|
54
|
+
})
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async fn oembed(client: &reqwest::Client, endpoint: &str, source: &Source) -> Outcome<Credit> {
|
|
58
|
+
let page = page_of(source).ok_or_else(|| Failure::new("a file has no page"))?;
|
|
59
|
+
let found: OEmbed = client
|
|
60
|
+
.get(endpoint)
|
|
61
|
+
.query(&[("format", "json"), ("url", &page)])
|
|
62
|
+
.send()
|
|
63
|
+
.await?
|
|
64
|
+
.error_for_status()?
|
|
65
|
+
.json()
|
|
66
|
+
.await?;
|
|
67
|
+
Ok(Credit {
|
|
68
|
+
title: found.title,
|
|
69
|
+
artist: found.author_name,
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async fn niconico(client: &reqwest::Client, source: &Source) -> Outcome<Credit> {
|
|
74
|
+
let text = client
|
|
75
|
+
.get(format!("{NICONICO_INFO}{}", source.id))
|
|
76
|
+
.send()
|
|
77
|
+
.await?
|
|
78
|
+
.error_for_status()?
|
|
79
|
+
.text()
|
|
80
|
+
.await?;
|
|
81
|
+
niconico_credit(&text)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async fn credit(client: &reqwest::Client, source: &Source) -> Outcome<Credit> {
|
|
85
|
+
match source.site {
|
|
86
|
+
Site::Youtube => oembed(client, YOUTUBE_OEMBED, source).await,
|
|
87
|
+
Site::Soundcloud => oembed(client, SOUNDCLOUD_OEMBED, source).await,
|
|
88
|
+
Site::Niconico => niconico(client, source).await,
|
|
89
|
+
Site::File => Err(Failure::new("a file has no credit online")),
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
pub async fn media_of(client: &reqwest::Client, source: Source) -> Media {
|
|
94
|
+
match credit(client, &source).await {
|
|
95
|
+
Ok(Credit { title, artist }) => Media {
|
|
96
|
+
source,
|
|
97
|
+
title,
|
|
98
|
+
artist,
|
|
99
|
+
},
|
|
100
|
+
Err(failure) => {
|
|
101
|
+
eprintln!("credit lookup failed: {failure}");
|
|
102
|
+
Media {
|
|
103
|
+
title: source.id.clone(),
|
|
104
|
+
source,
|
|
105
|
+
artist: String::new(),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#[cfg(test)]
|
|
112
|
+
mod tests {
|
|
113
|
+
use super::niconico_credit;
|
|
114
|
+
|
|
115
|
+
#[test]
|
|
116
|
+
fn reads_niconico_credits() {
|
|
117
|
+
let credit = niconico_credit(
|
|
118
|
+
r#"<nicovideo_thumb_response status="ok"><thumb><title>Tom & Jerry's</title><user_nickname>作者</user_nickname></thumb>"#,
|
|
119
|
+
)
|
|
120
|
+
.expect("credit");
|
|
121
|
+
assert_eq!(credit.title, "Tom & Jerry's");
|
|
122
|
+
assert_eq!(credit.artist, "作者");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
#[test]
|
|
126
|
+
fn refuses_pages_that_are_not_thumb_info() {
|
|
127
|
+
assert!(niconico_credit("<html><title>ニコニコ動画</title></html>").is_err());
|
|
128
|
+
}
|
|
129
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
use std::sync::LazyLock;
|
|
2
|
+
|
|
3
|
+
use regex::Regex;
|
|
4
|
+
use url::Url;
|
|
5
|
+
|
|
6
|
+
use crate::shared::protocol::{Site, Source};
|
|
7
|
+
|
|
8
|
+
const SECONDS_PER_MINUTE: u64 = 60;
|
|
9
|
+
const NESTED_YOUTUBE: [&str; 3] = ["shorts", "live", "embed"];
|
|
10
|
+
const HOST_PREFIXES: [&str; 4] = ["www.", "m.", "music.", "sp."];
|
|
11
|
+
|
|
12
|
+
static YOUTUBE_ID: LazyLock<Regex> =
|
|
13
|
+
LazyLock::new(|| Regex::new(r"^[\w-]{11}$").expect("valid YouTube id pattern"));
|
|
14
|
+
static NICONICO_ID: LazyLock<Regex> =
|
|
15
|
+
LazyLock::new(|| Regex::new(r"^(?:sm|so|nm)\d+$").expect("valid niconico id pattern"));
|
|
16
|
+
|
|
17
|
+
fn accepted(site: Site, id: Option<&str>, pattern: &Regex) -> Option<Source> {
|
|
18
|
+
id.filter(|value| pattern.is_match(value))
|
|
19
|
+
.map(|value| Source {
|
|
20
|
+
site,
|
|
21
|
+
id: value.to_owned(),
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
fn youtube_id<'a>(parts: &[&'a str], video: Option<&'a str>) -> Option<&'a str> {
|
|
26
|
+
match parts.first() {
|
|
27
|
+
Some(&"watch") => video,
|
|
28
|
+
Some(first) if NESTED_YOUTUBE.contains(first) => parts.get(1).copied(),
|
|
29
|
+
_ => None,
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
fn bare_host(host: &str) -> &str {
|
|
34
|
+
HOST_PREFIXES
|
|
35
|
+
.iter()
|
|
36
|
+
.find_map(|prefix| host.strip_prefix(prefix))
|
|
37
|
+
.unwrap_or(host)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#[must_use]
|
|
41
|
+
pub fn source_of(text: &str) -> Option<Source> {
|
|
42
|
+
let url = Url::parse(text.trim()).ok()?;
|
|
43
|
+
let host = bare_host(url.host_str()?);
|
|
44
|
+
let parts: Vec<&str> = url.path_segments().map_or_else(Vec::new, |segments| {
|
|
45
|
+
segments.filter(|part| !part.is_empty()).collect()
|
|
46
|
+
});
|
|
47
|
+
let video = url
|
|
48
|
+
.query_pairs()
|
|
49
|
+
.find(|(key, _)| key == "v")
|
|
50
|
+
.map(|(_, value)| value.into_owned());
|
|
51
|
+
match host {
|
|
52
|
+
"youtube.com" => accepted(
|
|
53
|
+
Site::Youtube,
|
|
54
|
+
youtube_id(&parts, video.as_deref()),
|
|
55
|
+
&YOUTUBE_ID,
|
|
56
|
+
),
|
|
57
|
+
"youtu.be" => accepted(Site::Youtube, parts.first().copied(), &YOUTUBE_ID),
|
|
58
|
+
"nicovideo.jp" => accepted(Site::Niconico, parts.get(1).copied(), &NICONICO_ID),
|
|
59
|
+
"nico.ms" => accepted(Site::Niconico, parts.first().copied(), &NICONICO_ID),
|
|
60
|
+
"soundcloud.com" if parts.len() == 2 => Some(Source {
|
|
61
|
+
site: Site::Soundcloud,
|
|
62
|
+
id: parts.join("/"),
|
|
63
|
+
}),
|
|
64
|
+
_ => None,
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
#[must_use]
|
|
69
|
+
pub fn page_of(source: &Source) -> Option<String> {
|
|
70
|
+
match source.site {
|
|
71
|
+
Site::Youtube => Some(format!("https://www.youtube.com/watch?v={}", source.id)),
|
|
72
|
+
Site::Niconico => Some(format!("https://www.nicovideo.jp/watch/{}", source.id)),
|
|
73
|
+
Site::Soundcloud => Some(format!("https://soundcloud.com/{}", source.id)),
|
|
74
|
+
Site::File => None,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
#[expect(
|
|
79
|
+
clippy::cast_possible_truncation,
|
|
80
|
+
clippy::cast_sign_loss,
|
|
81
|
+
reason = "only finite positive seconds reach the cast"
|
|
82
|
+
)]
|
|
83
|
+
fn whole(seconds: f64) -> u64 {
|
|
84
|
+
if seconds.is_finite() && seconds > 0.0 {
|
|
85
|
+
seconds.floor() as u64
|
|
86
|
+
} else {
|
|
87
|
+
0
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#[must_use]
|
|
92
|
+
pub fn clock(seconds: f64) -> String {
|
|
93
|
+
let total = whole(seconds);
|
|
94
|
+
format!(
|
|
95
|
+
"{}:{:02}",
|
|
96
|
+
total / SECONDS_PER_MINUTE,
|
|
97
|
+
total % SECONDS_PER_MINUTE
|
|
98
|
+
)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
#[must_use]
|
|
102
|
+
pub fn range(from: f64, to: f64) -> String {
|
|
103
|
+
format!("{}–{}", clock(from), clock(to))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
#[must_use]
|
|
107
|
+
pub fn link_at(source: &Source, seconds: f64) -> Option<String> {
|
|
108
|
+
match source.site {
|
|
109
|
+
Site::Youtube => Some(format!(
|
|
110
|
+
"https://youtu.be/{}?t={}",
|
|
111
|
+
source.id,
|
|
112
|
+
whole(seconds)
|
|
113
|
+
)),
|
|
114
|
+
Site::Niconico => Some(format!(
|
|
115
|
+
"https://www.nicovideo.jp/watch/{}?from={}",
|
|
116
|
+
source.id,
|
|
117
|
+
whole(seconds)
|
|
118
|
+
)),
|
|
119
|
+
Site::Soundcloud => Some(format!(
|
|
120
|
+
"https://soundcloud.com/{}#t={}",
|
|
121
|
+
source.id,
|
|
122
|
+
clock(seconds)
|
|
123
|
+
)),
|
|
124
|
+
Site::File => None,
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#[cfg(test)]
|
|
129
|
+
mod tests {
|
|
130
|
+
use super::{clock, link_at, page_of, range, source_of};
|
|
131
|
+
use crate::shared::protocol::{Site, Source};
|
|
132
|
+
|
|
133
|
+
fn source(site: Site, id: &str) -> Source {
|
|
134
|
+
Source {
|
|
135
|
+
site,
|
|
136
|
+
id: id.to_owned(),
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
#[test]
|
|
141
|
+
fn reads_every_site_link() {
|
|
142
|
+
let youtube = Some(source(Site::Youtube, "dQw4w9WgXcQ"));
|
|
143
|
+
assert_eq!(
|
|
144
|
+
source_of("https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=3"),
|
|
145
|
+
youtube
|
|
146
|
+
);
|
|
147
|
+
assert_eq!(source_of("https://youtu.be/dQw4w9WgXcQ"), youtube);
|
|
148
|
+
assert_eq!(
|
|
149
|
+
source_of("https://m.youtube.com/shorts/dQw4w9WgXcQ"),
|
|
150
|
+
youtube
|
|
151
|
+
);
|
|
152
|
+
assert_eq!(
|
|
153
|
+
source_of("https://music.youtube.com/watch?v=dQw4w9WgXcQ"),
|
|
154
|
+
youtube
|
|
155
|
+
);
|
|
156
|
+
let niconico = Some(source(Site::Niconico, "sm9"));
|
|
157
|
+
assert_eq!(source_of("https://www.nicovideo.jp/watch/sm9"), niconico);
|
|
158
|
+
assert_eq!(source_of("https://sp.nicovideo.jp/watch/sm9"), niconico);
|
|
159
|
+
assert_eq!(source_of("https://nico.ms/sm9"), niconico);
|
|
160
|
+
assert_eq!(
|
|
161
|
+
source_of(" https://soundcloud.com/artist/track "),
|
|
162
|
+
Some(source(Site::Soundcloud, "artist/track"))
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
#[test]
|
|
167
|
+
fn refuses_links_that_are_not_media() {
|
|
168
|
+
assert_eq!(source_of("https://www.youtube.com/@channel"), None);
|
|
169
|
+
assert_eq!(source_of("https://soundcloud.com/artist"), None);
|
|
170
|
+
assert_eq!(source_of("https://example.com/watch?v=dQw4w9WgXcQ"), None);
|
|
171
|
+
assert_eq!(source_of("https://www.youtube.com/watch?v=short"), None);
|
|
172
|
+
assert_eq!(source_of("https://www.nicovideo.jp/watch/lv123"), None);
|
|
173
|
+
assert_eq!(source_of("not a link"), None);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
#[test]
|
|
177
|
+
fn links_to_the_moment() {
|
|
178
|
+
let song = source(Site::Soundcloud, "artist/track");
|
|
179
|
+
assert_eq!(clock(83.9), "1:23");
|
|
180
|
+
assert_eq!(clock(-4.0), "0:00");
|
|
181
|
+
assert_eq!(range(61.5, 75.0), "1:01–1:15");
|
|
182
|
+
assert_eq!(
|
|
183
|
+
link_at(&song, 83.9).as_deref(),
|
|
184
|
+
Some("https://soundcloud.com/artist/track#t=1:23")
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#[test]
|
|
189
|
+
fn files_have_no_page() {
|
|
190
|
+
let file = source(Site::File, "abc");
|
|
191
|
+
assert_eq!(page_of(&file), None);
|
|
192
|
+
assert_eq!(link_at(&file, 3.0), None);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
use std::path::{Path, PathBuf};
|
|
2
|
+
|
|
3
|
+
use tauri::{AppHandle, Manager, State, async_runtime};
|
|
4
|
+
use tauri_plugin_dialog::DialogExt;
|
|
5
|
+
|
|
6
|
+
use crate::entities::media_file::opened_file;
|
|
7
|
+
use crate::entities::media_notes::MediaNotes;
|
|
8
|
+
use crate::entities::source::{media_of, source_of};
|
|
9
|
+
use crate::shared::lib::{Failure, Outcome};
|
|
10
|
+
use crate::shared::protocol::{Opened, Site};
|
|
11
|
+
|
|
12
|
+
const NOT_MEDIA: &str = "開けるリンクではありません";
|
|
13
|
+
const NO_FILES: &str = "このアプリはファイルを開けません";
|
|
14
|
+
const MISSING_FILE: &str = "ファイルが見つかりません";
|
|
15
|
+
const MEDIA_FILES: &str = "動画と音声";
|
|
16
|
+
const MEDIA_EXTENSIONS: [&str; 11] = [
|
|
17
|
+
"mp4", "m4v", "mov", "webm", "mkv", "mp3", "m4a", "aac", "wav", "flac", "ogg",
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
type Answer<T> = Result<T, Failure>;
|
|
21
|
+
|
|
22
|
+
#[tauri::command]
|
|
23
|
+
#[specta::specta]
|
|
24
|
+
pub async fn open_link(notes: State<'_, MediaNotes>, link: String) -> Answer<Opened> {
|
|
25
|
+
let source = source_of(&link)
|
|
26
|
+
.filter(|source| notes.sites.contains(&source.site))
|
|
27
|
+
.ok_or_else(|| Failure::new(NOT_MEDIA))?;
|
|
28
|
+
Ok(Opened {
|
|
29
|
+
media: media_of(¬es.client, source).await,
|
|
30
|
+
location: link.trim().to_owned(),
|
|
31
|
+
})
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fn readable(path: &Path) -> Outcome<()> {
|
|
35
|
+
if path.is_file() {
|
|
36
|
+
Ok(())
|
|
37
|
+
} else {
|
|
38
|
+
Err(Failure::new(MISSING_FILE))
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
fn failed(failure: impl std::fmt::Display) -> Failure {
|
|
43
|
+
Failure::new(failure.to_string())
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async fn opened_at(app: &AppHandle, sites: &[Site], path: PathBuf) -> Outcome<Opened> {
|
|
47
|
+
if !sites.contains(&Site::File) {
|
|
48
|
+
return Err(Failure::new(NO_FILES));
|
|
49
|
+
}
|
|
50
|
+
readable(&path)?;
|
|
51
|
+
let opened = async_runtime::spawn_blocking(move || opened_file(&path))
|
|
52
|
+
.await
|
|
53
|
+
.map_err(failed)??;
|
|
54
|
+
app.asset_protocol_scope()
|
|
55
|
+
.allow_file(&opened.location)
|
|
56
|
+
.map_err(failed)?;
|
|
57
|
+
Ok(opened)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
#[tauri::command]
|
|
61
|
+
#[specta::specta]
|
|
62
|
+
pub async fn open_file(
|
|
63
|
+
app: AppHandle,
|
|
64
|
+
notes: State<'_, MediaNotes>,
|
|
65
|
+
path: String,
|
|
66
|
+
) -> Answer<Opened> {
|
|
67
|
+
opened_at(&app, notes.sites, PathBuf::from(path)).await
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
#[tauri::command]
|
|
71
|
+
#[specta::specta]
|
|
72
|
+
pub async fn choose_file(app: AppHandle, notes: State<'_, MediaNotes>) -> Answer<Option<Opened>> {
|
|
73
|
+
let asking = app.clone();
|
|
74
|
+
let chosen = async_runtime::spawn_blocking(move || {
|
|
75
|
+
asking
|
|
76
|
+
.dialog()
|
|
77
|
+
.file()
|
|
78
|
+
.add_filter(MEDIA_FILES, &MEDIA_EXTENSIONS)
|
|
79
|
+
.blocking_pick_file()
|
|
80
|
+
})
|
|
81
|
+
.await
|
|
82
|
+
.map_err(failed)?;
|
|
83
|
+
let Some(chosen) = chosen else {
|
|
84
|
+
return Ok(None);
|
|
85
|
+
};
|
|
86
|
+
let path = chosen.into_path().map_err(failed)?;
|
|
87
|
+
Ok(Some(opened_at(&app, notes.sites, path).await?))
|
|
88
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
use tauri::State;
|
|
2
|
+
|
|
3
|
+
use crate::entities::media_notes::MediaNotes;
|
|
4
|
+
use crate::shared::lib::Failure;
|
|
5
|
+
use crate::shared::protocol::McpAccess;
|
|
6
|
+
|
|
7
|
+
#[tauri::command]
|
|
8
|
+
#[specta::specta]
|
|
9
|
+
#[expect(
|
|
10
|
+
clippy::needless_pass_by_value,
|
|
11
|
+
reason = "Tauri hands managed state to commands by value"
|
|
12
|
+
)]
|
|
13
|
+
pub fn mcp_access(notes: State<'_, MediaNotes>) -> Result<McpAccess, Failure> {
|
|
14
|
+
Ok(notes.mcp.clone())
|
|
15
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
mod entities;
|
|
2
|
+
mod features;
|
|
3
|
+
mod shared;
|
|
4
|
+
|
|
5
|
+
pub use entities::media_file::{digest, opened_file};
|
|
6
|
+
pub use entities::media_notes::{MediaNotes, Setting, media_notes};
|
|
7
|
+
pub use entities::source::{clock, link_at, media_of, page_of, range, source_of};
|
|
8
|
+
pub use features::open_media::{choose_file, open_file, open_link};
|
|
9
|
+
pub use features::share_mcp_access::mcp_access;
|
|
10
|
+
pub use shared::db::{Row, Store, fresh_id, is_id, now};
|
|
11
|
+
pub use shared::lib::{Failure, Outcome, answer};
|
|
12
|
+
pub use shared::protocol::{McpAccess, Media, Opened, Saved, Site, Source};
|