Fix thumbnails and file previews on Linux (#635)

* Fix thumbnails and file previews on Linux
 - Restructure auth_middleware to use axum's primitives to handle the auth tokens
 - Fix incorrect token loading in App.tsx
 - Add required dependencies for video playback to the Linux setup script
 - Minor improvements to the Linux setup script, make it less error prone
 - Fix a minor bug in the script_failure function in the Linux setup script

* Use a more idiomatic rust syntax in auth_middleware
This commit is contained in:
Vítor Vasconcellos 2023-03-28 18:19:11 -03:00 committed by GitHub
parent 20f0e7f15a
commit c84e28189c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 37 deletions

View file

@ -1,19 +1,17 @@
#!/bin/bash
#!/usr/bin/env bash
set -e
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
set -euo pipefail
function log_err() {
echo "$@" >&2
}
function script_failure() {
log_err "An error occurred$([ -z "$1" ] && " on line $1" || " (unknown)")."
log_err "An error occurred $(if [ -n "${1:-}" ]; then echo "on line $1"; else echo "(unknown)"; fi)."
log_err "Setup failed."
}
trap 'script_failure $LINENO' ERR
trap 'script_failure ${LINENO:-}' ERR
echo "Setting up this system for Spacedrive development."
echo
@ -37,18 +35,18 @@ else
echo "Skipping pnpm check."
fi
if [ "$CI" != "true" ]; then
if [ "${CI:-}" != "true" ]; then
echo "Installing Rust tools"
cargo install cargo-watch
fi
echo
if [ "$1" == "mobile" ]; then
if [ "${1:-}" == "mobile" ]; then
echo "Setting up for mobile development."
# iOS targets
if [[ "$OSTYPE" == "darwin"* ]]; then
if [[ $OSTYPE == "darwin"* ]]; then
echo "Checking for Xcode..."
if ! /usr/bin/xcodebuild -version >/dev/null; then
log_err "Xcode was not detected."
@ -86,7 +84,7 @@ if [ "$1" == "mobile" ]; then
echo
fi
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
if [[ $OSTYPE == "linux-gnu"* ]]; then
if command -v apt-get >/dev/null; then
echo "Detected apt!"
echo "Installing dependencies with apt..."
@ -97,6 +95,9 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# FFmpeg dependencies
DEBIAN_FFMPEG_DEPS="libavcodec-dev libavdevice-dev libavfilter-dev libavformat-dev libavutil-dev libswscale-dev libswresample-dev ffmpeg"
# Webkit2gtk requires gstreamer plugins for video playback to work
DEBIAN_VIDEO_DEPS="gstreamer1.0-libav gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly"
# Bindgen dependencies - it's used by a dependency of Spacedrive
DEBIAN_BINDGEN_DEPS="pkg-config clang"
@ -104,7 +105,7 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
DEBIAN_LIBP2P_DEPS="protobuf-compiler"
sudo apt-get -y update
sudo apt-get -y install ${SPACEDRIVE_CUSTOM_APT_FLAGS:-} $DEBIAN_TAURI_DEPS $DEBIAN_FFMPEG_DEPS $DEBIAN_BINDGEN_DEPS $DEBIAN_LIBP2P_DEPS
sudo apt-get -y install ${SPACEDRIVE_CUSTOM_APT_FLAGS:-} $DEBIAN_TAURI_DEPS $DEBIAN_FFMPEG_DEPS $DEBIAN_BINDGEN_DEPS $DEBIAN_LIBP2P_DEPS $DEBIAN_VIDEO_DEPS
elif command -v pacman >/dev/null; then
echo "Detected pacman!"
echo "Installing dependencies with pacman..."
@ -112,6 +113,9 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Tauri deps https://tauri.studio/guides/getting-started/setup/linux#1-system-dependencies
ARCH_TAURI_DEPS="webkit2gtk base-devel curl wget openssl appmenu-gtk-module gtk3 libappindicator-gtk3 librsvg libvips"
# Webkit2gtk requires gstreamer plugins for video playback to work
ARCH_VIDEO_DEPS="gst-libav gst-plugins-base gst-plugins-good gst-plugins-bad gst-plugins-ugly"
# FFmpeg dependencies
ARCH_FFMPEG_DEPS="ffmpeg"
@ -122,7 +126,7 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
ARCH_LIBP2P_DEPS="protobuf"
sudo pacman -Sy
sudo pacman -S --needed $ARCH_TAURI_DEPS $ARCH_FFMPEG_DEPS $ARCH_BINDGEN_DEPS $ARCH_LIBP2P_DEPS
sudo pacman -S --needed $ARCH_TAURI_DEPS $ARCH_FFMPEG_DEPS $ARCH_BINDGEN_DEPS $ARCH_LIBP2P_DEPS $ARCH_VIDEO_DEPS
elif command -v dnf >/dev/null; then
echo "Detected dnf!"
echo "Installing dependencies with dnf..."
@ -144,6 +148,9 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# FFmpeg dependencies
FEDORA_FFMPEG_DEPS="ffmpeg ffmpeg-devel"
# Webkit2gtk requires gstreamer plugins for video playback to work
FEDORA_VIDEO_DEPS="gstreamer1-plugin-libav gstreamer1-plugins-base gstreamer1-plugins-good gstreamer1-plugins-good-extras gstreamer1-plugins-bad-free gstreamer1-plugins-bad-free-extras gstreamer1-plugins-ugly-free"
# Bindgen dependencies - it's used by a dependency of Spacedrive
FEDORA_BINDGEN_DEPS="clang"
@ -162,13 +169,13 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
exit 1
fi
sudo dnf install $FEDORA_TAURI_DEPS $FEDORA_BINDGEN_DEPS $FEDORA_LIBP2P_DEPS
sudo dnf install $FEDORA_TAURI_DEPS $FEDORA_BINDGEN_DEPS $FEDORA_LIBP2P_DEPS $FEDORA_VIDEO_DEPS
sudo dnf group install "C Development Tools and Libraries"
else
log_err "Your Linux distro '$(lsb_release -s -d)' is not supported by this script. We would welcome a PR or some help adding your OS to this script. https://github.com/spacedriveapp/spacedrive/issues"
exit 1
fi
elif [[ "$OSTYPE" == "darwin"* ]]; then
elif [[ $OSTYPE == "darwin"* ]]; then
if ! command -v brew >/dev/null; then
log_err "Homebrew was not found. Please install it using the instructions at https://brew.sh and try again."
exit 1

27
Cargo.lock generated
View file

@ -454,6 +454,7 @@ dependencies = [
"bitflags",
"bytes",
"futures-util",
"headers",
"http",
"http-body",
"hyper",
@ -2835,6 +2836,31 @@ dependencies = [
"hashbrown 0.11.2",
]
[[package]]
name = "headers"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3e372db8e5c0d213e0cd0b9be18be2aca3d44cf2fe30a9d46a65581cd454584"
dependencies = [
"base64 0.13.1",
"bitflags",
"bytes",
"headers-core",
"http",
"httpdate",
"mime",
"sha1",
]
[[package]]
name = "headers-core"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429"
dependencies = [
"http",
]
[[package]]
name = "heck"
version = "0.3.3"
@ -7297,7 +7323,6 @@ dependencies = [
"tauri-build",
"tokio",
"tracing",
"url",
"window-shadows",
]

View file

@ -22,9 +22,8 @@ http = "0.2.8"
[target.'cfg(target_os = "linux")'.dependencies]
server = { path = "../../server" }
axum = "0.6.4"
axum = { version = "0.6.4", features = ["headers", "query"] }
rand = "0.8.5"
url = "2.1.1"
[target.'cfg(target_os = "macos")'.dependencies]
sd-desktop-macos.path = "../crates/macos"

View file

@ -6,17 +6,19 @@ use std::{
use sd_core::Node;
use axum::{
extract::State,
extract::{Query, State, TypedHeader},
headers::authorization::{Authorization, Bearer},
http::{Request, StatusCode},
middleware::{self, Next},
response::{IntoResponse, Response},
response::Response,
routing::get,
RequestPartsExt,
};
use httpz::{Endpoint, HttpEndpoint};
use rand::{distributions::Alphanumeric, Rng};
use serde::Deserialize;
use tauri::{plugin::TauriPlugin, Builder, Runtime};
use tracing::debug;
use url::Url;
pub(super) async fn setup<R: Runtime>(
app: Builder<R>,
@ -60,27 +62,38 @@ pub(super) async fn setup<R: Runtime>(
app.plugin(tauri_plugin(&auth_token, listen_addr))
}
#[derive(Deserialize)]
struct QueryToken {
token: String,
}
async fn auth_middleware<B>(
Query(query): Query<QueryToken>,
State(auth_token): State<String>,
request: Request<B>,
next: Next<B>,
) -> Response {
let url = Url::parse(&request.uri().to_string()).unwrap();
if let Some((_, v)) = url.query_pairs().find(|(k, _)| k == "token") {
if v == auth_token {
return next.run(request).await;
}
} else if let Some(v) = request
.headers()
.get("Authorization")
.and_then(|v| v.to_str().ok())
{
if v == auth_token {
return next.run(request).await;
}
}
) -> Result<Response, StatusCode>
where
B: Send,
{
let req = if query.token != auth_token {
let (mut parts, body) = request.into_parts();
(StatusCode::UNAUTHORIZED, "Unauthorized!").into_response()
let auth: TypedHeader<Authorization<Bearer>> = parts
.extract()
.await
.map_err(|_| StatusCode::UNAUTHORIZED)?;
if auth.token() != auth_token {
return Err(StatusCode::UNAUTHORIZED);
}
Request::from_parts(parts, body)
} else {
request
};
Ok(next.run(req).await)
}
fn tauri_plugin<R: Runtime>(auth_token: &str, listen_addr: SocketAddr) -> TauriPlugin<R> {

View file

@ -41,7 +41,7 @@ async function getOs(): Promise<OperatingSystem> {
}
let customUriServerUrl = (window as any).__SD_CUSTOM_URI_SERVER__ as string | undefined;
const customUriAuthToken = (window as any).__SD_CUSTOM_URI_TOKEN__ as string | undefined;
const customUriAuthToken = (window as any).__SD_CUSTOM_SERVER_AUTH_TOKEN__ as string | undefined;
const startupError = (window as any).__SD_ERROR__ as string | undefined;
if (customUriServerUrl && !customUriServerUrl?.endsWith('/')) {