Skip to content

Commit e71b99a

Browse files
committed
feat(helpers): use inputstreams for videos
1 parent 88b285d commit e71b99a

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

src/Services/Helpers/Video.vala

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
public class Tuba.Helper.Video {
2+
private static Soup.Session session;
3+
private static Soup.Cache cache;
4+
5+
public static void clear_cache () {
6+
new Helper.Video ();
7+
cache.clear ();
8+
}
9+
10+
public static void flush_cache () {
11+
new Helper.Video ();
12+
cache.flush ();
13+
cache.dump ();
14+
}
15+
16+
static construct {
17+
cache = new Soup.Cache (
18+
GLib.Path.build_path (GLib.Path.DIR_SEPARATOR_S, Tuba.cache_path, "soup", "videos"),
19+
Soup.CacheType.SINGLE_USER
20+
);
21+
cache.load ();
22+
cache.set_max_size (1024 * 1024 * 100 * 2);
23+
24+
session = new Soup.Session.with_options ("max-conns", 64, "max-conns-per-host", 64) {
25+
user_agent = @"$(Build.NAME)/$(Build.VERSION) libsoup/$(Soup.get_major_version()).$(Soup.get_minor_version()).$(Soup.get_micro_version()) ($(Soup.MAJOR_VERSION).$(Soup.MINOR_VERSION).$(Soup.MICRO_VERSION))" // vala-lint=line-length
26+
};
27+
session.add_feature (cache);
28+
}
29+
30+
public static async InputStream request (string? url) throws Oopsie {
31+
if (url == null || url == "") throw new Tuba.Oopsie.INTERNAL ("No url provided");
32+
new Helper.Video ();
33+
34+
var download_msg = new Soup.Message ("GET", url);
35+
try {
36+
return yield session.send_async (download_msg, 0, null);
37+
} catch (Error e) {
38+
throw new Tuba.Oopsie.INTERNAL (@"Failed to get video at \"$url\": $(e.message)");
39+
}
40+
}
41+
}

src/Services/Helpers/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ sources += files(
22
'Blurhash.vala',
33
'Entity.vala',
44
'Image.vala',
5+
'Video.vala',
56
)

src/Views/MediaViewer.vala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -843,8 +843,16 @@ public class Tuba.Views.MediaViewer : Gtk.Widget, Gtk.Buildable, Adw.Swipeable {
843843
item = new Item (video, final_friendly_url, final_preview, true);
844844

845845
if (stream) {
846-
File file = File.new_for_uri (url);
847-
video.set_file (file);
846+
Helper.Video.request.begin (url, (obj, res) => {
847+
try {
848+
video.set_media_stream (Gtk.MediaFile.for_input_stream (Helper.Video.request.end (res)));
849+
add_todo_item (item);
850+
} catch (Oopsie e) {
851+
warning (e.message);
852+
var dlg = app.inform (_("Error"), e.message);
853+
dlg.present (app.main_window);
854+
}
855+
});
848856
} else if (!as_is) {
849857
download_video.begin (url, (obj, res) => {
850858
try {

0 commit comments

Comments
 (0)