Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[space][web][rust] Add short video posts

Anand Baburajan0a16cb136200
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 24c26446a1..fa9c795eee 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2981,6 +2981,7 @@ dependencies = [
"ente-core",
"ente-space",
"ente-wasm-lib",
+ "futures-util",
"js-sys",
"log",
"serde",
@@ -2990,6 +2991,7 @@ dependencies = [
"tsify",
"wasm-bindgen",
"wasm-bindgen-futures",
+ "web-sys",
]
[[package]]
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 3609dc2fa1..5acb28a373 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -120,6 +120,7 @@ urlencoding = "2.1.3"
uuid = "1.23.1"
wasm-bindgen = "0.2.125"
wasm-bindgen-futures = "0.4.75"
+web-sys = "0.3.102"
zeroize = { version = "1.9.0", features = ["derive"] }
zip = { version = "8.6.0", default-features = false, features = ["deflate-flate2-zlib-rs"] }
diff --git a/rust/bindings/wasm/lib/Cargo.toml b/rust/bindings/wasm/lib/Cargo.toml
index f74e91cd01..0aa0139cc6 100644
--- a/rust/bindings/wasm/lib/Cargo.toml
+++ b/rust/bindings/wasm/lib/Cargo.toml
@@ -42,7 +42,7 @@ thiserror.workspace = true
tsify = { workspace = true, optional = true }
wasm-bindgen.workspace = true
wasm-bindgen-futures = { workspace = true, optional = true }
-web-sys = { version = "0.3.102", features = ["console"] }
+web-sys = { workspace = true, features = ["console"] }
[lints]
workspace = true
diff --git a/rust/bindings/wasm/space/Cargo.toml b/rust/bindings/wasm/space/Cargo.toml
index af9161c97a..40b5f33bce 100644
--- a/rust/bindings/wasm/space/Cargo.toml
+++ b/rust/bindings/wasm/space/Cargo.toml
@@ -9,6 +9,7 @@ crate-type = ["cdylib"]
ente-core.workspace = true
ente-space.workspace = true
ente-wasm-lib = { workspace = true, features = ["crypto"] }
+futures-util.workspace = true
js-sys.workspace = true
log.workspace = true
serde.workspace = true
@@ -17,6 +18,7 @@ thiserror.workspace = true
tsify.workspace = true
wasm-bindgen.workspace = true
wasm-bindgen-futures.workspace = true
+web-sys = { workspace = true, features = ["AbortSignal"] }
[dev-dependencies]
tokio = { workspace = true, features = ["macros", "rt"] }
diff --git a/rust/bindings/wasm/space/src/space.rs b/rust/bindings/wasm/space/src/space.rs
index dc3169808d..d35875ad71 100644
--- a/rust/bindings/wasm/space/src/space.rs
+++ b/rust/bindings/wasm/space/src/space.rs
@@ -1,11 +1,35 @@
use std::collections::BTreeMap;
+use std::future::Future;
use ente_core::b64;
use ente_space::{AccountSpaceCtx, SpaceLinkCtx};
+use futures_util::future::{AbortHandle, Abortable};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen as swb;
use tsify::Tsify;
use wasm_bindgen::prelude::*;
+use web_sys::AbortSignal;
+
+async fn with_abort_signal<T>(
+ future: impl Future<Output = Result<T, ente_space::Error>>,
+ signal: Option<AbortSignal>,
+) -> Result<T, JsValue> {
+ let Some(signal) = signal else {
+ return future.await.map_err(|error| Error::from(error).into());
+ };
+ if signal.aborted() {
+ return Err(signal.reason());
+ }
+ let (handle, registration) = AbortHandle::new_pair();
+ let on_abort = Closure::<dyn FnMut()>::new(move || handle.abort());
+ signal.add_event_listener_with_callback("abort", on_abort.as_ref().unchecked_ref())?;
+ let result = Abortable::new(future, registration).await;
+ signal.remove_event_listener_with_callback("abort", on_abort.as_ref().unchecked_ref())?;
+ match result {
+ Ok(result) => result.map_err(|error| Error::from(error).into()),
+ Err(_) => Err(signal.reason()),
+ }
+}
#[derive(Debug, thiserror::Error)]
pub enum Error {
@@ -151,11 +175,48 @@ pub struct PostPhotoAssetOptions {
}
#[derive(Deserialize, Tsify)]
-pub struct PostPhotoInput {
- #[serde(with = "swb::preserve")]
- #[tsify(type = "Uint8Array")]
- bytes: js_sys::Uint8Array,
- options: PostPhotoAssetOptions,
+#[serde(rename_all = "camelCase")]
+pub struct PostVideoAssetOptions {
+ width: i32,
+ height: i32,
+ duration_ms: u32,
+}
+
+#[derive(Serialize, Deserialize, Tsify)]
+#[serde(rename_all = "camelCase")]
+pub struct UploadedPostAsset {
+ object_key: String,
+ size: Option<i64>,
+ metadata_cipher: Option<String>,
+}
+
+impl From<ente_space::PostObjectPayload> for UploadedPostAsset {
+ fn from(value: ente_space::PostObjectPayload) -> Self {
+ Self {
+ object_key: value.object_key,
+ size: value.size,
+ metadata_cipher: value.metadata_cipher,
+ }
+ }
+}
+
+impl From<UploadedPostAsset> for ente_space::PostObjectPayload {
+ fn from(value: UploadedPostAsset) -> Self {
+ Self {
+ object_key: value.object_key,
+ size: value.size,
+ metadata_cipher: value.metadata_cipher,
+ position: None,
+ video: None,
+ }
+ }
+}
+
+#[derive(Deserialize, Tsify)]
+pub struct PostMediaInput {
+ preview: UploadedPostAsset,
+ #[tsify(optional)]
+ video: Option<UploadedPostAsset>,
}
#[derive(Serialize, Tsify)]
@@ -311,6 +372,8 @@ struct MessageConversationActivity {
#[derive(Serialize, Tsify)]
#[serde(rename_all = "camelCase")]
struct PostPhoto {
+ #[serde(skip_serializing_if = "Option::is_none")]
+ video: Option<PostVideo>,
asset: PostAsset,
#[serde(skip_serializing_if = "Option::is_none")]
position: Option<i32>,
@@ -328,6 +391,13 @@ struct PostPhoto {
media_type: Option<String>,
}
+#[derive(Serialize, Tsify)]
+#[serde(rename_all = "camelCase")]
+struct PostVideo {
+ asset: PostAsset,
+ duration_ms: u32,
+}
+
#[derive(Serialize, Tsify)]
#[serde(rename_all = "camelCase")]
struct ConversationChatSummaryResponse {
@@ -630,10 +700,27 @@ impl From<PostAsset> for ente_space::PostAsset {
}
}
+impl From<ente_space::PostAsset> for PostAsset {
+ fn from(asset: ente_space::PostAsset) -> Self {
+ Self {
+ space_id: asset.space_id,
+ post_id: asset.post_id,
+ object_key: asset.object_key,
+ encrypted_post_key: asset.encrypted_post_key,
+ key_version: asset.key_version,
+ size: asset.size,
+ }
+ }
+}
+
impl From<ente_space::PostPhoto> for PostPhoto {
fn from(photo: ente_space::PostPhoto) -> Self {
let metadata = photo.metadata.unwrap_or_default();
Self {
+ video: photo.video.map(|video| PostVideo {
+ asset: video.asset.into(),
+ duration_ms: video.duration_ms,
+ }),
asset: PostAsset {
space_id: photo.asset.space_id,
post_id: photo.asset.post_id,
@@ -1097,31 +1184,107 @@ impl SpaceAccountCtxHandle {
PostResponse::from(post).into_js().map_err(Into::into)
}
- #[wasm_bindgen(js_name = createPhotoPost)]
- pub async fn create_photo_post(
+ #[wasm_bindgen(js_name = generatePostKey)]
+ pub fn generate_post_key(&self) -> Vec<u8> {
+ self.inner.generate_post_key()
+ }
+
+ #[wasm_bindgen(js_name = uploadPostPhotoAsset)]
+ pub async fn upload_post_photo_asset(
+ &self,
+ space_id: String,
+ post_key: Vec<u8>,
+ bytes: Vec<u8>,
+ options: <PostPhotoAssetOptions as Tsify>::JsType,
+ signal: Option<AbortSignal>,
+ ) -> Result<<UploadedPostAsset as Tsify>::JsType, JsValue> {
+ let options = PostPhotoAssetOptions::from_js(options).map_err(Error::from)?;
+ let asset = with_abort_signal(
+ self.inner.upload_post_photo_asset(
+ &space_id,
+ &post_key,
+ &bytes,
+ ente_space::PostPhotoAssetOptions {
+ width: options.width,
+ height: options.height,
+ media_type: options.media_type,
+ thumb_hash: options.thumb_hash,
+ },
+ ),
+ signal,
+ )
+ .await?;
+ UploadedPostAsset::from(asset)
+ .into_js()
+ .map_err(|error| Error::from(error).into())
+ }
+
+ #[wasm_bindgen(js_name = uploadPostVideoAsset)]
+ pub async fn upload_post_video_asset(
&self,
space_id: String,
- photos: Vec<<PostPhotoInput as Tsify>::JsType>,
+ post_key: Vec<u8>,
+ bytes: Vec<u8>,
+ options: <PostVideoAssetOptions as Tsify>::JsType,
+ signal: Option<AbortSignal>,
+ ) -> Result<<UploadedPostAsset as Tsify>::JsType, JsValue> {
+ let options = PostVideoAssetOptions::from_js(options).map_err(Error::from)?;
+ let asset = with_abort_signal(
+ self.inner.upload_post_video_asset(
+ &space_id,
+ &post_key,
+ &bytes,
+ options.width,
+ options.height,
+ options.duration_ms,
+ ),
+ signal,
+ )
+ .await?;
+ UploadedPostAsset::from(asset)
+ .into_js()
+ .map_err(|error| Error::from(error).into())
+ }
+
+ #[wasm_bindgen(js_name = createMediaPost)]
+ pub async fn create_media_post(
+ &self,
+ space_id: String,
+ post_key: Vec<u8>,
+ items: Vec<<PostMediaInput as Tsify>::JsType>,
+ client_request_id: String,
caption: Option<String>,
- ) -> Result<<PostResponse as Tsify>::JsType, Error> {
- let photos = photos
+ ) -> Result<i64, Error> {
+ if !(1..=10).contains(&items.len()) {
+ return Err(
+ ente_space::Error::InvalidInput("Choose between 1 and 10 items".into()).into(),
+ );
+ }
+ let items = items
.into_iter()
- .map(PostPhotoInput::from_js)
+ .map(PostMediaInput::from_js)
.collect::<Result<Vec<_>, _>>()?;
- let photos = photos.into_iter().map(|photo| ente_space::PostPhotoInput {
- bytes: photo.bytes.to_vec(),
- options: ente_space::PostPhotoAssetOptions {
- width: photo.options.width,
- height: photo.options.height,
- media_type: photo.options.media_type,
- thumb_hash: photo.options.thumb_hash,
- },
- });
- let post = self
+ let objects = items
+ .into_iter()
+ .enumerate()
+ .map(|(position, item)| {
+ let mut object: ente_space::PostObjectPayload = item.preview.into();
+ object.position = Some(position as i32);
+ object.video = item.video.map(|video| Box::new(video.into()));
+ object
+ })
+ .collect::<Vec<_>>();
+ let (id, _) = self
.inner
- .create_photo_post(&space_id, photos, caption.as_deref())
+ .create_post_with_request_id(
+ &space_id,
+ &objects,
+ caption.as_deref().map(str::as_bytes),
+ Some(&post_key),
+ Some(client_request_id),
+ )
.await?;
- PostResponse::from(post).into_js().map_err(Into::into)
+ Ok(id)
}
#[wasm_bindgen(js_name = downloadPostAsset)]
diff --git a/rust/crates/space/src/client/assets.rs b/rust/crates/space/src/client/assets.rs
index efc27f9be4..410c2b65bb 100644
--- a/rust/crates/space/src/client/assets.rs
+++ b/rust/crates/space/src/client/assets.rs
@@ -15,6 +15,41 @@ use crate::{
};
impl AccountSpaceCtx {
+ pub async fn upload_post_video_asset(
+ &self,
+ space_id: &str,
+ post_key: &[u8],
+ plaintext: &[u8],
+ width: i32,
+ height: i32,
+ duration_ms: u32,
+ ) -> Result<PostObjectPayload> {
+ if plaintext.len() < 12
+ || &plaintext[4..8] != b"ftyp"
+ || width <= 0
+ || height <= 0
+ || !(1..=10_000).contains(&duration_ms)
+ {
+ return Err(Error::InvalidInput(
+ "Videos must be MP4 and at most 10 seconds".into(),
+ ));
+ }
+ let mut object = self
+ .upload_post_asset(space_id, post_key, plaintext, None)
+ .await?;
+ object.metadata_cipher = Some(encrypt_post_object_metadata(
+ post_key,
+ &PostObjectMetadata {
+ width: Some(width),
+ height: Some(height),
+ media_type: Some("video/mp4".into()),
+ duration_ms: Some(duration_ms),
+ ..Default::default()
+ },
+ )?);
+ Ok(object)
+ }
+
pub async fn presign_post_upload(
&self,
space_id: &str,
@@ -116,6 +151,7 @@ impl AccountSpaceCtx {
.await?;
self.upload_bytes(&presign, &encrypted).await?;
Ok(PostObjectPayload {
+ video: None,
object_key: presign.object_key,
size: Some(encrypted.len() as i64),
position,
diff --git a/rust/crates/space/src/client/mod.rs b/rust/crates/space/src/client/mod.rs
index a13bee31af..9cc5e9f4a9 100644
--- a/rust/crates/space/src/client/mod.rs
+++ b/rust/crates/space/src/client/mod.rs
@@ -48,7 +48,7 @@ const MESSAGE_KIND_POKE: &str = "poke";
const MESSAGE_KIND_POST_REPLY: &str = "post_reply";
const ONLY_PHOTOS_UPLOAD_MESSAGE: &str = "only photos can be uploaded";
-pub const MAX_SPACE_POST_UPLOAD_BYTES: usize = 5 * 1024 * 1024;
+pub const MAX_SPACE_POST_UPLOAD_BYTES: usize = 15 * 1024 * 1024;
pub const MAX_SPACE_AVATAR_UPLOAD_BYTES: usize = 2 * 1024 * 1024;
pub const MAX_SPACE_COVER_UPLOAD_BYTES: usize = 2 * 1024 * 1024;
pub const MAX_SPACE_POST_PLAINTEXT_BYTES: usize =
@@ -693,13 +693,30 @@ fn decrypt_post_object_metadata(
.map_err(|err| Error::InvalidInput(format!("invalid post object metadata: {err}")))
}
-fn ensure_post_objects_are_photos(objects: &[PostObjectPayload], post_key: &[u8]) -> Result<()> {
+fn ensure_post_objects_supported(objects: &[PostObjectPayload], post_key: &[u8]) -> Result<()> {
+ if objects.len() > 10 {
+ return Err(Error::InvalidInput("Choose between 1 and 10 items".into()));
+ }
for object in objects {
let metadata = decrypt_post_object_metadata(post_key, object)?
.ok_or_else(|| Error::InvalidInput("post object metadata is required".into()))?;
if ensure_supported_photo_media_type(metadata.media_type.as_deref())?.is_none() {
return Err(Error::InvalidInput(ONLY_PHOTOS_UPLOAD_MESSAGE.into()));
}
+ if let Some(video) = &object.video {
+ let metadata = decrypt_post_object_metadata(post_key, video)?
+ .ok_or_else(|| Error::InvalidInput("video metadata is required".into()))?;
+ if video.video.is_some()
+ || metadata.media_type.as_deref() != Some("video/mp4")
+ || !metadata
+ .duration_ms
+ .is_some_and(|duration| (1..=10_000).contains(&duration))
+ {
+ return Err(Error::InvalidInput(
+ "Videos must be MP4 and at most 10 seconds".into(),
+ ));
+ }
+ }
}
Ok(())
}
diff --git a/rust/crates/space/src/client/posts.rs b/rust/crates/space/src/client/posts.rs
index 4bd876bbec..4bff40b36e 100644
--- a/rust/crates/space/src/client/posts.rs
+++ b/rust/crates/space/src/client/posts.rs
@@ -1,11 +1,12 @@
use super::{
- AccountSpaceCtx, PostPhotoInput, decrypt_post_object_metadata, ensure_post_objects_are_photos,
+ AccountSpaceCtx, PostPhotoInput, decrypt_post_object_metadata, ensure_post_objects_supported,
retain_content_error,
};
use crate::crypto::{decrypt_secretbox_payload, encrypt_secretbox_payload, generate_key};
use crate::error::{Error, Result};
use crate::models::{
- HydratedKeys, Post, PostAsset, PostContent, PostPage, PostPhoto, SpaceActor, SpaceProfile,
+ HydratedKeys, Post, PostAsset, PostContent, PostPage, PostPhoto, PostVideo, SpaceActor,
+ SpaceProfile,
};
use crate::transport::{
CreatePostRequest, CreatePostResponse, LikePostResponse, PostObjectPayload, PostPageResponse,
@@ -54,9 +55,21 @@ impl AccountSpaceCtx {
objects: &[PostObjectPayload],
caption_plaintext: Option<&[u8]>,
post_key: Option<&[u8]>,
+ ) -> Result<(i64, Vec<u8>)> {
+ self.create_post_with_request_id(space_id, objects, caption_plaintext, post_key, None)
+ .await
+ }
+
+ pub async fn create_post_with_request_id(
+ &self,
+ space_id: &str,
+ objects: &[PostObjectPayload],
+ caption_plaintext: Option<&[u8]>,
+ post_key: Option<&[u8]>,
+ client_request_id: Option<String>,
) -> Result<(i64, Vec<u8>)> {
let post_key_bytes = post_key.map_or_else(generate_key, ToOwned::to_owned);
- ensure_post_objects_are_photos(objects, &post_key_bytes)?;
+ ensure_post_objects_supported(objects, &post_key_bytes)?;
let access = self
.resolve_owned_space_access(space_id)
.await?
@@ -71,6 +84,7 @@ impl AccountSpaceCtx {
None => None,
};
let request = CreatePostRequest {
+ client_request_id,
encrypted_post_key: b64::encode(&encrypt_secretbox_payload(
&access.space_key,
&post_key_bytes,
@@ -468,7 +482,36 @@ pub(super) fn open_post_content(
.filter(|object| !object.object_key.trim().is_empty())
.map(|object| {
let metadata = decrypt_post_object_metadata(&decrypted.post_key, &object)?;
+ let video = object
+ .video
+ .as_ref()
+ .map(|video| {
+ let metadata = decrypt_post_object_metadata(&decrypted.post_key, video)?
+ .ok_or_else(|| {
+ Error::InvalidInput("video metadata is required".into())
+ })?;
+ let duration_ms = metadata
+ .duration_ms
+ .filter(|duration| (1..=10_000).contains(duration))
+ .ok_or_else(|| Error::InvalidInput("invalid video duration".into()))?;
+ if metadata.media_type.as_deref() != Some("video/mp4") {
+ return Err(Error::InvalidInput("invalid video format".into()));
+ }
+ Ok(PostVideo {
+ asset: PostAsset {
+ space_id: post.space_id.clone(),
+ post_id: post.post_id,
+ object_key: video.object_key.clone(),
+ encrypted_post_key: post.encrypted_post_key.clone(),
+ key_version: post.key_version,
+ size: video.size,
+ },
+ duration_ms,
+ })
+ })
+ .transpose()?;
Ok(PostPhoto {
+ video,
asset: PostAsset {
space_id: post.space_id.clone(),
post_id: post.post_id,
@@ -524,6 +567,7 @@ mod tests {
caption_cipher: String::new(),
key_version: 1,
objects: vec![PostObjectPayload {
+ video: None,
object_key: "photo".into(),
size: None,
position: None,
@@ -574,6 +618,7 @@ mod tests {
b64::encode(&secretbox::encrypt_combined(&[0xff], &post_key));
let mut invalid_metadata = post(5, b64::encode(&valid_post_key));
invalid_metadata.objects.push(PostObjectPayload {
+ video: None,
object_key: "photo".into(),
size: None,
position: None,
diff --git a/rust/crates/space/src/client/tests.rs b/rust/crates/space/src/client/tests.rs
index 2cab2c2beb..b22e406d32 100644
--- a/rust/crates/space/src/client/tests.rs
+++ b/rust/crates/space/src/client/tests.rs
@@ -1234,6 +1234,7 @@ async fn create_post_rejects_video_object_media_type() {
.create_post(
"space_owner_main",
&[PostObjectPayload {
+ video: None,
object_key: "object-1".to_owned(),
size: None,
position: Some(0),
diff --git a/rust/crates/space/src/lib.rs b/rust/crates/space/src/lib.rs
index 1c9354ac45..6ea2785ae5 100644
--- a/rust/crates/space/src/lib.rs
+++ b/rust/crates/space/src/lib.rs
@@ -16,8 +16,8 @@ pub use models::{
ConversationChatSummary, Conversations, CreatedSpace, CreatedSpaceLink, DecryptedFriendShare,
DecryptedSpaceProfile, HydratedKeys, Message, MessageActivity, MessageContent, MessagePage,
MessagePayload, OpenAccountSpaceCtxInput, OpenSpaceLinkCtxInput, Post, PostAsset, PostContent,
- PostObjectMetadata, PostPage, PostPhoto, SpaceActor, SpaceFriend, SpaceFriendRequest,
- SpaceProfile, SpaceSentFriendRequest,
+ PostObjectMetadata, PostPage, PostPhoto, PostVideo, SpaceActor, SpaceFriend,
+ SpaceFriendRequest, SpaceProfile, SpaceSentFriendRequest,
};
pub use transport::{
ConversationChatSummaryResponse, ConversationsResponse, EntityKeyPayload,
diff --git a/rust/crates/space/src/models.rs b/rust/crates/space/src/models.rs
index a67bf45ec2..52917ac33f 100644
--- a/rust/crates/space/src/models.rs
+++ b/rust/crates/space/src/models.rs
@@ -138,6 +138,7 @@ pub struct PostContent {
}
pub struct PostPhoto {
+ pub video: Option<PostVideo>,
pub asset: PostAsset,
pub position: Option<i32>,
pub metadata: Option<PostObjectMetadata>,
@@ -152,6 +153,11 @@ pub struct PostAsset {
pub size: Option<i64>,
}
+pub struct PostVideo {
+ pub asset: PostAsset,
+ pub duration_ms: u32,
+}
+
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SpaceProfile {
pub full_name: Option<String>,
@@ -242,6 +248,8 @@ pub struct Conversations {
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct PostObjectMetadata {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub duration_ms: Option<u32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub variant: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
diff --git a/rust/crates/space/src/transport.rs b/rust/crates/space/src/transport.rs
index c2ae655a94..a2e0eba609 100644
--- a/rust/crates/space/src/transport.rs
+++ b/rust/crates/space/src/transport.rs
@@ -89,6 +89,8 @@ pub struct AssetDownloadResponse {
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct PostObjectPayload {
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub video: Option<Box<PostObjectPayload>>,
pub object_key: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<i64>,
@@ -101,6 +103,8 @@ pub struct PostObjectPayload {
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreatePostRequest {
+ #[serde(skip_serializing_if = "Option::is_none")]
+ pub client_request_id: Option<String>,
pub encrypted_post_key: String,
pub key_version: i32,
#[serde(skip_serializing_if = "Option::is_none")]
diff --git a/web/apps/space/public/_headers b/web/apps/space/public/_headers
index d9d30f30f3..bb61a2deeb 100644
--- a/web/apps/space/public/_headers
+++ b/web/apps/space/public/_headers
@@ -8,7 +8,7 @@
X-XSS-Protection: 1; mode=block
Referrer-Policy: no-referrer
Reporting-Endpoints: csp-endpoint="https://csp-reporter.ente.com"
- Content-Security-Policy: default-src 'self'; img-src 'self' blob: data:; media-src 'self' blob: data:; style-src 'self' 'unsafe-inline'; font-src 'self'; script-src 'self' 'wasm-unsafe-eval'; worker-src 'self' blob:; manifest-src 'self'; child-src 'self' blob:; object-src 'none'; connect-src 'self' https://*.ente.com https://*.ente.io https://space-api.ente-dev.com data: blob: https://*.s3.eu-central-003.backblazeb2.com https://*.s3.eu-central-2.wasabisys.com; base-uri 'self'; frame-ancestors 'none'; form-action 'none'; report-uri https://csp-reporter.ente.com; report-to csp-endpoint;
+ Content-Security-Policy: default-src 'self'; img-src 'self' blob: data:; media-src 'self' blob: data:; style-src 'self' 'unsafe-inline'; font-src 'self'; script-src 'self' 'wasm-unsafe-eval' https://assets.ente.com/ffmpeg-core-0.12.10/ffmpeg-core.js; worker-src 'self' blob:; manifest-src 'self'; child-src 'self' blob:; object-src 'none'; connect-src 'self' https://*.ente.com https://*.ente.io https://space-api.ente-dev.com data: blob: https://*.s3.eu-central-003.backblazeb2.com https://*.s3.eu-central-2.wasabisys.com; base-uri 'self'; frame-ancestors 'none'; form-action 'none'; report-uri https://csp-reporter.ente.com; report-to csp-endpoint;
/_next/static/*
! Cache-Control
diff --git a/web/apps/space/src/components/FileViewer.tsx b/web/apps/space/src/components/FileViewer.tsx
index 24fceaa1e4..d56c62f0a8 100644
--- a/web/apps/space/src/components/FileViewer.tsx
+++ b/web/apps/space/src/components/FileViewer.tsx
@@ -23,11 +23,16 @@ import { spacePostLikePopDurationMs } from "components/post-like-animation";
import { SpacePostPhotosCounter } from "components/PostPhotosCounter";
import { SpacePostPhotosDots } from "components/PostPhotosDots";
import { SpacePostReplyControls } from "components/PostReplyControls";
+import { createSpaceVideoContent } from "components/PostVideoContent";
import log from "ente-base/log";
import type PhotoSwipe from "photoswipe";
import React from "react";
import type { SpaceInviteIntent } from "services/invite";
-import type { SpacePostAsset, SpacePostAssetURLLoader } from "services/space";
+import type {
+ SpacePostAsset,
+ SpacePostAssetURLLoader,
+ SpacePostVideo,
+} from "services/space";
import { spaceDialogBackground, spaceTextMuted } from "styles/colors";
import { spaceTouchTargetSize } from "styles/touch-targets";
import { firstNameFrom, formatSpaceDate } from "utils/display";
@@ -108,6 +113,7 @@ const spaceViewerPostActionConfigs: Record<
};
export interface SpaceViewerPhoto {
+ video?: SpacePostVideo;
alt?: string;
avatarUrl?: string | null;
caption?: string;
@@ -181,7 +187,7 @@ const viewerTouchPoint = (event: Event) => {
};
const viewerPhotoContentKey = (photo: SpaceViewerPhoto) =>
- `${photo.imageUrl}:${photo.width ?? ""}:${photo.height ?? ""}:${photo.postPhotoCount ?? ""}`;
+ `${photo.imageUrl}:${photo.width ?? ""}:${photo.height ?? ""}:${photo.postPhotoCount ?? ""}:${photo.video?.asset?.objectKey ?? photo.video?.url ?? ""}:${photo.video?.start ?? ""}:${photo.video?.end ?? ""}:${photo.video?.muted ?? ""}`;
const viewerSwipeStartsOnInteractiveTarget = (target: EventTarget | null) =>
target instanceof Element &&
@@ -400,6 +406,8 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
? `${item.imageAsset.spaceId}:${item.imageAsset.objectKey}`
: item.imageUrl;
const hasDraftPhotoControls = Boolean(draftPhotoControls);
+ const hasDraftPhotoControlsRef = React.useRef(hasDraftPhotoControls);
+ hasDraftPhotoControlsRef.current = hasDraftPhotoControls;
const activePostActionMode = postActionMode;
const isDraftPost = activePostActionMode == "draft-post";
const { showLikeButton: showPhotoLikeButton } =
@@ -446,12 +454,17 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
Boolean(activePhoto.postId) &&
Boolean(onUpdatePostCaption);
const canManagePost = canDeletePost || canUpdatePostCaption;
+ const videoLoaderRef = React.useRef(onLoadPhoto);
+ videoLoaderRef.current = onLoadPhoto;
const viewerPhotosRef = React.useRef(viewerPhotos);
const onCloseRef = React.useRef(onClose);
const onPhotoIndexChangeRef = React.useRef(onPhotoIndexChange);
const initialPhotoIndexRef = React.useRef(activePhotoIndex);
const fallbackPhotoRef = React.useRef(activePhoto);
const pswpRef = React.useRef<PhotoSwipe | undefined>(undefined);
+ const videoContentsRef = React.useRef(
+ new Map<object, ReturnType<typeof createSpaceVideoContent>>(),
+ );
const closeOnSwipePastEndRef = React.useRef(closeOnSwipePastEnd);
closeOnSwipePastEndRef.current = closeOnSwipePastEnd;
viewerPhotosRef.current = viewerPhotos;
@@ -1012,6 +1025,10 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
pswp.goTo(activePhotoIndex);
}, [activePhotoIndex]);
+ React.useEffect(() => {
+ pswpRef.current?.updateSize(true);
+ }, [hasDraftPhotoControls]);
+
const handleDeleteSheetExited = () => {
if (!isDeleteExit) return;
@@ -1116,7 +1133,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
closeOnVerticalDrag: true,
counter: false,
doubleTapAction: "zoom",
- errorMsg: "Unable to preview this photo",
+ errorMsg: "Unable to preview this item",
escKey: false,
getViewportSizeFn: viewerViewportSize,
imageClickAction: "zoom",
@@ -1124,7 +1141,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
loop: false,
mainClass: "pswp-space-viewer",
paddingFn: () => ({
- bottom: hasDraftPhotoControls
+ bottom: hasDraftPhotoControlsRef.current
? 176
: window.innerWidth >= viewerDesktopMinWidth
? 0
@@ -1145,6 +1162,36 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
wheelToZoom: true,
zoom: false,
});
+ const videos = videoContentsRef.current;
+ pswp.on("contentLoad", (event) => {
+ if (event.content.data.type != "video") return;
+ event.preventDefault();
+ videos.get(event.content)?.destroy();
+ const item = viewerPhotosRef.current[event.content.index]!;
+ const video = createSpaceVideoContent(item, (asset) =>
+ videoLoaderRef.current!(asset),
+ );
+ event.content.element = video.element;
+ event.content.onLoaded();
+ videos.set(event.content, video);
+ });
+ pswp.on("contentActivate", ({ content }) =>
+ videos.get(content)?.play(),
+ );
+ pswp.on("contentDeactivate", ({ content }) =>
+ videos.get(content)?.deactivate(),
+ );
+ pswp.on("contentDestroy", ({ content }) => {
+ videos.get(content)?.destroy();
+ videos.delete(content);
+ });
+ pswp.on("destroy", () => {
+ videos.forEach((video) => video.destroy());
+ videos.clear();
+ });
+ pswp.addFilter("isContentZoomable", (zoomable, content) =>
+ content.data.type == "video" ? false : zoomable,
+ );
pswpRef.current = pswp;
displayedContentKeysRef.current = viewerPhotosRef.current.map(
viewerPhotoContentKey,
@@ -1153,9 +1200,22 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
pswp.addFilter("itemData", (_, index) => {
const item =
viewerPhotosRef.current[index] ?? fallbackPhotoRef.current;
+ if (item.video)
+ return {
+ type: "video",
+ width: item.width ?? defaultPhotoWidth,
+ height: item.height ?? defaultPhotoHeight,
+ };
if (!item.imageUrl)
return {
- html: `<div class="space-photo-placeholder" role="status">${item.postPhotoCount == 0 ? "Add photos to your post" : "Loading photo…"}</div>`,
+ html:
+ item.postPhotoCount == 0
+ ? '<div class="space-photo-placeholder" role="status">Add photos or videos to your post</div>'
+ : '<div class="space-photo-placeholder" role="status" aria-label="Preparing preview" aria-busy="true"></div>',
+ width: item.width ?? defaultPhotoWidth,
+ height:
+ item.height ??
+ defaultPhotoWidth * (isDraftPost ? 16 / 9 : 4 / 3),
};
return {
alt: item.alt ?? `${item.name} post`,
@@ -1238,7 +1298,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
pswp?.destroy();
root.style.removeProperty("--space-viewer-bg-opacity");
};
- }, [hasDraftPhotoControls, viewerViewportSize]);
+ }, [isDraftPost, viewerViewportSize]);
React.useEffect(() => {
if (typeof document == "undefined") return;
@@ -1284,7 +1344,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
<Box
ref={viewerRootRef}
role="dialog"
- aria-label={`${displayName} photo viewer`}
+ aria-label={`${displayName} post viewer`}
aria-modal="true"
onTransitionEnd={(event) => {
if (
@@ -1319,6 +1379,8 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
color: "#A6A6A6",
display: "flex",
height: "100%",
+ width: isDraftPost ? "100vw" : "100%",
+ ml: isDraftPost ? "calc((100% - 100vw) / 2)" : 0,
justifyContent: "center",
fontSize: 14,
},
@@ -1523,14 +1585,19 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
<Box
component="button"
type="button"
- aria-label="Edit photo"
- title="Edit photo"
+ aria-label="Edit item"
+ title="Edit item"
disabled={
isDraftPostActionRunning ||
isDraftPostPreviewPending ||
Boolean(draftPostPreparationError)
}
- onClick={onEditDraftPhoto}
+ onClick={() => {
+ videoContentsRef.current.forEach((video) =>
+ video.deactivate(),
+ );
+ onEditDraftPhoto();
+ }}
sx={{
...viewerHeaderButtonSx,
mr: postPhotoCount > 1 ? "8px" : 0,
@@ -1559,6 +1626,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
? "Cancel caption edit"
: "Close viewer"
}
+ disabled={isDraftPostActionRunning}
onClick={closeViewer}
sx={viewerHeaderButtonSx}
>
@@ -1711,7 +1779,7 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
component="button"
type="button"
aria-label={
- direction < 0 ? "Previous photo" : "Next photo"
+ direction < 0 ? "Previous item" : "Next item"
}
disabled={
isSwipeBlockedRef.current ||
@@ -1904,8 +1972,8 @@ export const SpaceFileViewer: React.FC<SpaceFileViewerProps> = ({
type="button"
aria-label={
draftPostPreparationError
- ? "Photo could not be prepared"
- : "Post photos"
+ ? "Item could not be prepared"
+ : "Post"
}
disabled={isDraftPostPublishDisabled}
onClick={publishDraftPost}
diff --git a/web/apps/space/src/components/InlinePostVideo.tsx b/web/apps/space/src/components/InlinePostVideo.tsx
new file mode 100644
index 0000000000..56c14a4af5
--- /dev/null
+++ b/web/apps/space/src/components/InlinePostVideo.tsx
@@ -0,0 +1,57 @@
+import { Box } from "@mui/material";
+import { createSpaceVideoContent } from "components/PostVideoContent";
+import React, { useEffect, useRef } from "react";
+import type { SpacePostAssetURLLoader, SpacePostVideo } from "services/space";
+
+let playingVideo: ReturnType<typeof createSpaceVideoContent> | undefined;
+
+export const SpaceInlinePostVideo: React.FC<{
+ imageUrl: string;
+ video: SpacePostVideo;
+ isActive: boolean;
+ onLoadVideo?: SpacePostAssetURLLoader;
+}> = ({ imageUrl, video, isActive, onLoadVideo }) => {
+ const root = useRef<HTMLDivElement>(null);
+ const player = useRef<
+ ReturnType<typeof createSpaceVideoContent> | undefined
+ >(undefined);
+
+ useEffect(() => {
+ const container = root.current!;
+ const content = createSpaceVideoContent(
+ { imageUrl, video },
+ onLoadVideo,
+ { inline: true },
+ );
+ player.current = content;
+ container.append(content.element);
+ const onPlay = () => {
+ if (playingVideo != content) playingVideo?.pause();
+ playingVideo = content;
+ };
+ content.video.addEventListener("play", onPlay);
+ const observer = new IntersectionObserver(([entry]) => {
+ if (!entry!.isIntersecting) content.deactivate();
+ });
+ observer.observe(container);
+ return () => {
+ observer.disconnect();
+ content.video.removeEventListener("play", onPlay);
+ content.destroy();
+ content.element.remove();
+ if (playingVideo == content) playingVideo = undefined;
+ player.current = undefined;
+ };
+ }, [imageUrl, onLoadVideo, video]);
+
+ useEffect(() => {
+ const content = player.current!;
+ if (!isActive) content.deactivate();
+ content.video.tabIndex = isActive ? 0 : -1;
+ content.element.querySelector("button")!.tabIndex = isActive ? 0 : -1;
+ }, [imageUrl, isActive, onLoadVideo, video]);
+
+ return (
+ <Box ref={root} sx={{ position: "absolute", inset: 0, zIndex: 1 }} />
+ );
+};
diff --git a/web/apps/space/src/components/PostComposer.tsx b/web/apps/space/src/components/PostComposer.tsx
index f0fcb09dfb..0ae4dd790c 100644
--- a/web/apps/space/src/components/PostComposer.tsx
+++ b/web/apps/space/src/components/PostComposer.tsx
@@ -23,6 +23,13 @@ import {
type SpacePostPhotoEdit,
} from "utils/post-image";
import { maxSpacePostPhotos, movePostPhoto } from "utils/post-photos";
+import {
+ initialSpaceVideoEdit,
+ isSpaceVideoFile,
+ spaceVideoCover,
+ spaceVideoInfo,
+ type SpacePostVideoEdit,
+} from "utils/post-video";
import { useSpaceRouter } from "utils/route-transitions";
import { spaceRoutes } from "utils/routes";
@@ -33,6 +40,7 @@ interface DraftPhoto {
photo?: SpaceViewerPhoto;
originalPhoto?: SpaceViewerPhoto;
edit?: SpacePostPhotoEdit;
+ videoEdit?: SpacePostVideoEdit;
}
let nextDraftPhotoID = 0;
@@ -55,8 +63,11 @@ export const SpacePostComposer: React.FC<{
const [activeIndex, setActiveIndex] = React.useState(0);
const [isPublishing, setIsPublishing] = React.useState(false);
const [isExiting, setIsExiting] = React.useState(false);
- const [isEditing, setIsEditing] = React.useState(false);
- const inputRef = React.useRef<HTMLInputElement | null>(null);
+ const [editorSnapshot, setEditorSnapshot] = React.useState<{
+ drafts: DraftPhoto[];
+ activeIndex: number;
+ }>();
+ const inputRef = React.useRef<HTMLInputElement>(null);
const previewURLsRef = React.useRef(new Set<string>());
const publishedPreviewURLRef = React.useRef<string>(undefined);
const preparingRef = React.useRef(new Set<number>());
@@ -77,7 +88,7 @@ export const SpacePostComposer: React.FC<{
const { clearBrowserBackState } = useBrowserBackClose({
open: true,
onClose: () => {
- if (!isPublishing) onClose();
+ if (!isPublishing || isExiting) onClose();
},
stateKey: "space-post-composer",
});
@@ -111,7 +122,30 @@ export const SpacePostComposer: React.FC<{
if (!isActiveDraft(draft.id)) continue;
try {
let photo: SpaceViewerPhoto;
- if (canPreviewSpaceImageFile(draft.file)) {
+ let videoEdit: SpacePostVideoEdit | undefined;
+ if (isSpaceVideoFile(draft.file)) {
+ const info = await spaceVideoInfo(draft.file);
+ videoEdit = initialSpaceVideoEdit(info.duration);
+ const cover = await spaceVideoCover(
+ draft.file,
+ videoEdit.coverTime,
+ );
+ const url = URL.createObjectURL(draft.file);
+ previewURLsRef.current.add(url);
+ photo = {
+ ...placeholder,
+ imageUrl: URL.createObjectURL(cover.file),
+ width: info.width,
+ height: info.height,
+ video: {
+ url,
+ durationMs: info.duration * 1000,
+ start: videoEdit.start,
+ end: videoEdit.end,
+ muted: videoEdit.muted,
+ },
+ };
+ } else if (canPreviewSpaceImageFile(draft.file)) {
photo = (
await createLoadedLocalPostPhoto({
avatarUrl: profile.avatarUrl,
@@ -132,13 +166,20 @@ export const SpacePostComposer: React.FC<{
}
if (!isActiveDraft(draft.id)) {
URL.revokeObjectURL(photo.imageUrl);
+ if (photo.video?.url)
+ URL.revokeObjectURL(photo.video.url);
continue;
}
previewURLsRef.current.add(photo.imageUrl);
setDrafts((current) =>
current.map((item) =>
item.id == draft.id
- ? { ...item, photo, originalPhoto: photo }
+ ? {
+ ...item,
+ photo,
+ originalPhoto: photo,
+ videoEdit,
+ }
: item,
),
);
@@ -150,9 +191,13 @@ export const SpacePostComposer: React.FC<{
item.id == draft.id
? {
...item,
- error: spacePostImageErrorMessage(
- error,
- ),
+ error:
+ isSpaceVideoFile(draft.file) &&
+ error instanceof Error
+ ? error.message
+ : spacePostImageErrorMessage(
+ error,
+ ),
}
: item,
),
@@ -168,22 +213,26 @@ export const SpacePostComposer: React.FC<{
setActiveIndex(drafts.length);
setDrafts((current) => [...current, ...draftPhotos(files)]);
};
- const removePhoto = () => {
- if (drafts.length == 1) {
- onClose();
- return;
- }
- const removed = drafts[activeIndex];
- if (!removed) return;
+ const releasePreview = (draft: DraftPhoto) => {
for (const url of new Set([
- removed.photo?.imageUrl,
- removed.originalPhoto?.imageUrl,
+ draft.photo?.imageUrl,
+ draft.originalPhoto?.imageUrl,
+ draft.originalPhoto?.video?.url,
])) {
if (url) {
URL.revokeObjectURL(url);
previewURLsRef.current.delete(url);
}
}
+ };
+ const removePhoto = () => {
+ if (drafts.length == 1) {
+ onClose();
+ return;
+ }
+ const removed = drafts[activeIndex];
+ if (!removed) return;
+ releasePreview(removed);
setDrafts((current) =>
current.filter((draft) => draft.id != removed.id),
);
@@ -193,8 +242,23 @@ export const SpacePostComposer: React.FC<{
setDrafts((current) => movePostPhoto(current, from, to));
setActiveIndex(to);
};
- const applyEdits = (results: SpacePostPhotoEditResult[], index: number) => {
- const nextDrafts = drafts.map((draft) => {
+ const cancelEdits = () => {
+ const snapshot = editorSnapshot!;
+ for (const draft of drafts) {
+ if (!snapshot.drafts.some(({ id }) => id == draft.id))
+ releasePreview(draft);
+ }
+ setDrafts(snapshot.drafts);
+ setActiveIndex(snapshot.activeIndex);
+ setEditorSnapshot(undefined);
+ };
+ const applyEdits = (
+ results: SpacePostPhotoEditResult[],
+ index: number,
+ photoIDs: number[],
+ ) => {
+ const nextDrafts = photoIDs.map((id) => {
+ const draft = drafts.find((draft) => draft.id == id)!;
const result = results.find((result) => result.id == draft.id);
if (!result) return draft;
if (draft.photo!.imageUrl != draft.originalPhoto!.imageUrl) {
@@ -210,11 +274,22 @@ export const SpacePostComposer: React.FC<{
}
: draft.originalPhoto!;
previewURLsRef.current.add(photo.imageUrl);
- return { ...draft, edit: result.edit, photo };
+ const videoEdit = result.videoEdit ?? draft.videoEdit;
+ if (photo.video && videoEdit)
+ photo.video = {
+ ...photo.video,
+ start: videoEdit.start,
+ end: videoEdit.end,
+ muted: videoEdit.muted,
+ };
+ return { ...draft, edit: result.edit, videoEdit, photo };
});
+ for (const draft of drafts) {
+ if (!photoIDs.includes(draft.id)) releasePreview(draft);
+ }
setDrafts(nextDrafts);
setActiveIndex(index);
- setIsEditing(false);
+ setEditorSnapshot(undefined);
};
const photos = drafts.map((draft, index) => ({
...(draft.photo ?? placeholder),
@@ -223,38 +298,58 @@ export const SpacePostComposer: React.FC<{
}));
const isPreparing = drafts.some((draft) => !draft.photo && !draft.error);
const preparationError = drafts.find((draft) => draft.error)?.error;
- const controls =
- files.length > 1 ? (
- <>
- <SpacePostPhotoInput
- inputRef={inputRef}
- onSelect={addPhotos}
- remaining={maxSpacePostPhotos - drafts.length}
- />
- <SpacePostPhotoStrip
- activeIndex={activeIndex}
- disabled={isPublishing}
- onAdd={() => inputRef.current?.click()}
- onMove={movePhoto}
- onRemove={removePhoto}
- onSelect={setActiveIndex}
- photos={drafts.map((draft) => ({
- id: draft.id,
- imageUrl: draft.photo?.imageUrl,
- }))}
- />
- </>
- ) : undefined;
+ const images = React.useMemo(
+ () =>
+ drafts.map((draft) => ({
+ cropArea: draft.edit?.cropArea,
+ file: draft.file,
+ height: draft.photo?.height,
+ previewUrl: draft.photo?.imageUrl ?? "",
+ rotationDegrees: draft.edit?.rotationDegrees,
+ width: draft.photo?.width,
+ video: draft.videoEdit,
+ })),
+ [drafts],
+ );
+ const showPhotoStrip = files.length > 1;
+ const controls = showPhotoStrip && (
+ <SpacePostPhotoStrip
+ activeIndex={activeIndex}
+ disabled={isPublishing}
+ onAdd={() => inputRef.current?.click()}
+ onMove={movePhoto}
+ onRemove={drafts.length > 1 ? removePhoto : undefined}
+ onSelect={setActiveIndex}
+ photos={drafts.map((draft) => ({
+ id: draft.id,
+ imageUrl: draft.photo?.imageUrl,
+ isLoading: !draft.photo && !draft.error,
+ durationMs: draft.videoEdit
+ ? (draft.videoEdit.end - draft.videoEdit.start) * 1000
+ : undefined,
+ }))}
+ />
+ );
return (
<>
+ <SpacePostPhotoInput
+ inputRef={inputRef}
+ onSelect={addPhotos}
+ remaining={maxSpacePostPhotos - drafts.length}
+ />
<SpaceViewerPostBackdrop exiting={isExiting} />
<SpaceFileViewer
draftPhotoControls={controls}
draftPostPreparationError={preparationError}
isDraftPostPreviewPending={isPreparing || !drafts.length}
- onClose={onClose}
- onEditDraftPhoto={() => setIsEditing(true)}
+ onClose={() => {
+ if (!isPublishing || isExiting) onClose();
+ }}
+ onEditDraftPhoto={() => {
+ if (!isPublishing)
+ setEditorSnapshot({ drafts, activeIndex });
+ }}
onDraftPostExitStart={() => setIsPublishing(true)}
onDraftPostExitAnimationStart={() => setIsExiting(true)}
onDraftPostPublished={() => {
@@ -266,18 +361,7 @@ export const SpacePostComposer: React.FC<{
: (caption) => {
publishedPreviewURLRef.current =
drafts[0]!.photo!.imageUrl;
- return onPublish(
- drafts.map((draft) => ({
- cropArea: draft.edit?.cropArea,
- file: draft.file,
- height: draft.photo!.height,
- previewUrl: draft.photo!.imageUrl,
- rotationDegrees:
- draft.edit?.rotationDegrees,
- width: draft.photo!.width,
- })),
- caption,
- );
+ return onPublish(images, caption);
}
}
photo={photos[0] ?? placeholder}
@@ -286,18 +370,32 @@ export const SpacePostComposer: React.FC<{
onPhotoIndexChange={setActiveIndex}
postActionMode="draft-post"
/>
- {isEditing && (
+ {editorSnapshot && (
<SpacePostPhotoEditor
initialIndex={activeIndex}
- onClose={() => setIsEditing(false)}
+ showPhotoStrip={showPhotoStrip}
+ onAdd={addPhotos}
+ onClose={cancelEdits}
onDone={applyEdits}
photos={drafts.map((draft) => ({
id: draft.id,
- imageURL: draft.originalPhoto!.imageUrl,
- previewURL: draft.photo!.imageUrl,
- width: draft.originalPhoto!.width!,
- height: draft.originalPhoto!.height!,
+ imageURL: draft.originalPhoto?.imageUrl ?? "",
+ previewURL: draft.photo?.imageUrl ?? "",
+ width: draft.originalPhoto?.width ?? 0,
+ height: draft.originalPhoto?.height ?? 0,
+ isLoading: !draft.photo && !draft.error,
+ preparationError: draft.error,
edit: draft.edit,
+ video: draft.videoEdit
+ ? {
+ file: draft.file,
+ sourceURL: draft.originalPhoto!.video!.url!,
+ duration:
+ draft.originalPhoto!.video!.durationMs /
+ 1000,
+ edit: draft.videoEdit,
+ }
+ : undefined,
}))}
/>
)}
diff --git a/web/apps/space/src/components/PostPhotoEditor.tsx b/web/apps/space/src/components/PostPhotoEditor.tsx
index dbdc94dda4..09f56309f9 100644
--- a/web/apps/space/src/components/PostPhotoEditor.tsx
+++ b/web/apps/space/src/components/PostPhotoEditor.tsx
@@ -1,7 +1,9 @@
import { Cancel01Icon, RotateTopRightIcon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
import { Box, Dialog } from "@mui/material";
+import { SpacePostPhotoInput } from "components/PostPhotoInput";
import { SpacePostPhotoStrip } from "components/PostPhotoStrip";
+import { SpacePostVideoEditor } from "components/PostVideoEditor";
import { SpacePhotoCrop } from "components/photo-crop/PhotoCrop";
import {
cropWithAspect,
@@ -17,6 +19,8 @@ import {
type SpacePostPhotoEdit,
type SpacePostPreviewImage,
} from "utils/post-image";
+import { maxSpacePostPhotos, movePostPhoto } from "utils/post-photos";
+import { spaceVideoCover, type SpacePostVideoEdit } from "utils/post-video";
export interface SpaceEditablePostPhoto {
id: number;
@@ -24,12 +28,21 @@ export interface SpaceEditablePostPhoto {
previewURL: string;
width: number;
height: number;
+ isLoading?: boolean;
+ preparationError?: string;
edit?: SpacePostPhotoEdit;
+ video?: {
+ file: File;
+ sourceURL: string;
+ duration: number;
+ edit: SpacePostVideoEdit;
+ };
}
export interface SpacePostPhotoEditResult {
id: number;
edit: SpacePostPhotoEdit;
+ videoEdit?: SpacePostVideoEdit;
preview?: SpacePostPreviewImage;
}
@@ -38,7 +51,6 @@ const aspects = [
{ label: "Free", value: undefined },
{ label: "Square", value: 1 },
{ label: "3:4", value: 3 / 4 },
- { label: "16:9", value: 16 / 9 },
];
const buttonSx = {
alignItems: "center",
@@ -62,24 +74,66 @@ const buttonSx = {
export const SpacePostPhotoEditor: React.FC<{
photos: SpaceEditablePostPhoto[];
initialIndex: number;
+ showPhotoStrip: boolean;
+ onAdd: (files: File[]) => void;
onClose: () => void;
- onDone: (results: SpacePostPhotoEditResult[], activeIndex: number) => void;
-}> = ({ photos, initialIndex, onClose, onDone }) => {
- const [activeIndex, setActiveIndex] = React.useState(initialIndex);
- const [edits, setEdits] = React.useState(() =>
- photos.map((photo) => photo.edit ?? originalEdit),
+ onDone: (
+ results: SpacePostPhotoEditResult[],
+ activeIndex: number,
+ photoIDs: number[],
+ ) => void;
+}> = ({
+ photos: initialPhotos,
+ initialIndex,
+ showPhotoStrip,
+ onAdd,
+ onClose,
+ onDone,
+}) => {
+ const [photos, setPhotos] = React.useState(initialPhotos);
+ const [activeID, setActiveID] = React.useState(
+ initialPhotos[initialIndex]!.id,
);
+ const [edits, setEdits] = React.useState<
+ Record<number, SpacePostPhotoEdit>
+ >({});
+ const [videoEdits, setVideoEdits] = React.useState<
+ Record<number, SpacePostVideoEdit>
+ >({});
+ const inputRef = React.useRef<HTMLInputElement>(null);
+ const knownIDs = React.useRef(new Set(initialPhotos.map(({ id }) => id)));
const [isSaving, setIsSaving] = React.useState(false);
const [error, setError] = React.useState<string>();
const mounted = React.useRef(false);
+ const activeIndex = photos.findIndex(({ id }) => id == activeID);
const photo = photos[activeIndex]!;
- const edit = edits[activeIndex]!;
+ const edit = edits[photo.id] ?? photo.edit ?? originalEdit;
+ const preparationError = photos.find(
+ (photo) => photo.preparationError,
+ )?.preparationError;
+ const isPreparing = photos.some((photo) => photo.isLoading);
+ const aspectIndex = aspects.findIndex(({ value }) => value == edit.aspect);
const size = rotatedImageSize(photo, edit.rotationDegrees);
const crop = edit.cropArea ?? fullImageCrop(size);
const isEdited = Boolean(
edit.cropArea || edit.rotationDegrees || edit.aspect,
);
+ React.useEffect(() => {
+ const added = initialPhotos.filter(
+ ({ id }) => !knownIDs.current.has(id),
+ );
+ initialPhotos.forEach(({ id }) => knownIDs.current.add(id));
+ setPhotos((current) => [
+ ...current.map(
+ (photo) =>
+ initialPhotos.find(({ id }) => id == photo.id) ?? photo,
+ ),
+ ...added,
+ ]);
+ if (added.length) setActiveID(added[0]!.id);
+ }, [initialPhotos]);
+
React.useEffect(() => {
mounted.current = true;
return () => {
@@ -95,17 +149,41 @@ export const SpacePostPhotoEditor: React.FC<{
const updateEdit = (next: SpacePostPhotoEdit) => {
setError(undefined);
- setEdits((current) =>
- current.map((item, index) => (index == activeIndex ? next : item)),
- );
+ setEdits((current) => ({ ...current, [photo.id]: next }));
+ };
+ const movePhoto = (from: number, to: number) => {
+ setPhotos((current) => movePostPhoto(current, from, to));
+ setActiveID(photos[from]!.id);
+ };
+ const removePhoto = () => {
+ const remaining = photos.filter(({ id }) => id != photo.id);
+ setPhotos(remaining);
+ setActiveID(remaining[Math.min(activeIndex, remaining.length - 1)]!.id);
+ setError(undefined);
};
const save = async () => {
setIsSaving(true);
setError(undefined);
const results: SpacePostPhotoEditResult[] = [];
try {
- for (const [index, photo] of photos.entries()) {
- const edit = edits[index]!;
+ for (const photo of photos) {
+ const edit = edits[photo.id] ?? photo.edit ?? originalEdit;
+ const videoEdit = videoEdits[photo.id] ?? photo.video?.edit;
+ if (photo.video && videoEdit) {
+ if (videoEdit === photo.video.edit) continue;
+ const cover = await spaceVideoCover(
+ photo.video.file,
+ videoEdit.coverTime,
+ );
+ const preview = {
+ url: URL.createObjectURL(cover.file),
+ width: cover.width,
+ height: cover.height,
+ };
+ results.push({ id: photo.id, edit, videoEdit, preview });
+ if (!mounted.current) return;
+ continue;
+ }
if (edit === (photo.edit ?? originalEdit)) continue;
const preview =
edit.cropArea || edit.rotationDegrees
@@ -117,7 +195,11 @@ export const SpacePostPhotoEditor: React.FC<{
results.push({ id: photo.id, edit, preview });
if (!mounted.current) return;
}
- onDone(results, activeIndex);
+ onDone(
+ results,
+ activeIndex,
+ photos.map((photo) => photo.id),
+ );
results.length = 0;
} catch (error) {
log.error("Failed to prepare edited post photos", error);
@@ -151,6 +233,11 @@ export const SpacePostPhotoEditor: React.FC<{
}}
sx={{ zIndex: 1400 }}
>
+ <SpacePostPhotoInput
+ inputRef={inputRef}
+ onSelect={onAdd}
+ remaining={maxSpacePostPhotos - photos.length}
+ />
<Box
sx={{
alignItems: "center",
@@ -165,13 +252,13 @@ export const SpacePostPhotoEditor: React.FC<{
<Box
component="button"
type="button"
- aria-label="Cancel photo edits"
+ aria-label="Cancel edits"
onClick={onClose}
sx={{
...buttonSx,
justifySelf: "start",
width: 44,
- ml: "-8px",
+ ml: "-16px",
p: 0,
}}
>
@@ -208,7 +295,9 @@ export const SpacePostPhotoEditor: React.FC<{
<Box
component="button"
type="button"
- disabled={isSaving}
+ disabled={
+ isSaving || isPreparing || Boolean(preparationError)
+ }
aria-busy={isSaving}
onClick={() => void save()}
sx={{
@@ -235,28 +324,57 @@ export const SpacePostPhotoEditor: React.FC<{
</Box>
</Box>
<Box
+ inert={isSaving}
sx={{
display: "flex",
flex: 1,
minHeight: 0,
- px: "24px",
- py: "20px",
+ px: photo.video ? 0 : "24px",
+ py: photo.video ? 0 : "20px",
maxWidth: 1000,
width: "100%",
boxSizing: "border-box",
alignSelf: "center",
}}
>
- <SpacePhotoCrop
- key={photo.id}
- imageURL={photo.imageURL}
- imageSize={photo}
- rotation={edit.rotationDegrees}
- crop={crop}
- aspect={edit.aspect}
- disabled={isSaving}
- onChange={(cropArea) => updateEdit({ ...edit, cropArea })}
- />
+ {photo.isLoading || photo.preparationError ? (
+ <Box
+ className="space-photo-placeholder"
+ role="status"
+ aria-label={
+ photo.isLoading ? "Preparing preview" : undefined
+ }
+ aria-busy={photo.isLoading || undefined}
+ sx={{ width: "100%", height: "100%" }}
+ />
+ ) : photo.video ? (
+ <SpacePostVideoEditor
+ key={photo.id}
+ file={photo.video.file}
+ sourceURL={photo.video.sourceURL}
+ duration={photo.video.duration}
+ edit={videoEdits[photo.id] ?? photo.video.edit}
+ onChange={(next) =>
+ setVideoEdits((current) => ({
+ ...current,
+ [photo.id]: next,
+ }))
+ }
+ />
+ ) : (
+ <SpacePhotoCrop
+ key={photo.id}
+ imageURL={photo.imageURL}
+ imageSize={photo}
+ rotation={edit.rotationDegrees}
+ crop={crop}
+ aspect={edit.aspect}
+ disabled={isSaving}
+ onChange={(cropArea) =>
+ updateEdit({ ...edit, cropArea })
+ }
+ />
+ )}
</Box>
<Box
sx={{
@@ -265,152 +383,174 @@ export const SpacePostPhotoEditor: React.FC<{
alignItems: "center",
gap: "12px",
px: "12px",
+ pt:
+ photo.video &&
+ (error || preparationError || showPhotoStrip)
+ ? "12px"
+ : 0,
pb: "max(16px, env(safe-area-inset-bottom))",
flexShrink: 0,
}}
>
- {error && (
+ {(error || preparationError) && (
<Box role="alert" sx={{ color: "#FF8A8A", fontSize: 13 }}>
- {error}
+ {error || preparationError}
</Box>
)}
- <Box
- sx={{
- alignItems: "center",
- display: "grid",
- gridTemplateColumns: "44px minmax(0, 1fr) 56px",
- gap: "6px",
- width: "100%",
- maxWidth: 390,
- }}
- >
- <Box
- component="button"
- type="button"
- aria-label="Rotate photo 90 degrees clockwise"
- title="Rotate 90Β°"
- disabled={isSaving}
- onClick={() => {
- const rotationDegrees =
- (edit.rotationDegrees + 90) % 360;
- const rotatedCrop = rotateImageCrop(crop, size);
- updateEdit({
- ...edit,
- rotationDegrees,
- cropArea: edit.aspect
- ? cropWithAspect(
- rotatedCrop,
- edit.aspect,
- rotatedImageSize(
- photo,
- rotationDegrees,
- ),
- )
- : edit.cropArea
- ? rotatedCrop
- : undefined,
- });
- }}
- sx={{ ...buttonSx, bgcolor: "#1C1C1E", p: 0 }}
- >
- <HugeiconsIcon
- icon={RotateTopRightIcon}
- size={20}
- strokeWidth={1.8}
- />
- </Box>
+ {!photo.video && photo.imageURL && (
<Box
- role="group"
- aria-label="Crop aspect ratio"
sx={{
- display: "flex",
- bgcolor: "#1C1C1E",
- borderRadius: "999px",
- px: "4px",
+ alignItems: "center",
+ display: "grid",
+ gridTemplateColumns: "44px minmax(0, 1fr) auto",
+ gap: "6px",
+ width: "100%",
+ maxWidth: 390,
}}
>
- {aspects.map(({ label, value }) => (
+ <Box
+ component="button"
+ type="button"
+ aria-label="Rotate photo 90 degrees clockwise"
+ title="Rotate 90Β°"
+ disabled={isSaving}
+ onClick={() => {
+ const rotationDegrees =
+ (edit.rotationDegrees + 90) % 360;
+ const rotatedCrop = rotateImageCrop(crop, size);
+ updateEdit({
+ ...edit,
+ rotationDegrees,
+ cropArea: edit.aspect
+ ? cropWithAspect(
+ rotatedCrop,
+ edit.aspect,
+ rotatedImageSize(
+ photo,
+ rotationDegrees,
+ ),
+ )
+ : edit.cropArea
+ ? rotatedCrop
+ : undefined,
+ });
+ }}
+ sx={{ ...buttonSx, bgcolor: "#1C1C1E", p: 0 }}
+ >
+ <HugeiconsIcon
+ icon={RotateTopRightIcon}
+ size={20}
+ strokeWidth={1.8}
+ />
+ </Box>
+ <Box
+ role="group"
+ aria-label="Crop aspect ratio"
+ sx={{
+ position: "relative",
+ display: "flex",
+ bgcolor: "#1C1C1E",
+ borderRadius: "999px",
+ px: "4px",
+ }}
+ >
<Box
- key={label}
- component="button"
- type="button"
- disabled={isSaving}
- aria-pressed={edit.aspect == value}
- onClick={() =>
- updateEdit({
- ...edit,
- aspect: value,
- cropArea: value
- ? cropWithAspect(crop, value, size)
- : edit.cropArea,
- })
- }
+ aria-hidden="true"
+ hidden={aspectIndex < 0}
sx={{
- ...buttonSx,
- flex: "1 0 auto",
- fontSize: 13,
- p: 0,
- color:
- edit.aspect == value
- ? "#FFFFFF"
- : "#A6A6A6",
+ position: "absolute",
+ top: 4,
+ bottom: 4,
+ left: 4,
+ width: `calc((100% - 8px) / ${aspects.length})`,
+ bgcolor: "#3A3A3C",
+ borderRadius: "999px",
+ pointerEvents: "none",
+ transform: `translateX(${aspectIndex * 100}%)`,
+ transition: "transform 200ms ease",
+ "@media (prefers-reduced-motion: reduce)": {
+ transition: "none",
+ },
}}
- >
+ />
+ {aspects.map(({ label, value }) => (
<Box
- component="span"
+ key={label}
+ component="button"
+ type="button"
+ disabled={isSaving}
+ aria-pressed={edit.aspect == value}
+ onClick={() =>
+ updateEdit({
+ ...edit,
+ aspect: value,
+ cropArea: value
+ ? cropWithAspect(
+ crop,
+ value,
+ size,
+ )
+ : edit.cropArea,
+ })
+ }
sx={{
- alignItems: "center",
- bgcolor:
+ ...buttonSx,
+ position: "relative",
+ flex: 1,
+ minWidth: 0,
+ fontSize: 13,
+ p: 0,
+ color:
edit.aspect == value
- ? "#3A3A3C"
- : "transparent",
- borderRadius: "999px",
- boxSizing: "border-box",
- display: "flex",
- height: 36,
- justifyContent: "center",
- width: "100%",
- px: "10px",
- "@media (max-width: 359px)": {
- px: "6px",
- },
+ ? "#FFFFFF"
+ : "#A6A6A6",
}}
>
{label}
</Box>
- </Box>
- ))}
- </Box>
- <Box
- component="button"
- type="button"
- title="Reset edits"
- disabled={isSaving || !isEdited}
- onClick={() => updateEdit(originalEdit)}
- sx={{
- ...buttonSx,
- bgcolor: "#1C1C1E",
- fontSize: 13,
- p: 0,
- "&:disabled": {
- opacity: 1,
- color: "#777777",
- cursor: "default",
- },
- }}
- >
- Reset
+ ))}
+ </Box>
+ <Box
+ component="button"
+ type="button"
+ title="Reset edits"
+ disabled={isSaving || !isEdited}
+ onClick={() => updateEdit(originalEdit)}
+ sx={{
+ ...buttonSx,
+ bgcolor: "#1C1C1E",
+ fontSize: 13,
+ px: "16px",
+ py: 0,
+ "&:disabled": {
+ opacity: 1,
+ color: "#777777",
+ cursor: "default",
+ },
+ }}
+ >
+ Reset
+ </Box>
</Box>
- </Box>
- {photos.length > 1 && (
+ )}
+ {showPhotoStrip && (
<Box sx={{ width: "100%", maxWidth: 390 }}>
<SpacePostPhotoStrip
activeIndex={activeIndex}
disabled={isSaving}
- onSelect={setActiveIndex}
+ onAdd={() => inputRef.current?.click()}
+ onMove={movePhoto}
+ onRemove={
+ photos.length > 1 ? removePhoto : undefined
+ }
+ onSelect={(index) => setActiveID(photos[index]!.id)}
photos={photos.map((photo) => ({
id: photo.id,
imageUrl: photo.previewURL,
+ isLoading: photo.isLoading,
+ durationMs: photo.video
+ ? photo.video.duration * 1000
+ : undefined,
}))}
/>
</Box>
diff --git a/web/apps/space/src/components/PostPhotoInput.tsx b/web/apps/space/src/components/PostPhotoInput.tsx
index f36bb5c4a7..d5ca6b3ad2 100644
--- a/web/apps/space/src/components/PostPhotoInput.tsx
+++ b/web/apps/space/src/components/PostPhotoInput.tsx
@@ -19,7 +19,7 @@ export const SpacePostPhotoInput: React.FC<{
<input
ref={inputRef}
type="file"
- accept={spacePostImageInputAccept}
+ accept={`${spacePostImageInputAccept},video/mp4,video/quicktime,video/webm,.mp4,.mov,.m4v,.webm`}
multiple
style={{ display: "none" }}
onChange={(event) => {
@@ -29,8 +29,8 @@ export const SpacePostPhotoInput: React.FC<{
if (files.length > remaining) {
setError(
remaining == maxSpacePostPhotos
- ? "Only 10 photos per post."
- : `You can only add ${remaining} more ${remaining == 1 ? "photo" : "photos"}.`,
+ ? "Only 10 photos and videos per post."
+ : `You can only add ${remaining} more ${remaining == 1 ? "item" : "items"}.`,
);
return;
}
@@ -41,7 +41,7 @@ export const SpacePostPhotoInput: React.FC<{
{error && (
<SpaceActionToast
autoDismissAfterMs={spaceToastAutoDismissDurationMs}
- closeLabel="Dismiss photo limit message"
+ closeLabel="Dismiss post limit message"
icon={
<HugeiconsIcon
icon={Alert02Icon}
diff --git a/web/apps/space/src/components/PostPhotoStrip.tsx b/web/apps/space/src/components/PostPhotoStrip.tsx
index 54446fb573..3c9d16b406 100644
--- a/web/apps/space/src/components/PostPhotoStrip.tsx
+++ b/web/apps/space/src/components/PostPhotoStrip.tsx
@@ -1,13 +1,16 @@
import { Add01Icon, Cancel01Icon } from "@hugeicons/core-free-icons";
import { HugeiconsIcon } from "@hugeicons/react";
-import { Box } from "@mui/material";
+import { Box, Skeleton } from "@mui/material";
import { keyframes } from "@mui/material/styles";
+import { SpacePostVideoBadge } from "components/PostPhotosBadge";
import React from "react";
import { maxSpacePostPhotos, movePostPhoto } from "utils/post-photos";
interface StripPhoto {
id: number;
imageUrl?: string;
+ isLoading?: boolean;
+ durationMs?: number;
}
interface PhotoDrag {
@@ -188,7 +191,7 @@ export const SpacePostPhotoStrip: React.FC<{
if (active && completed.from != completed.to) {
callbacksRef.current.onMove?.(completed.from, completed.to);
setAnnouncement(
- `Photo moved to position ${completed.to + 1} of ${photos.length}`,
+ `Item moved to position ${completed.to + 1} of ${photos.length}`,
);
}
};
@@ -222,23 +225,31 @@ export const SpacePostPhotoStrip: React.FC<{
? movePostPhoto(photos, drag.from, drag.to)
: photos;
const draggedPhoto = drag ? photos[drag.from] : undefined;
- const preview = (photo: StripPhoto) =>
- photo.imageUrl ? (
- <Box
- component="img"
- src={photo.imageUrl}
- alt=""
- draggable={false}
- sx={{
- height: "100%",
- width: "100%",
- objectFit: "cover",
- pointerEvents: "none",
- }}
- />
- ) : (
- photos.indexOf(photo) + 1
- );
+ const preview = (photo: StripPhoto) => (
+ <>
+ {photo.imageUrl ? (
+ <Box
+ component="img"
+ src={photo.imageUrl}
+ alt=""
+ draggable={false}
+ sx={{
+ height: "100%",
+ width: "100%",
+ objectFit: "cover",
+ pointerEvents: "none",
+ }}
+ />
+ ) : (
+ <Skeleton
+ animation="wave"
+ variant="rectangular"
+ sx={{ width: "100%", height: "100%", bgcolor: "#242424" }}
+ />
+ )}
+ <SpacePostVideoBadge durationMs={photo.durationMs} size={12} />
+ </>
+ );
return (
<Box sx={{ mb: "8px" }}>
@@ -300,7 +311,7 @@ export const SpacePostPhotoStrip: React.FC<{
component="button"
type="button"
data-photo-index={index}
- aria-label={`Photo ${index + 1}`}
+ aria-label={`${photo.durationMs ? "Video" : "Photo"} ${index + 1}`}
aria-pressed={
photo.id == photos[activeIndex]?.id
}
@@ -334,15 +345,16 @@ export const SpacePostPhotoStrip: React.FC<{
return;
onMove(index, to);
setAnnouncement(
- `Photo moved to position ${to + 1} of ${photos.length}`,
+ `Item moved to position ${to + 1} of ${photos.length}`,
);
}}
sx={{
...iconButtonSx,
bgcolor: "#242424",
outline:
+ photo.imageUrl &&
photo.id ==
- photos[activeIndex]?.id
+ photos[activeIndex]?.id
? "1.5px solid #FFFFFF"
: "none",
outlineOffset: "-1.5px",
@@ -365,12 +377,13 @@ export const SpacePostPhotoStrip: React.FC<{
{preview(photo)}
</Box>
{onRemove &&
+ !photo.isLoading &&
index == activeIndex &&
!dragging && (
<Box
component="button"
type="button"
- aria-label="Remove photo"
+ aria-label="Remove item"
disabled={disabled}
onClick={onRemove}
sx={{
@@ -410,7 +423,7 @@ export const SpacePostPhotoStrip: React.FC<{
<Box
component="button"
type="button"
- aria-label="Add photos"
+ aria-label="Add photos or videos"
disabled={disabled}
onClick={onAdd}
sx={{
@@ -447,8 +460,10 @@ export const SpacePostPhotoStrip: React.FC<{
role="status"
sx={{
position: "absolute",
- width: 1,
- height: 1,
+ top: 0,
+ left: 0,
+ width: "1px",
+ height: "1px",
overflow: "hidden",
clipPath: "inset(50%)",
}}
diff --git a/web/apps/space/src/components/PostPhotosBadge.tsx b/web/apps/space/src/components/PostPhotosBadge.tsx
index a2619abb6b..e763a18db2 100644
--- a/web/apps/space/src/components/PostPhotosBadge.tsx
+++ b/web/apps/space/src/components/PostPhotosBadge.tsx
@@ -10,7 +10,7 @@ export const SpacePostPhotosBadge = ({
count > 1 ? (
<Box
component="span"
- aria-label={`${count} photos`}
+ aria-label={`${count} items`}
sx={{
alignItems: "center",
color: "#FFFFFF",
@@ -46,3 +46,40 @@ export const SpacePostPhotosBadge = ({
</svg>
</Box>
) : null;
+
+export const SpacePostVideoBadge = ({
+ durationMs,
+ size,
+}: {
+ durationMs?: number;
+ size?: number;
+}) =>
+ durationMs == undefined ? null : (
+ <Box
+ component="span"
+ aria-label="Video"
+ sx={{
+ position: "absolute",
+ left: "50%",
+ top: "50%",
+ transform: "translate(-50%, -50%)",
+ zIndex: 2,
+ pointerEvents: "none",
+ color: "#FFFFFF",
+ display: "flex",
+ filter: "drop-shadow(0 1px 3px rgba(0, 0, 0, 0.65))",
+ aspectRatio: "1",
+ width: size ?? "clamp(14px, 14%, 28px)",
+ }}
+ >
+ <svg
+ aria-hidden
+ width="100%"
+ height="100%"
+ viewBox="0 0 24 24"
+ fill="currentColor"
+ >
+ <path d="M6 3v18l15-9z" />
+ </svg>
+ </Box>
+ );
diff --git a/web/apps/space/src/components/PostPhotosCounter.tsx b/web/apps/space/src/components/PostPhotosCounter.tsx
index 18a024c9aa..2769af9789 100644
--- a/web/apps/space/src/components/PostPhotosCounter.tsx
+++ b/web/apps/space/src/components/PostPhotosCounter.tsx
@@ -13,7 +13,7 @@ export const SpacePostPhotosCounter = ({
<Box
component="span"
aria-live="polite"
- aria-label={`Photo ${index + 1} of ${count}`}
+ aria-label={`Item ${index + 1} of ${count}`}
sx={{
alignItems: "center",
bgcolor: compact
diff --git a/web/apps/space/src/components/PostVideoContent.ts b/web/apps/space/src/components/PostVideoContent.ts
new file mode 100644
index 0000000000..b3604ae319
--- /dev/null
+++ b/web/apps/space/src/components/PostVideoContent.ts
@@ -0,0 +1,210 @@
+import type { SpaceViewerPhoto } from "components/FileViewer";
+import type { SpacePostAssetURLLoader } from "services/space";
+
+export const createSpaceVideoContent = (
+ item: Pick<SpaceViewerPhoto, "imageUrl" | "video">,
+ load?: SpacePostAssetURLLoader,
+ { inline = false }: { inline?: boolean } = {},
+) => {
+ const media = item.video!;
+ const element = document.createElement("div");
+ element.className = inline
+ ? "space-video-content"
+ : "pswp__content space-video-content";
+ Object.assign(element.style, {
+ position: "relative",
+ width: "100%",
+ height: "100%",
+ });
+ const video = document.createElement("video");
+ video.playsInline = true;
+ video.preload = "none";
+ video.poster = item.imageUrl;
+ video.muted = media.muted ?? false;
+ video.tabIndex = 0;
+ video.setAttribute("role", "button");
+ video.setAttribute("aria-label", "Play video");
+ Object.assign(video.style, {
+ width: "100%",
+ height: "100%",
+ objectFit: inline ? "cover" : "contain",
+ cursor: "pointer",
+ });
+ const button = document.createElement("button");
+ button.type = "button";
+ button.className = "space-video-control";
+ button.innerHTML =
+ '<svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true" style="display: block"><path d="M6 3v18l15-9z"/></svg>';
+ button.setAttribute("aria-label", "Play video");
+ button.setAttribute("aria-hidden", String(!inline));
+ const controlStyle = {
+ position: "absolute",
+ top: "50%",
+ left: "50%",
+ transform: "translate(-50%, -50%)",
+ border: "0",
+ borderRadius: "50%",
+ background: "rgba(0, 0, 0, 0.45)",
+ color: "#FFFFFF",
+ width: "44px",
+ height: "44px",
+ padding: "10px",
+ boxSizing: "border-box",
+ };
+ Object.assign(button.style, { ...controlStyle, cursor: "pointer" });
+ const loading = document.createElement("span");
+ loading.className = "space-video-control";
+ loading.setAttribute("aria-hidden", "true");
+ loading.setAttribute("role", "status");
+ loading.setAttribute("aria-label", "Loading video");
+ loading.innerHTML = '<span class="space-video-spinner"></span>';
+ Object.assign(loading.style, { ...controlStyle, pointerEvents: "none" });
+ element.append(video, button, loading);
+ let generation = 0;
+ let ownedURL: string | undefined;
+ let disposed = false;
+ let active = inline;
+ let loadingTimer: ReturnType<typeof setTimeout> | undefined;
+ const hideLoading = () => {
+ clearTimeout(loadingTimer);
+ loadingTimer = undefined;
+ loading.setAttribute("aria-hidden", "true");
+ element.removeAttribute("aria-busy");
+ };
+ const showLoading = () => {
+ if (document.activeElement == button)
+ video.focus({ preventScroll: true });
+ element.setAttribute("aria-busy", "true");
+ if (
+ loadingTimer == undefined &&
+ loading.getAttribute("aria-hidden") == "true"
+ )
+ loadingTimer = setTimeout(() => {
+ loadingTimer = undefined;
+ loading.setAttribute("aria-hidden", "false");
+ button.setAttribute("aria-hidden", "true");
+ }, 180);
+ };
+ const showPlayButton = () => {
+ hideLoading();
+ button.setAttribute("aria-hidden", String(!active));
+ button.disabled = false;
+ button.setAttribute("aria-label", "Play video");
+ video.setAttribute("aria-label", "Play video");
+ };
+ const showPlaying = () => {
+ hideLoading();
+ button.disabled = false;
+ button.setAttribute("aria-hidden", "true");
+ video.setAttribute("aria-label", "Pause video");
+ };
+ const pause = () => {
+ generation++;
+ video.pause();
+ showPlayButton();
+ };
+ const reset = () => {
+ pause();
+ video.removeAttribute("src");
+ video.load();
+ };
+ const clear = () => {
+ active = inline;
+ reset();
+ if (ownedURL) URL.revokeObjectURL(ownedURL);
+ ownedURL = undefined;
+ };
+ const visibility = () => {
+ if (document.hidden) pause();
+ };
+ document.addEventListener("visibilitychange", visibility);
+ const play = () => {
+ if (button.disabled || !video.paused) return;
+ active = true;
+ const current = ++generation;
+ showLoading();
+ button.disabled = true;
+ void (async () => {
+ let url = media.url ?? ownedURL;
+ if (!url) {
+ if (!media.asset || !load) throw new Error("Video unavailable");
+ url = await load(media.asset);
+ if (disposed || generation != current) {
+ URL.revokeObjectURL(url);
+ return;
+ }
+ ownedURL = url;
+ }
+ if (disposed || generation != current) return;
+ if (!video.getAttribute("src")) {
+ video.src = url;
+ video.currentTime = media.start ?? 0;
+ } else if (
+ video.ended ||
+ (media.end != undefined && video.currentTime >= media.end)
+ ) {
+ video.currentTime = media.start ?? 0;
+ }
+ await video.play();
+ if (generation != current) video.pause();
+ else showPlaying();
+ })().catch(() => {
+ if (!disposed && generation == current) showPlayButton();
+ });
+ };
+ const togglePlayback = () => {
+ if (button.disabled) return;
+ if (video.paused) play();
+ else pause();
+ };
+ button.onclick = togglePlayback;
+ let pointerStart = { x: 0, y: 0 };
+ video.onpointerdown = (event) => {
+ pointerStart = { x: event.clientX, y: event.clientY };
+ };
+ video.onclick = (event) => {
+ if (
+ event.detail == 0 ||
+ Math.hypot(
+ event.clientX - pointerStart.x,
+ event.clientY - pointerStart.y,
+ ) < 8
+ )
+ togglePlayback();
+ };
+ video.onkeydown = (event) => {
+ if (event.key == " " || event.key == "Enter") {
+ event.preventDefault();
+ event.stopPropagation();
+ togglePlayback();
+ }
+ };
+ video.onpause = showPlayButton;
+ video.onended = reset;
+ video.onwaiting = () => {
+ if (!video.paused) showLoading();
+ };
+ video.onplaying = showPlaying;
+ video.onerror = pause;
+ video.ontimeupdate = () => {
+ if (media.end != undefined && video.currentTime >= media.end) {
+ reset();
+ }
+ };
+ element.addEventListener("pointerdown", (event) => {
+ if (!inline && button.contains(event.target as Node))
+ event.stopPropagation();
+ });
+ return {
+ element,
+ video,
+ play,
+ pause,
+ deactivate: clear,
+ destroy: () => {
+ disposed = true;
+ clear();
+ document.removeEventListener("visibilitychange", visibility);
+ },
+ };
+};
diff --git a/web/apps/space/src/components/PostVideoEditor.tsx b/web/apps/space/src/components/PostVideoEditor.tsx
new file mode 100644
index 0000000000..213eeed270
--- /dev/null
+++ b/web/apps/space/src/components/PostVideoEditor.tsx
@@ -0,0 +1,646 @@
+import { VolumeHighIcon, VolumeOffIcon } from "@hugeicons/core-free-icons";
+import { HugeiconsIcon } from "@hugeicons/react";
+import { Box } from "@mui/material";
+import {
+ dragVideoTrim,
+ type VideoTrimHandle,
+} from "components/video-trim/geometry";
+import { createVideoTrimPlayback } from "components/video-trim/playback";
+import React from "react";
+import {
+ clampVideoCover,
+ initialSpaceVideoEdit,
+ maxSpaceVideoDuration,
+ openSpaceVideo,
+ seekSpaceVideo,
+ type SpacePostVideoEdit,
+} from "utils/post-video";
+
+const control = {
+ alignItems: "center",
+ bgcolor: "#1C1C1E",
+ border: 0,
+ borderRadius: "999px",
+ color: "#F2F2F2",
+ cursor: "pointer",
+ display: "inline-flex",
+ font: "inherit",
+ fontSize: 13,
+ fontWeight: 600,
+ justifyContent: "center",
+ minHeight: 44,
+ px: "12px",
+ "&:disabled": { color: "#777777", cursor: "default" },
+ "&:focus-visible": { outline: "2px solid #08C225", outlineOffset: 2 },
+};
+const bound = (value: number, min: number, max: number) =>
+ Math.max(min, Math.min(max, value));
+type DragMode = VideoTrimHandle | "cover";
+
+export const SpacePostVideoEditor: React.FC<{
+ file: File;
+ sourceURL: string;
+ duration: number;
+ edit: SpacePostVideoEdit;
+ onChange: (edit: SpacePostVideoEdit) => void;
+}> = ({ file, sourceURL, duration, edit, onChange }) => {
+ const [mode, setMode] = React.useState<"trim" | "cover">("trim");
+ const [frames, setFrames] = React.useState<string[]>([]);
+ const [playing, setPlaying] = React.useState(false);
+ const [previewError, setPreviewError] = React.useState<string>();
+ const videoRef = React.useRef<HTMLVideoElement>(null);
+ const timelineRef = React.useRef<HTMLDivElement>(null);
+ const latest = React.useRef({ edit, onChange });
+ latest.current = { edit, onChange };
+ const playback =
+ React.useRef<ReturnType<typeof createVideoTrimPlayback>>(undefined);
+ const drag = React.useRef<
+ | {
+ mode: DragMode;
+ grabOffset: number;
+ edit: SpacePostVideoEdit;
+ pointerID: number;
+ }
+ | undefined
+ >(undefined);
+ const minimum = Math.min(1 / 30, duration);
+ const originalEdit = initialSpaceVideoEdit(duration);
+ const isEdited =
+ edit.start != originalEdit.start ||
+ edit.end != originalEdit.end ||
+ edit.coverTime != originalEdit.coverTime ||
+ edit.muted != originalEdit.muted;
+ const span = mode == "cover" ? edit.end - edit.start : duration;
+ const visibleStart = mode == "cover" ? edit.start : 0;
+ const percent = (time: number) => (100 * (time - visibleStart)) / span;
+
+ React.useEffect(() => {
+ const controller = createVideoTrimPlayback(
+ videoRef.current!,
+ () => latest.current.edit,
+ () => setPreviewError("Couldn't play this video preview."),
+ );
+ playback.current = controller;
+ return () => {
+ controller.dispose();
+ playback.current = undefined;
+ };
+ }, []);
+
+ const seek = React.useCallback((time: number) => {
+ playback.current?.seek(time);
+ }, []);
+
+ const togglePlayback = () => {
+ setPreviewError(undefined);
+ playback.current?.toggle();
+ };
+
+ React.useEffect(() => {
+ seek(
+ mode == "cover"
+ ? latest.current.edit.coverTime
+ : latest.current.edit.start,
+ );
+ }, [mode, seek]);
+
+ React.useEffect(() => {
+ const video = videoRef.current!;
+ const pause = () => {
+ if (document.hidden) video.pause();
+ };
+ document.addEventListener("visibilitychange", pause);
+ return () => {
+ video.pause();
+ document.removeEventListener("visibilitychange", pause);
+ };
+ }, []);
+
+ React.useEffect(() => {
+ const controller = new AbortController();
+ const { signal } = controller;
+ const urls: string[] = [];
+ const timer = window.setTimeout(() => {
+ void (async () => {
+ const { video, dispose } = await openSpaceVideo(file, signal);
+ try {
+ const times = Array.from(
+ { length: 8 },
+ (_, i) => visibleStart + (span * (i + 0.5)) / 8,
+ );
+ for (const time of times) {
+ await seekSpaceVideo(video, time, signal);
+ const canvas = document.createElement("canvas");
+ canvas.width = 80;
+ canvas.height = 64;
+ const scale = Math.max(
+ 80 / video.videoWidth,
+ 64 / video.videoHeight,
+ );
+ const width = video.videoWidth * scale;
+ const height = video.videoHeight * scale;
+ canvas
+ .getContext("2d")!
+ .drawImage(
+ video,
+ (80 - width) / 2,
+ (64 - height) / 2,
+ width,
+ height,
+ );
+ urls.push(canvas.toDataURL("image/jpeg", 0.65));
+ }
+ if (!signal.aborted) setFrames(urls);
+ } finally {
+ dispose();
+ }
+ })().catch(() => {
+ if (!signal.aborted) setFrames([]);
+ });
+ }, 120);
+ return () => {
+ controller.abort();
+ window.clearTimeout(timer);
+ };
+ }, [file, visibleStart, span]);
+
+ const changeBoundary = (boundary: "start" | "end", value: number) => {
+ if (!Number.isFinite(value)) return;
+ const next = clampVideoCover({
+ ...edit,
+ ...dragVideoTrim(
+ edit,
+ boundary,
+ value,
+ duration,
+ maxSpaceVideoDuration,
+ ),
+ });
+ onChange(next);
+ seek(
+ boundary == "start"
+ ? next.start
+ : Math.max(next.start, next.end - minimum),
+ );
+ };
+
+ const updateDrag = (clientX: number) => {
+ const gesture = drag.current;
+ const rect = timelineRef.current?.getBoundingClientRect();
+ if (!gesture || !rect) return;
+ let next = { ...gesture.edit };
+ if (gesture.mode == "cover")
+ next.coverTime =
+ visibleStart +
+ bound((clientX - rect.left) / rect.width, 0, 1) * span;
+ else {
+ const position =
+ ((clientX - rect.left) / rect.width) * duration -
+ gesture.grabOffset;
+ Object.assign(
+ next,
+ dragVideoTrim(
+ next,
+ gesture.mode,
+ position,
+ duration,
+ maxSpaceVideoDuration,
+ ),
+ );
+ }
+ next = clampVideoCover(next);
+ const previous = latest.current.edit;
+ const changed =
+ next.start != previous.start ||
+ next.end != previous.end ||
+ next.coverTime != previous.coverTime;
+ if (changed) {
+ latest.current.edit = next;
+ latest.current.onChange(next);
+ }
+ if (changed || gesture.mode == "cover")
+ seek(
+ gesture.mode == "cover"
+ ? next.coverTime
+ : gesture.mode == "end"
+ ? Math.max(next.start, next.end - minimum)
+ : next.start,
+ );
+ };
+
+ const startDrag = (event: React.PointerEvent<HTMLDivElement>) => {
+ if (!event.isPrimary || event.button != 0 || drag.current) return;
+ const rect = event.currentTarget.getBoundingClientRect();
+ const x = event.clientX - rect.left;
+ const left = (percent(edit.start) / 100) * rect.width;
+ const right = (percent(edit.end) / 100) * rect.width;
+ const handle =
+ Math.min(Math.abs(x - left), Math.abs(x - right)) <=
+ Math.min(22, (right - left) / 3);
+ const action =
+ mode == "cover"
+ ? "cover"
+ : handle
+ ? Math.abs(x - left) <= Math.abs(x - right)
+ ? "start"
+ : "end"
+ : x > left && x < right
+ ? "move"
+ : undefined;
+ if (!action) return;
+ event.preventDefault();
+ drag.current = {
+ mode: action,
+ grabOffset:
+ visibleStart +
+ (x / rect.width) * span -
+ (action == "end" ? edit.end : edit.start),
+ edit,
+ pointerID: event.pointerId,
+ };
+ event.currentTarget.setPointerCapture(event.pointerId);
+ videoRef.current?.pause();
+ updateDrag(event.clientX);
+ };
+
+ const stopDrag = () => {
+ drag.current = undefined;
+ };
+
+ return (
+ <Box
+ sx={{
+ display: "flex",
+ flexDirection: "column",
+ flex: 1,
+ minHeight: 0,
+ width: "100%",
+ }}
+ >
+ <Box
+ sx={{
+ flex: 1,
+ minHeight: 0,
+ display: "flex",
+ justifyContent: "center",
+ position: "relative",
+ px: "24px",
+ py: "20px",
+ }}
+ >
+ <video
+ aria-label={playing ? "Pause video" : "Play video"}
+ role="button"
+ tabIndex={0}
+ ref={videoRef}
+ src={sourceURL}
+ playsInline
+ muted={edit.muted}
+ style={{
+ width: "100%",
+ height: "100%",
+ objectFit: "contain",
+ cursor: "pointer",
+ }}
+ onClick={togglePlayback}
+ onKeyDown={(event) => {
+ if (event.key == " " || event.key == "Enter") {
+ event.preventDefault();
+ togglePlayback();
+ }
+ }}
+ onPlay={() => setPlaying(true)}
+ onPause={() => setPlaying(false)}
+ onEnded={() => setPlaying(false)}
+ onError={() =>
+ setPreviewError("Couldn't play this video preview.")
+ }
+ />
+ {!playing && (
+ <Box
+ component="button"
+ type="button"
+ aria-label="Play video"
+ onClick={() => {
+ videoRef.current!.focus({ preventScroll: true });
+ togglePlayback();
+ }}
+ sx={{
+ position: "absolute",
+ top: "50%",
+ left: "50%",
+ transform: "translate(-50%, -50%)",
+ border: 0,
+ borderRadius: "50%",
+ bgcolor: "rgba(0, 0, 0, 0.45)",
+ color: "#FFFFFF",
+ width: 44,
+ height: 44,
+ p: "10px",
+ cursor: "pointer",
+ "&:focus-visible": {
+ outline: "2px solid #08C225",
+ outlineOffset: 2,
+ },
+ }}
+ >
+ <svg
+ width={24}
+ height={24}
+ viewBox="0 0 24 24"
+ fill="currentColor"
+ aria-hidden
+ style={{ display: "block" }}
+ >
+ <path d="M6 3v18l15-9z" />
+ </svg>
+ </Box>
+ )}
+ </Box>
+ <Box
+ sx={{
+ width: "100%",
+ maxWidth: 414,
+ boxSizing: "border-box",
+ px: "12px",
+ alignSelf: "center",
+ flexShrink: 0,
+ display: "flex",
+ flexDirection: "column",
+ gap: "16px",
+ }}
+ >
+ {previewError && (
+ <Box role="alert" sx={{ color: "#FF8A8A", fontSize: 13 }}>
+ {previewError}
+ </Box>
+ )}
+ <Box sx={{ mx: "10px" }}>
+ <Box
+ ref={timelineRef}
+ onPointerDown={startDrag}
+ onPointerMove={(event) => {
+ if (drag.current?.pointerID == event.pointerId)
+ updateDrag(event.clientX);
+ }}
+ onPointerUp={(event) => {
+ if (drag.current?.pointerID != event.pointerId)
+ return;
+ updateDrag(event.clientX);
+ stopDrag();
+ if (
+ event.currentTarget.hasPointerCapture(
+ event.pointerId,
+ )
+ )
+ event.currentTarget.releasePointerCapture(
+ event.pointerId,
+ );
+ }}
+ onPointerCancel={stopDrag}
+ onLostPointerCapture={stopDrag}
+ sx={{
+ height: 64,
+ position: "relative",
+ bgcolor: "#1C1C1E",
+ borderRadius: "8px",
+ touchAction: "none",
+ userSelect: "none",
+ }}
+ >
+ <Box
+ sx={{
+ display: "flex",
+ width: "100%",
+ height: "100%",
+ overflow: "hidden",
+ borderRadius: "8px",
+ pointerEvents: "none",
+ "&::after":
+ mode == "trim"
+ ? {
+ content: '""',
+ position: "absolute",
+ inset: 0,
+ borderRadius: "8px",
+ background: `linear-gradient(to right, #0008 ${bound(percent(edit.start), 0, 100)}%, transparent ${bound(percent(edit.start), 0, 100)}%, transparent ${bound(percent(edit.end), 0, 100)}%, #0008 ${bound(percent(edit.end), 0, 100)}%)`,
+ }
+ : undefined,
+ }}
+ >
+ {frames.map((url, index) => (
+ <img
+ key={index}
+ src={url}
+ alt=""
+ draggable={false}
+ style={{
+ width: "12.5%",
+ objectFit: "cover",
+ }}
+ />
+ ))}
+ </Box>
+ {mode == "trim" && (
+ <Box
+ sx={{
+ position: "absolute",
+ insetBlock: 0,
+ left: `${bound(percent(edit.start), 0, 100)}%`,
+ right: `${100 - bound(percent(edit.end), 0, 100)}%`,
+ borderBlock: "3px solid white",
+ boxSizing: "border-box",
+ cursor: "grab",
+ }}
+ />
+ )}
+ {(mode == "trim"
+ ? (["start", "end"] as const)
+ : (["coverTime"] as const)
+ ).map((boundary) => (
+ <Box
+ key={boundary}
+ role="slider"
+ tabIndex={0}
+ aria-label={
+ boundary == "coverTime"
+ ? "Cover frame"
+ : boundary == "start"
+ ? "Trim start"
+ : "Trim end"
+ }
+ aria-valuemin={
+ boundary == "end"
+ ? edit.start + minimum
+ : boundary == "start"
+ ? Math.max(
+ 0,
+ edit.end -
+ maxSpaceVideoDuration,
+ )
+ : edit.start
+ }
+ aria-valuemax={
+ boundary == "start"
+ ? edit.end - minimum
+ : boundary == "end"
+ ? Math.min(
+ duration,
+ edit.start +
+ maxSpaceVideoDuration,
+ )
+ : edit.end - 0.001
+ }
+ aria-valuenow={edit[boundary]}
+ aria-valuetext={`${edit[boundary].toFixed(2)} seconds`}
+ onKeyDown={(event) => {
+ if (
+ ![
+ "ArrowLeft",
+ "ArrowRight",
+ "Home",
+ "End",
+ ].includes(event.key)
+ )
+ return;
+ event.preventDefault();
+ event.stopPropagation();
+ const value =
+ event.key == "Home"
+ ? 0
+ : event.key == "End"
+ ? duration
+ : edit[boundary] +
+ (event.key == "ArrowLeft"
+ ? -1
+ : 1) *
+ (event.shiftKey ? 1 : 0.1);
+ if (boundary == "coverTime") {
+ const next = clampVideoCover({
+ ...edit,
+ coverTime: value,
+ });
+ onChange(next);
+ seek(next.coverTime);
+ } else changeBoundary(boundary, value);
+ }}
+ sx={{
+ position: "absolute",
+ top: -4,
+ bottom: -4,
+ width: 44,
+ left: `calc(${bound(percent(edit[boundary]), 0, 100)}% - 22px)`,
+ cursor: "ew-resize",
+ display: "flex",
+ justifyContent: "center",
+ "&:focus-visible": {
+ outline: "2px solid #08C225",
+ },
+ }}
+ >
+ <Box
+ sx={{
+ width: mode == "trim" ? 14 : 4,
+ bgcolor: "white",
+ borderRadius: "4px",
+ border: "1px solid #777",
+ boxSizing: "border-box",
+ }}
+ />
+ </Box>
+ ))}
+ </Box>
+ </Box>
+ <Box
+ sx={{
+ display: "grid",
+ gridTemplateColumns: "44px minmax(0, 1fr) auto",
+ gap: "6px",
+ }}
+ >
+ <Box
+ component="button"
+ type="button"
+ sx={{ ...control, p: 0 }}
+ aria-label={edit.muted ? "Sound off" : "Sound on"}
+ title={edit.muted ? "Turn sound on" : "Turn sound off"}
+ aria-pressed={!edit.muted}
+ onClick={() =>
+ onChange({ ...edit, muted: !edit.muted })
+ }
+ >
+ <HugeiconsIcon
+ icon={edit.muted ? VolumeOffIcon : VolumeHighIcon}
+ size={20}
+ strokeWidth={1.8}
+ />
+ </Box>
+ <Box
+ role="group"
+ aria-label="Video editing mode"
+ sx={{
+ position: "relative",
+ display: "flex",
+ bgcolor: "#1C1C1E",
+ borderRadius: "999px",
+ px: "4px",
+ }}
+ >
+ <Box
+ aria-hidden="true"
+ sx={{
+ position: "absolute",
+ top: 4,
+ bottom: 4,
+ left: 4,
+ width: "calc((100% - 8px) / 2)",
+ bgcolor: "#3A3A3C",
+ borderRadius: "999px",
+ pointerEvents: "none",
+ transform: `translateX(${mode == "trim" ? 0 : 100}%)`,
+ transition: "transform 200ms ease",
+ "@media (prefers-reduced-motion: reduce)": {
+ transition: "none",
+ },
+ }}
+ />
+ {(["trim", "cover"] as const).map((value) => (
+ <Box
+ key={value}
+ component="button"
+ type="button"
+ aria-pressed={mode == value}
+ onClick={() => setMode(value)}
+ sx={{
+ ...control,
+ position: "relative",
+ flex: 1,
+ minWidth: 0,
+ p: 0,
+ bgcolor: "transparent",
+ color:
+ mode == value ? "#FFFFFF" : "#A6A6A6",
+ }}
+ >
+ {value == "trim" ? "Trim" : "Cover"}
+ </Box>
+ ))}
+ </Box>
+ <Box
+ component="button"
+ type="button"
+ title="Reset edits"
+ disabled={!isEdited}
+ sx={{ ...control, px: "16px", py: 0 }}
+ onClick={() => {
+ onChange(originalEdit);
+ seek(0);
+ }}
+ >
+ Reset
+ </Box>
+ </Box>
+ </Box>
+ </Box>
+ );
+};
diff --git a/web/apps/space/src/components/video-trim/geometry.ts b/web/apps/space/src/components/video-trim/geometry.ts
new file mode 100644
index 0000000000..c00dd1cba8
--- /dev/null
+++ b/web/apps/space/src/components/video-trim/geometry.ts
@@ -0,0 +1,40 @@
+export type VideoTrimHandle = "start" | "end" | "move";
+
+interface VideoTrimRange {
+ start: number;
+ end: number;
+}
+
+const bound = (value: number, min: number, max: number) =>
+ Math.max(min, Math.min(max, value));
+
+export const dragVideoTrim = (
+ range: VideoTrimRange,
+ handle: VideoTrimHandle,
+ position: number,
+ duration: number,
+ maximum: number,
+): VideoTrimRange => {
+ const minimum = Math.min(1 / 30, duration);
+ if (handle == "start")
+ return {
+ start: bound(
+ position,
+ Math.max(0, range.end - maximum),
+ range.end - minimum,
+ ),
+ end: range.end,
+ };
+ if (handle == "end")
+ return {
+ start: range.start,
+ end: bound(
+ position,
+ range.start + minimum,
+ Math.min(duration, range.start + maximum),
+ ),
+ };
+ const length = range.end - range.start;
+ const start = bound(position, 0, duration - length);
+ return { start, end: start + length };
+};
diff --git a/web/apps/space/src/components/video-trim/playback.ts b/web/apps/space/src/components/video-trim/playback.ts
new file mode 100644
index 0000000000..ba2e8edf5d
--- /dev/null
+++ b/web/apps/space/src/components/video-trim/playback.ts
@@ -0,0 +1,117 @@
+export const createVideoTrimPlayback = (
+ video: HTMLVideoElement,
+ getRange: () => { start: number; end: number },
+ onError: () => void,
+) => {
+ let pendingSeek: number | undefined;
+ let frame: number | undefined;
+ let restartOnPlay = true;
+ let playRequested = false;
+ let generation = 0;
+
+ const cancelFrame = () => {
+ if (frame != undefined) cancelAnimationFrame(frame);
+ frame = undefined;
+ };
+
+ const pause = () => {
+ generation++;
+ playRequested = false;
+ video.pause();
+ cancelFrame();
+ };
+
+ const applySeek = () => {
+ if (pendingSeek == undefined || video.seeking || video.readyState < 1)
+ return;
+ const time = pendingSeek;
+ pendingSeek = undefined;
+ if (Math.abs(video.currentTime - time) >= 0.002)
+ video.currentTime = time;
+ };
+
+ const seek = (time: number) => {
+ pause();
+ restartOnPlay = true;
+ pendingSeek = time;
+ applySeek();
+ };
+
+ const checkRange = () => {
+ if (video.paused || video.seeking) return;
+ const { start, end } = getRange();
+ if (video.currentTime >= end) seek(start);
+ else if (video.currentTime < start) video.currentTime = start;
+ };
+
+ const checkFrame = () => {
+ frame = undefined;
+ checkRange();
+ if (!video.paused) frame = requestAnimationFrame(checkFrame);
+ };
+
+ const onPlay = () => {
+ if (!playRequested) {
+ video.pause();
+ return;
+ }
+ cancelFrame();
+ checkFrame();
+ };
+
+ const onPause = () => {
+ if (!video.paused) return;
+ generation++;
+ playRequested = false;
+ cancelFrame();
+ };
+
+ const onEnded = () => seek(getRange().start);
+
+ const toggle = () => {
+ if (playRequested || !video.paused) {
+ pause();
+ return;
+ }
+ const { start, end } = getRange();
+ const time =
+ restartOnPlay ||
+ video.currentTime < start ||
+ video.currentTime >= end
+ ? start
+ : video.currentTime;
+ restartOnPlay = false;
+ pendingSeek = undefined;
+ if (video.readyState < 1) pendingSeek = time;
+ else if (Math.abs(video.currentTime - time) >= 0.002)
+ video.currentTime = time;
+ const current = ++generation;
+ playRequested = true;
+ void video.play().catch(() => {
+ if (generation != current) return;
+ pause();
+ onError();
+ });
+ };
+
+ video.addEventListener("play", onPlay);
+ video.addEventListener("pause", onPause);
+ video.addEventListener("ended", onEnded);
+ video.addEventListener("timeupdate", checkRange);
+ video.addEventListener("seeked", applySeek);
+ video.addEventListener("loadedmetadata", applySeek);
+
+ return {
+ toggle,
+ seek,
+ dispose: () => {
+ pause();
+ video.removeEventListener("play", onPlay);
+ video.removeEventListener("pause", onPause);
+ video.removeEventListener("ended", onEnded);
+ video.removeEventListener("timeupdate", checkRange);
+ video.removeEventListener("seeked", applySeek);
+ video.removeEventListener("loadedmetadata", applySeek);
+ },
+ };
+};
diff --git a/web/apps/space/src/pages/app/profile.tsx b/web/apps/space/src/pages/app/profile.tsx
index f9811dae53..c465c96641 100644
--- a/web/apps/space/src/pages/app/profile.tsx
+++ b/web/apps/space/src/pages/app/profile.tsx
@@ -127,8 +127,8 @@ const Page: React.FC = () => {
showPostLoadingIndicator={showInitialPostLoadingIndicator}
onBack={() => void router.push(spaceRoutes.home)}
onPostSubmitted={() => void router.push(spaceRoutes.home)}
- onCreatePost={async (image, caption) => {
- await publishPost(image, caption);
+ onCreatePost={async (images, caption) => {
+ await publishPost(images, caption);
}}
onDeletePost={async (postId) => {
const spaceId = profile.spaceId;
diff --git a/web/apps/space/src/screens/HomeScreen.tsx b/web/apps/space/src/screens/HomeScreen.tsx
index 5798cee8b0..eafbeaa605 100644
--- a/web/apps/space/src/screens/HomeScreen.tsx
+++ b/web/apps/space/src/screens/HomeScreen.tsx
@@ -16,6 +16,7 @@ import {
type SpaceViewerPostActionMode,
} from "components/FileViewer";
import { SpaceHomeHeader } from "components/HomeHeader";
+import { SpaceInlinePostVideo } from "components/InlinePostVideo";
import {
spacePostLikeButtonPop,
spacePostLikeHeartPop,
@@ -39,6 +40,7 @@ import {
type SpacePostAssetURLLoader,
type SpacePostAvatarURLLoader,
type SpacePostPhoto,
+ type SpacePostVideo,
} from "services/space";
import type { LocalSpaceFeedPost } from "state/app-state";
import { spaceEmptyStateButtonSx } from "styles/buttons";
@@ -378,10 +380,12 @@ interface FeedItemProps {
imageUrl?: string;
isAvatarPending: boolean;
isOwnPost: boolean;
+ isViewerOpen?: boolean;
isUnavailable?: boolean;
name: string;
onLoadAvatar?: () => Promise<string | null | undefined>;
onLoadImage?: (index: number) => Promise<string | undefined>;
+ onLoadVideo?: SpacePostAssetURLLoader;
onOpenFriend?: (friendID: string, username?: string) => void;
onOpenPhoto?: (photo: SpaceViewerPhoto, focusReplyOnOpen?: boolean) => void;
onOpenProfile?: () => void;
@@ -652,11 +656,13 @@ const FeedPhotoOverlay: React.FC<{
};
const FeedPhoto: React.FC<{
+ video?: SpacePostVideo;
imageUrl?: string;
isActive: boolean;
isUnavailable: boolean;
name: string;
onLoadImage?: () => Promise<string | undefined>;
+ onLoadVideo?: SpacePostAssetURLLoader;
onOpenPhoto?: () => void;
shouldLoad: boolean;
thumbHash?: string;
@@ -666,9 +672,11 @@ const FeedPhoto: React.FC<{
isUnavailable,
name,
onLoadImage,
+ onLoadVideo,
onOpenPhoto,
shouldLoad,
thumbHash,
+ video,
}) => {
const decodedPhoto = useDecodedImage(imageUrl, true);
const isPostUnavailable = isUnavailable || Boolean(decodedPhoto.failed);
@@ -700,14 +708,24 @@ const FeedPhoto: React.FC<{
return (
<Box
- component="button"
- type="button"
+ component={video ? "div" : "button"}
+ type={video ? undefined : "button"}
aria-label={
- isPostUnavailable ? "Post unavailable" : `Open ${name} photo`
+ isPostUnavailable
+ ? "Post unavailable"
+ : video
+ ? undefined
+ : `Open ${name} photo`
}
- disabled={!canOpenPhoto}
- tabIndex={isActive ? 0 : -1}
- onClick={onOpenPhoto}
+ disabled={video ? undefined : !canOpenPhoto}
+ tabIndex={!video && isActive ? 0 : -1}
+ onClick={video ? undefined : onOpenPhoto}
+ onFocus={(event: React.FocusEvent<HTMLElement>) => {
+ if (video && event.target == event.currentTarget)
+ event.currentTarget
+ .querySelector("video")
+ ?.focus({ preventScroll: true });
+ }}
sx={{
appearance: "none",
bgcolor: "transparent",
@@ -789,6 +807,14 @@ const FeedPhoto: React.FC<{
}}
/>
)}
+ {!isPostUnavailable && isPhotoReady && video && (
+ <SpaceInlinePostVideo
+ imageUrl={displayImageUrl!}
+ video={video}
+ isActive={isActive}
+ onLoadVideo={onLoadVideo}
+ />
+ )}
{isPostUnavailable && (
<Box
sx={{
@@ -822,10 +848,12 @@ const FeedItem: React.FC<FeedItemProps> = ({
imageUrl,
isAvatarPending,
isOwnPost,
+ isViewerOpen,
isUnavailable = false,
name,
onLoadAvatar,
onLoadImage,
+ onLoadVideo,
onOpenFriend,
onOpenPhoto,
onOpenProfile,
@@ -1284,7 +1312,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
<FeedPhoto
key={index}
imageUrl={photo.imageUrl}
- isActive={index == photoIndex}
+ isActive={index == photoIndex && !isViewerOpen}
isUnavailable={
isUnavailable || Boolean(photo.isUnavailable)
}
@@ -1294,6 +1322,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
? () => onLoadImage(index)
: undefined
}
+ onLoadVideo={onLoadVideo}
onOpenPhoto={
onOpenPhoto
? () => openPhoto(false, index)
@@ -1304,6 +1333,7 @@ const FeedItem: React.FC<FeedItemProps> = ({
Math.abs(index - photoIndex) <= 1
}
thumbHash={photo.thumbHash}
+ video={photo.video}
/>
))}
</Box>
@@ -1997,6 +2027,7 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
isOwnPost={
Boolean(viewerSpaceId) && item.spaceId == viewerSpaceId
}
+ isViewerOpen={Boolean(selectedViewer)}
isUnavailable={isUnavailable}
name={item.name}
onLoadAvatar={
@@ -2005,6 +2036,7 @@ export const HomeScreen: React.FC<HomeScreenProps> = ({
: undefined
}
onLoadImage={(index) => loadFeedPostImage(postPhotos[index]!)}
+ onLoadVideo={onLoadPostImage}
onOpenFriend={onOpenFriend}
onOpenPhoto={(photo, focusReply) =>
openFeedPhoto({ ...item, photos }, photo, focusReply)
diff --git a/web/apps/space/src/screens/MessagesScreen.tsx b/web/apps/space/src/screens/MessagesScreen.tsx
index d0bb0d7c6a..9880584c21 100644
--- a/web/apps/space/src/screens/MessagesScreen.tsx
+++ b/web/apps/space/src/screens/MessagesScreen.tsx
@@ -23,7 +23,10 @@ import {
MessageReactionPicker,
} from "components/MessageReactionPicker";
import { SpacePostPhotoInput } from "components/PostPhotoInput";
-import { SpacePostPhotosBadge } from "components/PostPhotosBadge";
+import {
+ SpacePostPhotosBadge,
+ SpacePostVideoBadge,
+} from "components/PostPhotosBadge";
import { SpaceLoadingSpinner } from "components/RouteFallback";
import { SpaceShareInviteButton } from "components/ShareInviteButton";
import { SpaceSkipLink } from "components/SkipLink";
@@ -728,18 +731,31 @@ const ConversationListItem: React.FC<{
{showPostThumbnailSlot &&
(postThumbnailUrl ? (
<Box
- component="img"
- alt=""
- src={postThumbnailUrl}
sx={{
- borderRadius: "20%",
- display: "block",
- height: 44,
- objectFit: "cover",
- objectPosition: "center",
+ position: "relative",
width: 44,
+ height: 44,
}}
- />
+ >
+ <Box
+ component="img"
+ alt=""
+ src={postThumbnailUrl}
+ sx={{
+ borderRadius: "20%",
+ display: "block",
+ height: "100%",
+ objectFit: "cover",
+ objectPosition: "center",
+ width: "100%",
+ }}
+ />
+ <SpacePostVideoBadge
+ durationMs={
+ (activityPost ?? post)?.durationMs
+ }
+ />
+ </Box>
) : (
<Box
aria-label={
@@ -1359,6 +1375,9 @@ const PostQuotePreview: React.FC<{
</Box>
)}
{canOpen && <SpacePostPhotosBadge count={photoCount} />}
+ {canOpen && (
+ <SpacePostVideoBadge durationMs={loadedQuote?.durationMs} />
+ )}
</Box>
</QuoteFrame>
);
diff --git a/web/apps/space/src/screens/ProfileScreen.tsx b/web/apps/space/src/screens/ProfileScreen.tsx
index b0c085b5dc..a654a89185 100644
--- a/web/apps/space/src/screens/ProfileScreen.tsx
+++ b/web/apps/space/src/screens/ProfileScreen.tsx
@@ -28,7 +28,10 @@ import {
} from "components/FileViewer";
import { SpacePostComposer } from "components/PostComposer";
import { SpacePostPhotoInput } from "components/PostPhotoInput";
-import { SpacePostPhotosBadge } from "components/PostPhotosBadge";
+import {
+ SpacePostPhotosBadge,
+ SpacePostVideoBadge,
+} from "components/PostPhotosBadge";
import { SpaceLoadingSpinner } from "components/RouteFallback";
import { SpaceShareIcon } from "components/ShareInviteButton";
import { SpaceSkipLink } from "components/SkipLink";
@@ -405,7 +408,13 @@ const ProfilePostTile: React.FC<ProfilePostTileProps> = ({
/>
) : null}
{!isUnavailable && (
- <SpacePostPhotosBadge count={photoCount} inset={8} />
+ <>
+ <SpacePostPhotosBadge count={photoCount} inset={8} />
+ <SpacePostVideoBadge
+ durationMs={item.photos?.[0]?.video?.durationMs}
+ size={18}
+ />
+ </>
)}
{isUnavailable && (
<Box
diff --git a/web/apps/space/src/services/feed-cache.ts b/web/apps/space/src/services/feed-cache.ts
index 17421c641d..2feecc1c49 100644
--- a/web/apps/space/src/services/feed-cache.ts
+++ b/web/apps/space/src/services/feed-cache.ts
@@ -6,7 +6,7 @@ import { CachedSpacePost } from "services/post-cache";
import type { SpacePost, SpacePostPage } from "services/space";
import { z } from "zod";
-const spaceFeedCacheVersion = 2;
+const spaceFeedCacheVersion = 3;
const spaceFeedCacheSize = 10;
const SpaceFeedCacheSnapshotSchema = z.object({
diff --git a/web/apps/space/src/services/media-cache.ts b/web/apps/space/src/services/media-cache.ts
index c43d1389f1..03f69add24 100644
--- a/web/apps/space/src/services/media-cache.ts
+++ b/web/apps/space/src/services/media-cache.ts
@@ -91,6 +91,8 @@ export const cachedSpaceMediaBlobURL = async (
load: () => Promise<Uint8Array>,
mediaType?: string,
) => {
+ if (mediaType?.startsWith("video/"))
+ return URL.createObjectURL(blobForBytes(await load(), mediaType));
const storageKey = await spaceMediaStorageKey(cacheKey);
const cached = spaceMediaURLCache.get(storageKey);
if (cached) {
diff --git a/web/apps/space/src/services/post-cache.ts b/web/apps/space/src/services/post-cache.ts
index 15016e71c2..f8e31c8ba2 100644
--- a/web/apps/space/src/services/post-cache.ts
+++ b/web/apps/space/src/services/post-cache.ts
@@ -9,6 +9,11 @@ const CachedSpacePostAsset = z.object({
spaceId: z.string(),
});
+const CachedSpacePostVideo = z.object({
+ asset: CachedSpacePostAsset,
+ durationMs: z.number(),
+});
+
export const CachedSpacePost = z.object({
avatarKeyVersion: z.number().optional(),
avatarObjectID: z.string().optional(),
@@ -17,11 +22,13 @@ export const CachedSpacePost = z.object({
caption: z.string().optional(),
friendID: z.string(),
height: z.number().optional(),
+ video: CachedSpacePostVideo.optional(),
imageAsset: CachedSpacePostAsset.optional(),
photos: z
.array(
z.object({
height: z.number().optional(),
+ video: CachedSpacePostVideo.optional(),
imageAsset: CachedSpacePostAsset.optional(),
thumbHash: z.string().optional(),
width: z.number().optional(),
diff --git a/web/apps/space/src/services/space.ts b/web/apps/space/src/services/space.ts
index a4dc8b2946..ad58bae494 100644
--- a/web/apps/space/src/services/space.ts
+++ b/web/apps/space/src/services/space.ts
@@ -3,6 +3,7 @@ import { clientPackageName, desktopAppVersion, isDesktop } from "ente-base/app";
import { isNamedError } from "ente-base/error";
import log from "ente-base/log";
import { apiOrigin } from "ente-base/origins";
+import type { UploadedPostAsset } from "ente-space-wasm";
import {
openSpaceLinkContext,
type DecryptedSpaceProfile,
@@ -71,7 +72,17 @@ interface SpacePostBase {
export type SpacePostAsset = PostAsset & { mediaType?: string };
+export interface SpacePostVideo {
+ asset?: SpacePostAsset;
+ url?: string;
+ durationMs: number;
+ start?: number;
+ end?: number;
+ muted?: boolean;
+}
+
export interface SpacePostPhoto {
+ video?: SpacePostVideo;
height?: number;
imageAsset?: SpacePostAsset;
imageUrl?: string;
@@ -134,6 +145,7 @@ interface PublicSpaceIdentityResponse {
type SpaceMessageKind = MessageResponse["kind"];
export interface SpaceMessageQuote {
+ durationMs?: number;
imageUrl?: string;
isUnavailable?: boolean;
hasLoadError?: boolean;
@@ -405,8 +417,17 @@ const cacheAccountPostAssetURL = async (photo: PostPhoto, blob: Blob) => {
await rememberCachedSpaceMediaBlobURL(key, blob);
};
+const postVideoFrom = (photo: PostPhoto): SpacePostVideo | undefined =>
+ photo.video
+ ? {
+ asset: { ...photo.video.asset, mediaType: "video/mp4" },
+ durationMs: photo.video.durationMs,
+ }
+ : undefined;
+
const postPhotosFromResponse = (post: PostResponse): SpacePostPhoto[] =>
post.photos.map((photo) => ({
+ video: postVideoFrom(photo),
height: photo.height,
imageAsset: postAssetFrom(photo),
thumbHash: photo.thumbHash,
@@ -460,6 +481,7 @@ const postFromAccountPost = async (
return {
...base,
avatarUrl: author.avatarUrl,
+ video: postVideoFrom(photo),
height: photo.height,
imageAsset: postAssetFrom(photo),
imageUrl,
@@ -479,6 +501,7 @@ const profilePostFromPost = (post: PostResponse): SpaceProfilePost => {
return {
...base,
avatarUrl: null,
+ video: postVideoFrom(photo),
height: photo.height,
imageAsset: postAssetFrom(photo),
photos: postPhotosFromResponse(post),
@@ -597,6 +620,7 @@ const messageQuoteFromPostResponse = async (
);
const photo = post.photos[photoIndex];
const quote: SpaceMessageQuote = {
+ durationMs: photo?.video?.durationMs,
objectKey,
photoCount: post.photos.length,
postId: post.postId,
@@ -1108,38 +1132,97 @@ export const loadCurrentFriendAvatarURL = async (
}
};
-export const createCurrentPhotoPost = async ({
- caption,
+export interface PreparedSpacePostMedia {
+ file: File;
+ height: number;
+ width: number;
+ thumbHash: string;
+ video?: { file: File; durationMs: number };
+}
+
+export interface SpacePostUploadSession {
+ requestId?: string;
+ key?: Uint8Array;
+ postId?: number;
+ uploads: {
+ preview?: UploadedPostAsset;
+ video?: UploadedPostAsset;
+ expiresAt: number;
+ }[];
+}
+
+export const createCurrentMediaPost = async ({
images,
+ caption,
spaceId,
+ session,
+ signal,
}: {
- caption?: string;
- images: { file: File; height: number; width: number; thumbHash: string }[];
+ images: PreparedSpacePostMedia[];
+ caption: string;
spaceId: string;
+ session: SpacePostUploadSession;
+ signal?: AbortSignal;
}) => {
const ctx = await ensureCurrentSpaceContext();
try {
- const photos = await Promise.all(
- images.map(async (image) => ({
- bytes: new Uint8Array(await image.file.arrayBuffer()),
- options: {
- width: normalizedImageDimension(image.width),
- height: normalizedImageDimension(image.height),
- mediaType: image.file.type || undefined,
- thumbHash: image.thumbHash || undefined,
- },
- })),
- );
- const created = await ctx.createPhotoPost(
+ if (!session.postId) {
+ session.key ??= ctx.generatePostKey();
+ session.requestId ??= crypto.randomUUID();
+ for (const [index, image] of images.entries()) {
+ signal?.throwIfAborted();
+ let upload = session.uploads[index];
+ if (!upload || upload.expiresAt < Date.now()) {
+ upload = { expiresAt: Date.now() + 25 * 60 * 1000 };
+ session.uploads[index] = upload;
+ }
+ upload.preview ??= await ctx.uploadPostPhotoAsset(
+ spaceId,
+ session.key,
+ new Uint8Array(await image.file.arrayBuffer()),
+ {
+ width: image.width,
+ height: image.height,
+ mediaType: image.file.type,
+ thumbHash: image.thumbHash,
+ },
+ signal,
+ );
+ signal?.throwIfAborted();
+ if (image.video)
+ upload.video ??= await ctx.uploadPostVideoAsset(
+ spaceId,
+ session.key,
+ new Uint8Array(await image.video.file.arrayBuffer()),
+ {
+ width: image.width,
+ height: image.height,
+ durationMs: image.video.durationMs,
+ },
+ signal,
+ );
+ }
+ signal?.throwIfAborted();
+ session.postId = Number(
+ await ctx.createMediaPost(
+ spaceId,
+ session.key,
+ session.uploads.map((upload) => ({
+ preview: upload.preview!,
+ video: upload.video,
+ })),
+ session.requestId,
+ caption.trim() || undefined,
+ ),
+ );
+ }
+ const created = await ctx.getPost(
+ spaceId,
+ BigInt(session.postId),
spaceId,
- photos,
- caption?.trim() || null,
- );
- await Promise.all(
- created.photos.map((photo, index) =>
- cacheAccountPostAssetURL(photo, images[index]!.file),
- ),
);
+ for (const [index, photo] of created.photos.entries())
+ await cacheAccountPostAssetURL(photo, images[index]!.file);
const post = await postFromAccountPost(ctx, created, true, spaceId);
await prependCachedSpaceFeedPost(spaceId, post);
return post;
@@ -1148,11 +1231,6 @@ export const createCurrentPhotoPost = async ({
}
};
-const normalizedImageDimension = (dimension: number | undefined) =>
- typeof dimension == "number" && Number.isFinite(dimension) && dimension > 0
- ? Math.round(dimension)
- : undefined;
-
export const setCurrentPostLiked = async (
spaceId: string,
postId: number,
diff --git a/web/apps/space/src/state/AppStateProvider.tsx b/web/apps/space/src/state/AppStateProvider.tsx
index 88515eda45..0776dd5cdb 100644
--- a/web/apps/space/src/state/AppStateProvider.tsx
+++ b/web/apps/space/src/state/AppStateProvider.tsx
@@ -23,9 +23,11 @@ import {
import {
clearSpaceFriendsCache,
clearSpaceMediaURLCache,
- createCurrentPhotoPost,
+ createCurrentMediaPost,
+ type PreparedSpacePostMedia,
} from "services/space";
import {
+ initialFriends,
type LocalSpaceFeedPost,
type OnboardingEntrySource,
type PendingCreateProfile,
@@ -34,7 +36,6 @@ import {
SpaceAppStateContext,
type SpacePostPublication,
type SpaceProfileLoadStatus,
- initialFriends,
} from "state/app-state";
import {
confirmLocalFeedPost,
@@ -42,6 +43,7 @@ import {
failLocalFeedPost,
} from "utils/local-feed-post";
import { prepareSpacePostImageFromEdit } from "utils/post-image";
+import { prepareSpaceVideo } from "utils/post-video";
const postStatusDurationMs = 2000;
@@ -166,20 +168,23 @@ export const SpaceAppStateProvider: React.FC<React.PropsWithChildren> = ({
};
setPostPublication(publication);
try {
- const preparedImages = [];
+ const prepared: PreparedSpacePostMedia[] = [];
for (const image of images) {
- preparedImages.push(
- await prepareSpacePostImageFromEdit(
- image.file,
- image.cropArea,
- image.rotationDegrees,
- ),
+ prepared.push(
+ image.video
+ ? await prepareSpaceVideo(image.file, image.video)
+ : await prepareSpacePostImageFromEdit(
+ image.file,
+ image.cropArea,
+ image.rotationDegrees,
+ ),
);
}
- const post = await createCurrentPhotoPost({
+ const post = await createCurrentMediaPost({
caption,
- images: preparedImages,
+ images: prepared,
spaceId,
+ session: { uploads: [] },
});
confirmLocalFeedPost(setLocalFeedPosts, localPostId, post);
if (postPublishGenerationRef.current == generation) {
diff --git a/web/apps/space/src/styles/globals.css b/web/apps/space/src/styles/globals.css
index 0d85ee71cc..8a250af373 100644
--- a/web/apps/space/src/styles/globals.css
+++ b/web/apps/space/src/styles/globals.css
@@ -95,3 +95,75 @@ html[data-space-route-motion="back"]::view-transition-new(root) {
--pswp-bg: #000000;
height: 100svh;
}
+
+.space-video-control {
+ opacity: 0;
+ visibility: hidden;
+ pointer-events: none;
+ transition:
+ opacity 160ms ease,
+ visibility 160ms;
+}
+
+.space-photo-placeholder[aria-busy="true"] {
+ animation: space-preview-shimmer 1.4s ease-in-out infinite;
+ background-color: #171717;
+ background-image: linear-gradient(
+ 90deg,
+ #171717 0%,
+ #242424 42%,
+ #2d2d2d 50%,
+ #242424 58%,
+ #171717 100%
+ );
+ background-size: 200% 100%;
+ box-shadow: 0 16px 42px rgba(0, 0, 0, 0.34);
+ overflow: hidden;
+}
+
+@keyframes space-preview-shimmer {
+ from {
+ background-position: 100% 0;
+ }
+
+ to {
+ background-position: -100% 0;
+ }
+}
+
+.space-video-control[aria-hidden="false"] {
+ opacity: 1;
+ visibility: visible;
+ pointer-events: auto;
+}
+
+.space-video-spinner {
+ display: block;
+ box-sizing: border-box;
+ width: 24px;
+ height: 24px;
+ border: 2px solid rgba(255, 255, 255, 0.25);
+ border-top-color: #fff;
+ border-radius: 50%;
+ animation: space-video-spin 800ms linear infinite;
+}
+
+@keyframes space-video-spin {
+ to {
+ transform: rotate(360deg);
+ }
+}
+
+@media (prefers-reduced-motion: reduce) {
+ .space-photo-placeholder[aria-busy="true"] {
+ animation: none;
+ }
+
+ .space-video-control {
+ transition: none;
+ }
+
+ .space-video-spinner {
+ animation: none;
+ }
+}
diff --git a/web/apps/space/src/utils/post-image.ts b/web/apps/space/src/utils/post-image.ts
index b062d7e9e6..75a666eab5 100644
--- a/web/apps/space/src/utils/post-image.ts
+++ b/web/apps/space/src/utils/post-image.ts
@@ -1,6 +1,7 @@
import { lowercaseExtension, nameAndExtension } from "ente-base/file-name";
import { isHEICExtension } from "ente-media/formats";
import { heicToJPEG } from "ente-media/heic-convert";
+import type { SpacePostVideoEdit } from "utils/post-video";
import { thumbHashBase64FromCanvas } from "utils/thumbhash";
interface PreparedSpaceImage {
@@ -39,6 +40,7 @@ export interface SpacePostPhotoEdit {
}
export interface SpaceDraftPostImage {
+ video?: SpacePostVideoEdit;
cropArea?: SpaceImageCropArea;
file: File;
height?: number;
diff --git a/web/apps/space/src/utils/post-video.ts b/web/apps/space/src/utils/post-video.ts
new file mode 100644
index 0000000000..d7c437f992
--- /dev/null
+++ b/web/apps/space/src/utils/post-video.ts
@@ -0,0 +1,271 @@
+import type { FFmpegCommand } from "ente-base/types/ipc";
+import {
+ inputPathPlaceholder,
+ outputPathPlaceholder,
+} from "ente-media/ffmpeg/constants";
+import type { PreparedSpacePostImage } from "utils/post-image";
+import { thumbHashBase64FromCanvas } from "utils/thumbhash";
+
+export interface SpacePostVideoEdit {
+ start: number;
+ end: number;
+ coverTime: number;
+ muted: boolean;
+}
+
+export interface SpaceVideoInfo {
+ duration: number;
+ width: number;
+ height: number;
+}
+
+export const maxSpaceVideoDuration = 10;
+
+export const isSpaceVideoFile = (file: File) =>
+ file.type.startsWith("video/") || /\.(mp4|mov|m4v|webm)$/i.test(file.name);
+
+export const initialSpaceVideoEdit = (
+ duration: number,
+): SpacePostVideoEdit => ({
+ start: 0,
+ end: Math.min(duration, maxSpaceVideoDuration),
+ coverTime: 0,
+ muted: false,
+});
+
+export const clampVideoCover = (edit: SpacePostVideoEdit) => ({
+ ...edit,
+ coverTime: Math.max(edit.start, Math.min(edit.coverTime, edit.end - 0.001)),
+});
+
+const waitForVideo = (
+ video: HTMLVideoElement,
+ event: "loadeddata" | "seeked",
+ signal?: AbortSignal,
+) =>
+ new Promise<void>((resolve, reject) => {
+ const finish = (error?: Error) => {
+ window.clearTimeout(timer);
+ video.removeEventListener(event, ready);
+ video.removeEventListener("error", failed);
+ signal?.removeEventListener("abort", abort);
+ if (error) reject(error);
+ else resolve();
+ };
+ const ready = () => finish();
+ const failed = () =>
+ finish(new Error("This browser can't preview this video."));
+ const abort = () => finish(new DOMException("Canceled", "AbortError"));
+ const timer = window.setTimeout(
+ () => finish(new Error("The video took too long to load.")),
+ 30_000,
+ );
+ video.addEventListener(event, ready, { once: true });
+ video.addEventListener("error", failed, { once: true });
+ signal?.addEventListener("abort", abort, { once: true });
+ if (signal?.aborted) abort();
+ });
+
+export const openSpaceVideo = async (file: Blob, signal?: AbortSignal) => {
+ signal?.throwIfAborted();
+ const video = document.createElement("video");
+ const url = URL.createObjectURL(file);
+ video.muted = true;
+ video.playsInline = true;
+ video.preload = "auto";
+ const dispose = () => {
+ video.pause();
+ video.removeAttribute("src");
+ video.load();
+ URL.revokeObjectURL(url);
+ };
+ try {
+ const ready = waitForVideo(video, "loadeddata", signal);
+ video.src = url;
+ video.load();
+ await ready;
+ return { video, dispose };
+ } catch (error) {
+ dispose();
+ throw error;
+ }
+};
+
+export const seekSpaceVideo = async (
+ video: HTMLVideoElement,
+ time: number,
+ signal?: AbortSignal,
+) => {
+ signal?.throwIfAborted();
+ const target = Math.max(0, Math.min(time, video.duration - 0.001));
+ if (video.currentTime == target) return;
+ const ready = waitForVideo(video, "seeked", signal);
+ video.currentTime = target;
+ await ready;
+};
+
+export const spaceVideoInfo = async (file: File): Promise<SpaceVideoInfo> => {
+ const { video, dispose } = await openSpaceVideo(file);
+ try {
+ if (!Number.isFinite(video.duration) || video.duration <= 0)
+ throw new Error("This video has no readable duration.");
+ return {
+ duration: video.duration,
+ width: video.videoWidth,
+ height: video.videoHeight,
+ };
+ } finally {
+ dispose();
+ }
+};
+
+export const spaceVideoCover = async (
+ file: Blob,
+ time: number,
+ signal?: AbortSignal,
+): Promise<PreparedSpacePostImage> => {
+ const { video, dispose } = await openSpaceVideo(file, signal);
+ try {
+ await seekSpaceVideo(video, time, signal);
+ const scale = Math.min(
+ 1,
+ 1280 / Math.max(video.videoWidth, video.videoHeight),
+ );
+ const canvas = document.createElement("canvas");
+ canvas.width = Math.round(video.videoWidth * scale);
+ canvas.height = Math.round(video.videoHeight * scale);
+ const context = canvas.getContext("2d");
+ if (!context) throw new Error("Couldn't prepare the video cover.");
+ context.drawImage(video, 0, 0, canvas.width, canvas.height);
+ const thumbHash = thumbHashBase64FromCanvas(canvas);
+ const blob = await new Promise<Blob>((resolve, reject) =>
+ canvas.toBlob(
+ (blob) =>
+ blob
+ ? resolve(blob)
+ : reject(
+ new Error("Couldn't prepare the video cover."),
+ ),
+ "image/webp",
+ 0.85,
+ ),
+ );
+ return {
+ file: new File([blob], "cover.webp", { type: blob.type }),
+ width: canvas.width,
+ height: canvas.height,
+ thumbHash,
+ };
+ } finally {
+ dispose();
+ }
+};
+
+const videoExportCommand = (
+ edit: SpacePostVideoEdit,
+ hdr: boolean,
+): string[] => [
+ "-ss",
+ String(edit.start),
+ "-i",
+ inputPathPlaceholder,
+ "-t",
+ String(Math.min(maxSpaceVideoDuration, edit.end - edit.start)),
+ "-map",
+ "0:v:0",
+ ...(edit.muted
+ ? ["-an"]
+ : ["-map", "0:a:0?", "-c:a", "aac", "-b:a", "192k"]),
+ "-map_metadata",
+ "-1",
+ "-map_metadata:s",
+ "-1",
+ "-map_chapters",
+ "-1",
+ "-vf",
+ [
+ "fps=30:start_time=0",
+ "scale=w='trunc(iw*min(1,min(1920/max(iw,ih),1080/min(iw,ih)))/2)*2':h='trunc(ih*min(1,min(1920/max(iw,ih),1080/min(iw,ih)))/2)*2'",
+ ...(hdr
+ ? [
+ "zscale=transfer=linear",
+ "tonemap=tonemap=hable:desat=0",
+ "zscale=primaries=709:transfer=709:matrix=709",
+ ]
+ : []),
+ "format=yuv420p",
+ "setsar=1",
+ ].join(","),
+ "-c:v",
+ "libx264",
+ "-preset",
+ "veryfast",
+ "-crf",
+ "20",
+ "-maxrate",
+ "8M",
+ "-bufsize",
+ "16M",
+ "-movflags",
+ "+faststart",
+ outputPathPlaceholder,
+];
+
+const exports = new WeakMap<
+ File,
+ { key: string; file: File; durationMs: number }
+>();
+
+export const prepareSpaceVideo = async (
+ file: File,
+ edit: SpacePostVideoEdit,
+ signal?: AbortSignal,
+) => {
+ signal?.throwIfAborted();
+ const duration = edit.end - edit.start;
+ if (
+ ![edit.start, edit.end, edit.coverTime].every(Number.isFinite) ||
+ edit.start < 0 ||
+ duration <= 0 ||
+ duration > maxSpaceVideoDuration + 0.000001
+ )
+ throw new Error("Choose a video segment of up to 10 seconds.");
+ const key = JSON.stringify([edit.start, edit.end, edit.muted]);
+ let exported = exports.get(file);
+ if (exported?.key != key) {
+ const { transcodeVideoWeb, determineVideoDurationWeb } =
+ await import("ente-media/ffmpeg/web");
+ const command: FFmpegCommand = {
+ default: videoExportCommand(edit, false),
+ hdr: videoExportCommand(edit, true),
+ };
+ const bytes = await transcodeVideoWeb(file, edit, command, signal);
+ signal?.throwIfAborted();
+ const output = new File([bytes], "video.mp4", { type: "video/mp4" });
+ if (output.size > 15 * 1024 * 1024 - 42)
+ throw new Error("This video is too large. Try a shorter trim.");
+ const outputDuration = await determineVideoDurationWeb(output, signal);
+ if (
+ !Number.isFinite(outputDuration) ||
+ outputDuration <= 0 ||
+ outputDuration > 10.000001
+ )
+ throw new Error("Couldn't export a video within 10 seconds.");
+ exported = {
+ key,
+ file: output,
+ durationMs: Math.min(10_000, Math.ceil(outputDuration * 1000)),
+ };
+ exports.set(file, exported);
+ }
+ signal?.throwIfAborted();
+ const cover = await spaceVideoCover(
+ exported.file,
+ Math.max(0, edit.coverTime - edit.start),
+ signal,
+ );
+ return {
+ ...cover,
+ video: { file: exported.file, durationMs: exported.durationMs },
+ };
+};
diff --git a/web/apps/space/tests/post-upload.test.ts b/web/apps/space/tests/post-upload.test.ts
index 54e311f68c..0b0bbc26da 100644
--- a/web/apps/space/tests/post-upload.test.ts
+++ b/web/apps/space/tests/post-upload.test.ts
@@ -18,10 +18,19 @@ interface UploadedPost {
encryptedPostKey: string;
captionCipher: string;
keyVersion: number;
- objects: { objectKey: string; position: number; metadataCipher: string }[];
+ clientRequestId?: string;
+ objects: {
+ objectKey: string;
+ position: number;
+ metadataCipher: string;
+ video?: { objectKey: string; metadataCipher: string };
+ }[];
}
-const uploadFixture = async (failSecondUpload = false) => {
+const uploadFixture = async (
+ failSecondUpload = false,
+ interceptRequest?: (request: Request) => Promise<Response> | undefined,
+) => {
const rootKey = btoa("r".repeat(32));
const spaceKey = btoa("s".repeat(32));
const { encryptedData, nonce } = await encryptBox(spaceKey, rootKey);
@@ -124,19 +133,45 @@ const uploadFixture = async (failSecondUpload = false) => {
throw new Error(`Unexpected request: ${request.method} ${path}`);
};
vi.stubGlobal("fetch", async (request: Request) => {
- const response = await respond(request);
+ const response = await (interceptRequest?.(request) ??
+ respond(request));
Object.defineProperty(response, "url", { value: request.url });
return response;
});
- return { ctx, calls, author, created: () => created };
+ const createPhotoPost = async (
+ spaceId: string,
+ photos: ReturnType<typeof photo>[],
+ caption: string,
+ ) => {
+ const key = ctx.generatePostKey();
+ const items = [];
+ for (const photo of photos)
+ items.push({
+ preview: await ctx.uploadPostPhotoAsset(
+ spaceId,
+ key,
+ photo.bytes,
+ photo.options,
+ ),
+ });
+ const postId = await ctx.createMediaPost(
+ spaceId,
+ key,
+ items,
+ crypto.randomUUID(),
+ caption,
+ );
+ return ctx.getPost(spaceId, postId);
+ };
+ return { ctx, calls, author, created: () => created, createPhotoPost };
};
test.each([1, 3, 10])(
"publishes %i encrypted photos in order with one caption",
async (count) => {
- const { ctx, calls, created } = await uploadFixture();
+ const { ctx, calls, created, createPhotoPost } = await uploadFixture();
try {
- const result = await ctx.createPhotoPost(
+ const result = await createPhotoPost(
"test-space",
Array.from({ length: count }, (_, index) => photo(index)),
"One shared caption",
@@ -173,7 +208,7 @@ test.each([1, 3, 10])(
);
test("copies typed-array photo bytes without JavaScript iteration", async () => {
- const { ctx, created } = await uploadFixture();
+ const { ctx, created, createPhotoPost } = await uploadFixture();
const input = photo(0);
const buffer = new Uint8Array(input.bytes.length + 16);
buffer.set(input.bytes, 8);
@@ -184,11 +219,7 @@ test("copies typed-array photo bytes without JavaScript iteration", async () =>
},
});
try {
- const post = await ctx.createPhotoPost(
- "test-space",
- [input],
- "Caption",
- );
+ const post = await createPhotoPost("test-space", [input], "Caption");
expect(created()?.objects).toHaveLength(1);
const cached = CachedSpacePost.shape.imageAsset
.unwrap()
@@ -201,10 +232,10 @@ test("copies typed-array photo bytes without JavaScript iteration", async () =>
});
test("a failed photo upload does not publish a partial post", async () => {
- const { ctx, calls } = await uploadFixture(true);
+ const { ctx, calls, createPhotoPost } = await uploadFixture(true);
try {
await expect(
- ctx.createPhotoPost(
+ createPhotoPost(
"test-space",
[photo(0), photo(1), photo(2)],
"Caption",
@@ -216,16 +247,24 @@ test("a failed photo upload does not publish a partial post", async () => {
}
});
-test.each([0, 11])("rejects %i photos before uploading", async (count) => {
+test.each([0, 11])("rejects %i items before publishing", async (count) => {
const { ctx, calls } = await uploadFixture();
try {
await expect(
- ctx.createPhotoPost(
+ ctx.createMediaPost(
"test-space",
- Array.from({ length: count }, (_, i) => photo(i)),
+ ctx.generatePostKey(),
+ Array.from({ length: count }, (_, i) => ({
+ preview: {
+ objectKey: `photo-${i}`,
+ size: undefined,
+ metadataCipher: undefined,
+ },
+ })),
+ crypto.randomUUID(),
"Caption",
),
- ).rejects.toThrow("Choose between 1 and 10 photos");
+ ).rejects.toThrow("Choose between 1 and 10 items");
expect(calls).toHaveLength(0);
} finally {
ctx.free();
@@ -233,13 +272,9 @@ test.each([0, 11])("rejects %i photos before uploading", async (count) => {
});
test("pages expose typed profiles and mark corrupt posts unavailable", async () => {
- const { ctx, created, author } = await uploadFixture();
+ const { ctx, created, author, createPhotoPost } = await uploadFixture();
try {
- const post = await ctx.createPhotoPost(
- "test-space",
- [photo(0)],
- "Caption",
- );
+ const post = await createPhotoPost("test-space", [photo(0)], "Caption");
expect(post.author.profile).toEqual({ fullName: "Test User" });
expect(post.isUnavailable).toBe(false);
const metadataCipher = created()!.objects[0]!.metadataCipher;
@@ -264,3 +299,237 @@ test("pages expose typed profiles and mark corrupt posts unavailable", async ()
ctx.free();
}
});
+
+const videoBytes = new Uint8Array([
+ 0, 0, 0, 24, 102, 116, 121, 112, 109, 112, 52, 50,
+]);
+
+test.each([
+ ["photo", "POST"],
+ ["photo", "PUT"],
+ ["video", "POST"],
+ ["video", "PUT"],
+])(
+ "cancels a stalled %s %s request and allows another upload",
+ async (kind, method) => {
+ const started = Promise.withResolvers<Request>();
+ let stalled = true;
+ const { ctx, calls } = await uploadFixture(false, (request) => {
+ if (!stalled || request.method != method) return;
+ started.resolve(request);
+ return new Promise((_, reject) => {
+ request.signal.addEventListener(
+ "abort",
+ () => reject(request.signal.reason as Error),
+ { once: true },
+ );
+ });
+ });
+ const controller = new AbortController();
+ const key = ctx.generatePostKey();
+ const upload = (signal?: AbortSignal) =>
+ kind == "photo"
+ ? ctx.uploadPostPhotoAsset(
+ "test-space",
+ key,
+ photo(0).bytes,
+ photo(0).options,
+ signal,
+ )
+ : ctx.uploadPostVideoAsset(
+ "test-space",
+ key,
+ videoBytes,
+ { width: 1280, height: 720, durationMs: 10000 },
+ signal,
+ );
+ try {
+ const canceled = expect(
+ upload(controller.signal),
+ ).rejects.toMatchObject({ name: "AbortError" });
+ const request = await started.promise;
+ controller.abort();
+ await canceled;
+ expect(request.signal.aborted).toBe(true);
+ expect(calls).not.toContain("POST /spaces/test-space/posts");
+ stalled = false;
+ await expect(upload()).resolves.toHaveProperty("objectKey");
+ } finally {
+ ctx.free();
+ }
+ },
+);
+
+test.each(["photo", "video"])(
+ "does not start an already-canceled %s upload",
+ async (kind) => {
+ const { ctx, calls } = await uploadFixture();
+ const controller = new AbortController();
+ controller.abort();
+ const key = ctx.generatePostKey();
+ try {
+ const upload =
+ kind == "photo"
+ ? ctx.uploadPostPhotoAsset(
+ "test-space",
+ key,
+ photo(0).bytes,
+ photo(0).options,
+ controller.signal,
+ )
+ : ctx.uploadPostVideoAsset(
+ "test-space",
+ key,
+ videoBytes,
+ { width: 1280, height: 720, durationMs: 10000 },
+ controller.signal,
+ );
+ await expect(upload).rejects.toMatchObject({ name: "AbortError" });
+ expect(calls).toHaveLength(0);
+ } finally {
+ ctx.free();
+ }
+ },
+);
+
+test("uploads a video larger than the previous photo limit", async () => {
+ const { ctx, calls } = await uploadFixture();
+ const bytes = new Uint8Array(10 * 1024 * 1024);
+ bytes.set(videoBytes);
+ try {
+ await ctx.uploadPostVideoAsset(
+ "test-space",
+ ctx.generatePostKey(),
+ bytes,
+ { width: 1920, height: 1080, durationMs: 10000 },
+ );
+ expect(calls).toContain("PUT /upload/0");
+ } finally {
+ ctx.free();
+ }
+});
+
+test("rejects a video exceeding the encrypted upload limit", async () => {
+ const { ctx, calls } = await uploadFixture();
+ const bytes = new Uint8Array(15 * 1024 * 1024);
+ bytes.set(videoBytes);
+ try {
+ await expect(
+ ctx.uploadPostVideoAsset(
+ "test-space",
+ ctx.generatePostKey(),
+ bytes,
+ { width: 1920, height: 1080, durationMs: 10000 },
+ ),
+ ).rejects.toThrow();
+ expect(calls).toHaveLength(0);
+ } finally {
+ ctx.free();
+ }
+});
+
+test.each([1, 5, 10])(
+ "publishes ten encrypted items with %i videos and downloads their covers and clips",
+ async (videoCount) => {
+ const { ctx, created, calls } = await uploadFixture();
+ try {
+ const key = ctx.generatePostKey();
+ const items = [];
+ for (let i = 0; i < 10; i++) {
+ const image = photo(i);
+ const preview = await ctx.uploadPostPhotoAsset(
+ "test-space",
+ key,
+ image.bytes,
+ image.options,
+ );
+ const video =
+ i < videoCount
+ ? await ctx.uploadPostVideoAsset(
+ "test-space",
+ key,
+ videoBytes,
+ { width: 1280, height: 720, durationMs: 10000 },
+ )
+ : undefined;
+ items.push({ preview, video });
+ }
+ const id = await ctx.createMediaPost(
+ "test-space",
+ key,
+ items,
+ "retry-id",
+ "A mixed post",
+ );
+ const post = await ctx.getPost("test-space", id);
+ expect(created()?.clientRequestId).toBe("retry-id");
+ expect(post.photos).toHaveLength(10);
+ expect(post.photos.filter((photo) => photo.video)).toHaveLength(
+ videoCount,
+ );
+ expect(created()?.objects.map((object) => object.position)).toEqual(
+ Array.from({ length: 10 }, (_, i) => i),
+ );
+ for (const [index, item] of post.photos.entries()) {
+ expect(
+ await ctx.downloadPostAsset(item.asset, "test-space"),
+ ).toEqual(photo(index).bytes);
+ if (item.video) {
+ expect(item.video.durationMs).toBe(10000);
+ expect(
+ await ctx.downloadPostAsset(
+ item.video.asset,
+ "test-space",
+ ),
+ ).toEqual(videoBytes);
+ expect(
+ CachedSpacePost.parse(
+ JSON.parse(
+ JSON.stringify({
+ postId: 501,
+ spaceId: "test-space",
+ friendID: "test-space",
+ viewerLiked: false,
+ timestampMs: 0,
+ name: "Test",
+ imageUrl: "",
+ video: item.video,
+ }),
+ ),
+ ).video,
+ ).toBeDefined();
+ }
+ }
+ expect(
+ calls.filter((call) => call.startsWith("PUT /upload/")),
+ ).toHaveLength(10 + videoCount);
+ expect(
+ calls.indexOf("POST /spaces/test-space/posts"),
+ ).toBeGreaterThan(
+ calls.lastIndexOf(`PUT /upload/${9 + videoCount}`),
+ );
+ } finally {
+ ctx.free();
+ }
+ },
+);
+
+test.each([0, 10001])(
+ "rejects an invalid video duration of %i ms before uploading",
+ async (duration) => {
+ const { ctx, calls } = await uploadFixture();
+ try {
+ await expect(
+ ctx.uploadPostVideoAsset(
+ "test-space",
+ ctx.generatePostKey(),
+ videoBytes,
+ { width: 1280, height: 720, durationMs: duration },
+ ),
+ ).rejects.toThrow();
+ expect(calls).toHaveLength(0);
+ } finally {
+ ctx.free();
+ }
+ },
+);
diff --git a/web/apps/space/tests/video-export.test.ts b/web/apps/space/tests/video-export.test.ts
new file mode 100644
index 0000000000..3c056bf945
--- /dev/null
+++ b/web/apps/space/tests/video-export.test.ts
@@ -0,0 +1,155 @@
+import { afterEach, beforeEach, expect, test, vi } from "vitest";
+
+const mocks = vi.hoisted(() => ({
+ encode: vi.fn(),
+ load: vi.fn(),
+ workers: [] as {
+ loaded: boolean;
+ terminate: ReturnType<typeof vi.fn>;
+ exec: ReturnType<typeof vi.fn>;
+ unmount: ReturnType<typeof vi.fn>;
+ }[],
+}));
+vi.mock("ente-base/log", () => ({
+ default: { debug: vi.fn(), info: vi.fn(), warn: vi.fn(), error: vi.fn() },
+}));
+vi.mock("ente-media/ffmpeg/web-codecs", () => ({
+ encodeVideoWithWebCodecs: mocks.encode,
+}));
+vi.mock("@ffmpeg/ffmpeg", () => ({
+ FFFSType: { WORKERFS: "WORKERFS" },
+ FFmpeg: class {
+ loaded = true;
+ constructor() {
+ mocks.workers.push(this);
+ }
+ load = mocks.load;
+ terminate = vi.fn(() => {
+ this.loaded = false;
+ });
+ on = vi.fn();
+ off = vi.fn();
+ createDir = vi.fn();
+ deleteDir = vi.fn();
+ mount = vi.fn();
+ unmount = vi.fn();
+ deleteFile = vi.fn();
+ exec = vi.fn().mockResolvedValue(0);
+ readFile = vi.fn().mockResolvedValue(new Uint8Array([7]));
+ },
+}));
+
+beforeEach(() => {
+ vi.resetModules();
+ mocks.encode.mockReset();
+ mocks.load.mockReset().mockResolvedValue(true);
+ mocks.workers.length = 0;
+ vi.stubGlobal("VideoEncoder", {});
+ vi.stubGlobal("VideoDecoder", {});
+});
+afterEach(() => vi.unstubAllGlobals());
+
+const exporter = async () => {
+ const { transcodeVideoWeb } = await import("ente-media/ffmpeg/web");
+ return (signal?: AbortSignal) =>
+ transcodeVideoWeb(
+ new Blob(["video"]),
+ { start: 0, end: 10, muted: false },
+ ["fallback"],
+ signal,
+ );
+};
+
+test("uses WebCodecs output without running the software encoder", async () => {
+ const bytes = new Uint8Array([1, 2]);
+ mocks.encode.mockResolvedValue(bytes);
+ expect(await (await exporter())()).toBe(bytes);
+ expect(mocks.workers[0]!.exec).not.toHaveBeenCalled();
+ expect(mocks.workers[0]!.unmount).toHaveBeenCalled();
+});
+
+test.each(["unsupported", "failed", "missing API"])(
+ "keeps the software exporter available when WebCodecs is %s",
+ async (mode) => {
+ if (mode === "failed")
+ mocks.encode.mockRejectedValue(new Error("Codec unavailable"));
+ else mocks.encode.mockResolvedValue(undefined);
+ if (mode === "missing API") vi.stubGlobal("VideoEncoder", undefined);
+ expect(await (await exporter())()).toEqual(new Uint8Array([7]));
+ expect(mocks.workers[0]!.exec).toHaveBeenCalledWith(["fallback"]);
+ if (mode === "missing API") expect(mocks.encode).not.toHaveBeenCalled();
+ },
+);
+
+test("cancellation stops the active export without starting the software fallback", async () => {
+ mocks.encode.mockImplementation(
+ (_ffmpeg, _path, _blob, _edit, signal: AbortSignal) =>
+ new Promise((_, reject) =>
+ signal.addEventListener(
+ "abort",
+ () => reject(new DOMException("Canceled", "AbortError")),
+ { once: true },
+ ),
+ ),
+ );
+ const controller = new AbortController();
+ const pending = (await exporter())(controller.signal);
+ const rejected = expect(pending).rejects.toMatchObject({
+ name: "AbortError",
+ });
+ await vi.waitFor(() => expect(mocks.encode).toHaveBeenCalled());
+ controller.abort();
+ await rejected;
+ expect(mocks.workers[0]!.terminate).toHaveBeenCalled();
+ expect(mocks.workers[0]!.exec).not.toHaveBeenCalled();
+});
+
+test("a queued export can be cancelled without waiting for or stopping another export", async () => {
+ let finish!: (bytes: Uint8Array) => void;
+ mocks.encode.mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ finish = resolve;
+ }),
+ );
+ const run = await exporter();
+ const first = run();
+ await vi.waitFor(() => expect(mocks.encode).toHaveBeenCalledTimes(1));
+ const controller = new AbortController();
+ const second = run(controller.signal);
+ const rejected = expect(second).rejects.toMatchObject({
+ name: "AbortError",
+ });
+ controller.abort();
+ await rejected;
+ expect(mocks.workers[0]!.terminate).not.toHaveBeenCalled();
+ finish(new Uint8Array([1]));
+ await first;
+ expect(mocks.encode).toHaveBeenCalledTimes(1);
+});
+
+test("cancelling engine loading terminates it and allows the next export to load again", async () => {
+ mocks.load.mockImplementationOnce(
+ (_config, { signal }: { signal: AbortSignal }) =>
+ new Promise((_, reject) =>
+ signal.addEventListener(
+ "abort",
+ () => reject(new DOMException("Canceled", "AbortError")),
+ { once: true },
+ ),
+ ),
+ );
+ const run = await exporter();
+ const controller = new AbortController();
+ const pending = run(controller.signal);
+ const rejected = expect(pending).rejects.toMatchObject({
+ name: "AbortError",
+ });
+ await vi.waitFor(() => expect(mocks.load).toHaveBeenCalled());
+ controller.abort();
+ await rejected;
+ mocks.encode.mockResolvedValue(new Uint8Array([2]));
+ expect(await run()).toEqual(new Uint8Array([2]));
+ expect(mocks.workers[0]!.terminate).toHaveBeenCalled();
+ expect(mocks.workers).toHaveLength(2);
+});
diff --git a/web/apps/space/tests/video-trim-playback.test.ts b/web/apps/space/tests/video-trim-playback.test.ts
new file mode 100644
index 0000000000..5a61e73304
--- /dev/null
+++ b/web/apps/space/tests/video-trim-playback.test.ts
@@ -0,0 +1,189 @@
+import { afterEach, beforeEach, expect, test, vi } from "vitest";
+import { createVideoTrimPlayback } from "../src/components/video-trim/playback";
+
+class Video extends EventTarget {
+ currentTime = 0;
+ paused = true;
+ seeking = false;
+ readyState = 4;
+
+ play() {
+ this.paused = false;
+ this.dispatchEvent(new Event("play"));
+ return Promise.resolve();
+ }
+
+ pause() {
+ if (this.paused) return;
+ this.paused = true;
+ this.dispatchEvent(new Event("pause"));
+ }
+}
+
+let video: Video;
+let range: { start: number; end: number };
+let onError = vi.fn<() => void>();
+let playback: ReturnType<typeof createVideoTrimPlayback>;
+let frames: Map<number, FrameRequestCallback>;
+
+beforeEach(() => {
+ video = new Video();
+ range = { start: 5, end: 8 };
+ onError = vi.fn<() => void>();
+ frames = new Map();
+ let nextFrame = 0;
+ vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
+ frames.set(++nextFrame, callback);
+ return nextFrame;
+ });
+ vi.stubGlobal("cancelAnimationFrame", (id: number) => frames.delete(id));
+ playback = createVideoTrimPlayback(
+ video as unknown as HTMLVideoElement,
+ () => range,
+ onError,
+ );
+});
+
+afterEach(() => {
+ playback.dispose();
+ vi.unstubAllGlobals();
+});
+
+const renderFrame = () => {
+ const callbacks = [...frames.values()];
+ frames.clear();
+ callbacks.forEach((callback) => callback(0));
+};
+
+test("Play previews the selected clip from its start after trimming either edge", () => {
+ playback.seek(7.95);
+ playback.toggle();
+ expect(video.currentTime).toBe(5);
+ expect(video.paused).toBe(false);
+});
+
+test("playback stops at the trim end without waiting for a timeupdate event", () => {
+ playback.toggle();
+ video.currentTime = 8.01;
+ renderFrame();
+ expect(video.paused).toBe(true);
+ expect(video.currentTime).toBe(5);
+ expect(frames.size).toBe(0);
+ playback.toggle();
+ expect(video.currentTime).toBe(5);
+ expect(video.paused).toBe(false);
+});
+
+test("pause and resume inside the clip retain the playback position", () => {
+ playback.toggle();
+ video.currentTime = 6;
+ playback.toggle();
+ expect(video.paused).toBe(true);
+ expect(frames.size).toBe(0);
+ playback.toggle();
+ expect(video.currentTime).toBe(6);
+ expect(video.paused).toBe(false);
+});
+
+test("Play supersedes a preview seek that is still in progress", () => {
+ video.currentTime = 6;
+ video.seeking = true;
+ playback.seek(7.95);
+ playback.toggle();
+ video.seeking = false;
+ video.dispatchEvent(new Event("seeked"));
+ expect(video.currentTime).toBe(5);
+ expect(video.paused).toBe(false);
+});
+
+test("returning to the current frame cancels an older queued preview seek", () => {
+ video.currentTime = 5;
+ video.seeking = true;
+ playback.seek(7);
+ playback.seek(5);
+ video.seeking = false;
+ video.dispatchEvent(new Event("seeked"));
+ expect(video.currentTime).toBe(5);
+ video.currentTime = 6;
+ video.dispatchEvent(new Event("seeked"));
+ expect(video.currentTime).toBe(6);
+});
+
+test("the playback guard uses the latest trim bounds", () => {
+ playback.toggle();
+ range = { start: 5, end: 6 };
+ video.currentTime = 6.01;
+ renderFrame();
+ expect(video.paused).toBe(true);
+ expect(video.currentTime).toBe(5);
+});
+
+test("an out-of-range playback position is corrected to the trim start", () => {
+ playback.toggle();
+ video.currentTime = 0;
+ renderFrame();
+ expect(video.currentTime).toBe(5);
+ expect(video.paused).toBe(false);
+});
+
+test("Play before metadata loads keeps the selected start and does not get paused", () => {
+ video.readyState = 0;
+ playback.seek(7);
+ playback.toggle();
+ video.readyState = 1;
+ video.dispatchEvent(new Event("loadedmetadata"));
+ expect(video.currentTime).toBe(5);
+ expect(video.paused).toBe(false);
+});
+
+test("a second tap cancels pending playback without reporting an error", async () => {
+ let rejectPlay: (error: Error) => void;
+ vi.spyOn(video, "play").mockImplementation(
+ () => new Promise((_, reject) => (rejectPlay = reject)),
+ );
+ playback.toggle();
+ playback.toggle();
+ rejectPlay!(new Error("Play interrupted"));
+ await Promise.resolve();
+ expect(video.paused).toBe(true);
+ expect(onError).not.toHaveBeenCalled();
+ expect(frames.size).toBe(0);
+});
+
+test("trimming cancels pending playback and the next Play uses the new range", async () => {
+ let rejectPlay: (error: Error) => void;
+ const play = vi
+ .spyOn(video, "play")
+ .mockImplementationOnce(
+ () => new Promise((_, reject) => (rejectPlay = reject)),
+ );
+ playback.toggle();
+ range = { start: 20, end: 22 };
+ playback.seek(21.95);
+ playback.toggle();
+ rejectPlay!(new Error("Play interrupted"));
+ await Promise.resolve();
+ expect(play).toHaveBeenCalledTimes(2);
+ expect(video.currentTime).toBe(20);
+ expect(video.paused).toBe(false);
+ expect(onError).not.toHaveBeenCalled();
+});
+
+test("reaching the source end resets the selected clip for replay", () => {
+ playback.toggle();
+ video.currentTime = 8;
+ video.paused = true;
+ video.dispatchEvent(new Event("ended"));
+ expect(video.currentTime).toBe(5);
+ expect(frames.size).toBe(0);
+});
+
+test("closing the editor pauses playback and removes the frame monitor", () => {
+ playback.toggle();
+ playback.dispose();
+ expect(video.paused).toBe(true);
+ expect(frames.size).toBe(0);
+ video.currentTime = 12;
+ video.dispatchEvent(new Event("timeupdate"));
+ expect(video.currentTime).toBe(12);
+});
diff --git a/web/apps/space/tests/video-trim.test.ts b/web/apps/space/tests/video-trim.test.ts
new file mode 100644
index 0000000000..59da20b32d
--- /dev/null
+++ b/web/apps/space/tests/video-trim.test.ts
@@ -0,0 +1,129 @@
+import { expect, test } from "vitest";
+import {
+ dragVideoTrim,
+ type VideoTrimHandle,
+} from "../src/components/video-trim/geometry";
+
+test("dragging either handle past ten seconds keeps the opposite edge fixed", () => {
+ const range = { start: 8, end: 18 };
+ expect(dragVideoTrim(range, "end", 20, 40, 10)).toEqual(range);
+ expect(dragVideoTrim(range, "start", -20, 40, 10)).toEqual(range);
+ expect(dragVideoTrim(range, "start", 11, 40, 10)).toEqual({
+ start: 11,
+ end: 18,
+ });
+ expect(dragVideoTrim(range, "end", 15, 40, 10)).toEqual({
+ start: 8,
+ end: 15,
+ });
+});
+
+test("moving the range past ten seconds preserves its length in either direction", () => {
+ const later = dragVideoTrim({ start: 0, end: 10 }, "move", 17, 40, 10);
+ expect(later).toEqual({ start: 17, end: 27 });
+ expect(dragVideoTrim(later, "move", 5, 40, 10)).toEqual({
+ start: 5,
+ end: 15,
+ });
+ expect(dragVideoTrim(later, "move", 100, 40, 10)).toEqual({
+ start: 30,
+ end: 40,
+ });
+ expect(dragVideoTrim(later, "move", -100, 40, 10)).toEqual({
+ start: 0,
+ end: 10,
+ });
+});
+
+test.each([
+ { handle: "end" as const, outside: [23, 21, 19, 18], inside: 17 },
+ { handle: "start" as const, outside: [2, 4, 6, 8], inside: 9 },
+])(
+ "$handle keeps its grab point after overshooting",
+ ({ handle, outside, inside }) => {
+ const original = { start: 8, end: 18 };
+ let range = original;
+ for (const position of outside) {
+ range = dragVideoTrim(range, handle, position, 40, 10);
+ expect(range).toEqual(original);
+ }
+ expect(dragVideoTrim(range, handle, inside, 40, 10)).toEqual({
+ ...original,
+ [handle]: inside,
+ });
+ },
+);
+
+test("moving the range keeps its grab point beyond either end of the video", () => {
+ let range = { start: 30, end: 40 };
+ for (const position of [38, 35, 32, 30]) {
+ range = dragVideoTrim(range, "move", position, 40, 10);
+ expect(range).toEqual({ start: 30, end: 40 });
+ }
+ expect(dragVideoTrim(range, "move", 29, 40, 10)).toEqual({
+ start: 29,
+ end: 39,
+ });
+ range = { start: 0, end: 10 };
+ for (const position of [-8, -5, -2, 0]) {
+ range = dragVideoTrim(range, "move", position, 40, 10);
+ expect(range).toEqual({ start: 0, end: 10 });
+ }
+ expect(dragVideoTrim(range, "move", 1, 40, 10)).toEqual({
+ start: 1,
+ end: 11,
+ });
+});
+
+test.each([0, 15, 30, 40])(
+ "holding a ten-second range at position %s does not advance it",
+ (position) => {
+ const original = { start: 0, end: 10 };
+ const expected = dragVideoTrim(original, "move", position, 40, 10);
+ let range = expected;
+ for (let frame = 0; frame < 300; frame++) {
+ range = dragVideoTrim(range, "move", position, 40, 10);
+ expect(range).toEqual(expected);
+ }
+ expect(range.end - range.start).toBe(10);
+ },
+);
+
+test("moving a shortened range across the full video preserves its duration", () => {
+ const original = { start: 4, end: 9 };
+ expect(dragVideoTrim(original, "move", 18, 40, 10)).toEqual({
+ start: 18,
+ end: 23,
+ });
+ expect(dragVideoTrim(original, "move", 40, 40, 10)).toEqual({
+ start: 35,
+ end: 40,
+ });
+ expect(dragVideoTrim(original, "move", -5, 40, 10)).toEqual({
+ start: 0,
+ end: 5,
+ });
+});
+
+test.each([0.02, 4, 10, 40, 180])(
+ "drags stay valid for a %s-second video",
+ (duration) => {
+ const range = { start: 0, end: Math.min(10, duration) };
+ for (const handle of ["start", "end", "move"] as VideoTrimHandle[])
+ for (const position of [-200, -1, 0, 1, 200]) {
+ const next = dragVideoTrim(
+ range,
+ handle,
+ position,
+ duration,
+ 10,
+ );
+ expect(next.start).toBeGreaterThanOrEqual(0);
+ expect(next.end).toBeLessThanOrEqual(duration);
+ expect(next.end - next.start).toBeGreaterThanOrEqual(
+ Math.min(1 / 30, duration) - 1e-10,
+ );
+ expect(next.end - next.start).toBeLessThanOrEqual(10 + 1e-10);
+ }
+ },
+);
diff --git a/web/apps/space/tests/video-webcodecs.test.ts b/web/apps/space/tests/video-webcodecs.test.ts
new file mode 100644
index 0000000000..d7c915ee64
--- /dev/null
+++ b/web/apps/space/tests/video-webcodecs.test.ts
@@ -0,0 +1,241 @@
+import type { FFmpeg } from "@ffmpeg/ffmpeg";
+import { encodeVideoWithWebCodecs } from "ente-media/ffmpeg/web-codecs";
+import { afterEach, beforeEach, expect, test, vi } from "vitest";
+
+let frames: Frame[];
+let encodedSources: number[];
+let encoderFailure: boolean;
+let stallEncoder: (() => void) | undefined;
+let supported: boolean;
+
+class Frame {
+ timestamp: number;
+ duration: number;
+ sourceTime: number;
+ closed = false;
+ constructor(
+ source: { timestamp: number; duration: number; sourceTime?: number },
+ init?: VideoFrameInit,
+ ) {
+ this.timestamp = init?.timestamp ?? source.timestamp;
+ this.duration = init?.duration ?? source.duration;
+ this.sourceTime = source.sourceTime ?? source.timestamp;
+ frames.push(this);
+ }
+ close() {
+ this.closed = true;
+ }
+}
+
+class Decoder extends EventTarget {
+ static isConfigSupported = () => Promise.resolve({ supported });
+ state = "unconfigured";
+ decodeQueueSize = 0;
+ constructor(private init: VideoDecoderInit) {
+ super();
+ }
+ configure() {
+ this.state = "configured";
+ }
+ decode(chunk: EncodedVideoChunk) {
+ this.init.output(
+ new Frame({
+ timestamp: chunk.timestamp,
+ duration: chunk.duration!,
+ }) as unknown as VideoFrame,
+ );
+ }
+ flush() {
+ return Promise.resolve();
+ }
+ close() {
+ this.state = "closed";
+ }
+}
+
+class Encoder extends EventTarget {
+ static isConfigSupported = () => Promise.resolve({ supported });
+ state = "unconfigured";
+ encodeQueueSize = 0;
+ constructor(private init: VideoEncoderInit) {
+ super();
+ }
+ configure() {
+ this.state = "configured";
+ }
+ encode(frame: Frame) {
+ if (encoderFailure) {
+ this.init.error(new DOMException("Encoder failed"));
+ return;
+ }
+ if (stallEncoder) {
+ this.encodeQueueSize++;
+ if (this.encodeQueueSize > 8) queueMicrotask(stallEncoder);
+ return;
+ }
+ encodedSources.push(frame.sourceTime);
+ this.init.output({
+ timestamp: frame.timestamp,
+ byteLength: 1,
+ copyTo: (bytes: Uint8Array) => bytes.fill(1),
+ } as unknown as EncodedVideoChunk);
+ }
+ flush() {
+ return Promise.resolve();
+ }
+ close() {
+ this.state = "closed";
+ }
+}
+
+beforeEach(() => {
+ frames = [];
+ encodedSources = [];
+ encoderFailure = false;
+ stallEncoder = undefined;
+ supported = true;
+ vi.stubGlobal("VideoFrame", Frame);
+ vi.stubGlobal("VideoEncoder", Encoder);
+ vi.stubGlobal("VideoDecoder", Decoder);
+ vi.stubGlobal(
+ "EncodedVideoChunk",
+ class {
+ timestamp: number;
+ duration?: number;
+ constructor(init: EncodedVideoChunkInit) {
+ this.timestamp = init.timestamp;
+ this.duration = init.duration;
+ }
+ },
+ );
+});
+afterEach(() => vi.unstubAllGlobals());
+
+const fixture = (fps = 30, streamChanges = {}) => {
+ const files = new Map<string, Uint8Array | string>();
+ const packets = Array.from({ length: Math.ceil(fps * 20) }, (_, index) => ({
+ pts_time: (index / fps).toFixed(6),
+ duration_time: (1 / fps).toFixed(6),
+ pos: index,
+ size: 1,
+ flags: index % 60 === 0 ? "K_" : "__",
+ }));
+ const ffmpeg = {
+ loaded: true,
+ createDir: vi.fn(),
+ deleteDir: vi.fn(),
+ deleteFile: vi.fn((path: string) => files.delete(path)),
+ listDir: (directory: string) =>
+ [...files.keys()]
+ .filter((path) => path.startsWith(directory + "/"))
+ .map((path) => ({
+ name: path.slice(directory.length + 1),
+ isDir: false,
+ })),
+ readFile: (path: string) => files.get(path),
+ writeFile: (path: string, bytes: Uint8Array) => files.set(path, bytes),
+ ffprobe: (args: string[]) => {
+ files.set(
+ args[args.indexOf("-o") + 1]!,
+ JSON.stringify({
+ format: { format_name: "mov,mp4,m4a,3gp,3g2,mj2" },
+ streams: [
+ {
+ codec_name: "h264",
+ codec_tag_string: "avc1",
+ width: 320,
+ height: 240,
+ pix_fmt: "yuv420p",
+ ...streamChanges,
+ },
+ ],
+ packets,
+ }),
+ );
+ return 0;
+ },
+ exec: vi.fn((args: string[]) => {
+ if (args[0] === "-dump_attachment:v:0")
+ files.set(
+ args[1]!,
+ new Uint8Array([1, 100, 0, 40, 255, 225, 0]),
+ );
+ else files.set(args.at(-1)!, new Uint8Array([1, 2, 3]));
+ return 0;
+ }),
+ };
+ const encode = (start: number, end: number, signal?: AbortSignal) =>
+ encodeVideoWithWebCodecs(
+ ffmpeg as unknown as FFmpeg,
+ "/input",
+ new Blob([new Uint8Array(packets.length)]),
+ { start, end, muted: false },
+ signal,
+ );
+ return { encode, files, ffmpeg };
+};
+
+test("keeps every 30 fps frame without rounding duplicates", async () => {
+ const { encode, files } = fixture();
+ await encode(0, 10);
+ expect(encodedSources).toEqual(
+ Array.from({ length: 300 }, (_, index) =>
+ Math.round((index * 1_000_000) / 30),
+ ),
+ );
+ expect(frames.every((frame) => frame.closed)).toBe(true);
+ expect(files.size).toBe(0);
+});
+
+test("a trim between keyframes begins at the frame visible at the selected start", async () => {
+ const { encode } = fixture();
+ await encode(7.125, 17.125);
+ expect(encodedSources).toHaveLength(300);
+ expect(encodedSources[0]).toBe(7_100_000);
+ expect(encodedSources.at(-1)).toBe(17_066_667);
+ expect(new Set(encodedSources).size).toBe(300);
+});
+
+test("29.97 fps and fractional selections never export more than the selected duration", async () => {
+ const { encode } = fixture(30000 / 1001);
+ await encode(1.125, 11.125);
+ expect(encodedSources).toHaveLength(300);
+ encodedSources.forEach((time, index) => {
+ const target = 1_125_000 + Math.round((index * 1_000_000) / 30);
+ expect(time).toBeLessThanOrEqual(target);
+ expect(target - time).toBeLessThanOrEqual(33_367);
+ });
+ encodedSources = [];
+ await encode(1.125, 11.115);
+ expect(encodedSources).toHaveLength(299);
+});
+
+test("HDR and unsupported hardware are left to the existing exporter", async () => {
+ const hdr = fixture(30, { color_transfer: "smpte2084" });
+ expect(await hdr.encode(0, 10)).toBeUndefined();
+ expect(hdr.ffmpeg.exec).not.toHaveBeenCalled();
+ expect(hdr.files.size).toBe(0);
+ supported = false;
+ expect(await fixture().encode(0, 10)).toBeUndefined();
+ expect(encodedSources).toHaveLength(0);
+});
+
+test("cancelling a stalled encoder releases frames and temporary files", async () => {
+ const { encode, files, ffmpeg } = fixture();
+ const controller = new AbortController();
+ stallEncoder = () => controller.abort();
+ await expect(encode(0, 10, controller.signal)).rejects.toMatchObject({
+ name: "AbortError",
+ });
+ expect(frames.every((frame) => frame.closed)).toBe(true);
+ expect(files.size).toBe(0);
+ expect(ffmpeg.exec).toHaveBeenCalledTimes(1);
+});
+
+test("codec failures release resources and reject instead of leaving the export pending", async () => {
+ const { encode, files } = fixture();
+ encoderFailure = true;
+ await expect(encode(0, 10)).rejects.toThrow("Encoder failed");
+ expect(frames.every((frame) => frame.closed)).toBe(true);
+ expect(files.size).toBe(0);
+});
diff --git a/web/package-lock.json b/web/package-lock.json
index b5a8912212..8458153d8c 100644
--- a/web/package-lock.json
+++ b/web/package-lock.json
@@ -12210,7 +12210,6 @@
"name": "ente-gallery",
"version": "0.0.0",
"dependencies": {
- "@ffmpeg/ffmpeg": "0.12.15",
"@hugeicons/core-free-icons": "1.2.1",
"@hugeicons/react": "1.1.6",
"bs58": "6.0.0",
@@ -12244,6 +12243,7 @@
"name": "ente-media",
"version": "0.0.0",
"dependencies": {
+ "@ffmpeg/ffmpeg": "0.12.15",
"ente-base": "*",
"ente-utils": "*",
"file-type": "21.3.4",
diff --git a/web/packages/gallery/package.json b/web/packages/gallery/package.json
index 1efd2915ba..f7bbfa51e7 100644
--- a/web/packages/gallery/package.json
+++ b/web/packages/gallery/package.json
@@ -3,7 +3,6 @@
"version": "0.0.0",
"private": true,
"dependencies": {
- "@ffmpeg/ffmpeg": "0.12.15",
"@hugeicons/core-free-icons": "1.2.1",
"@hugeicons/react": "1.1.6",
"bs58": "6.0.0",
diff --git a/web/packages/gallery/services/ffmpeg/constants.ts b/web/packages/gallery/services/ffmpeg/constants.ts
index dae03a9976..0a098cf212 100644
--- a/web/packages/gallery/services/ffmpeg/constants.ts
+++ b/web/packages/gallery/services/ffmpeg/constants.ts
@@ -1,5 +1,5 @@
-/* Ditto in the desktop app's code (used by the native FFmpeg invocation). */
-
-export const ffmpegPathPlaceholder = "FFMPEG";
-export const inputPathPlaceholder = "INPUT";
-export const outputPathPlaceholder = "OUTPUT";
+export {
+ ffmpegPathPlaceholder,
+ inputPathPlaceholder,
+ outputPathPlaceholder,
+} from "ente-media/ffmpeg/constants";
diff --git a/web/packages/gallery/services/ffmpeg/web.ts b/web/packages/gallery/services/ffmpeg/web.ts
index 229ecb6c20..6657ba5b1c 100644
--- a/web/packages/gallery/services/ffmpeg/web.ts
+++ b/web/packages/gallery/services/ffmpeg/web.ts
@@ -1,258 +1,4 @@
-import { FFFSType, FFmpeg } from "@ffmpeg/ffmpeg";
-import { joinPath } from "ente-base/file-name";
-import { newID } from "ente-base/id";
-import log from "ente-base/log";
-import type { FFmpegCommand } from "ente-base/types/ipc";
-import { ensureArrayBufferBacked } from "ente-utils/bytes";
-import { PromiseQueue } from "ente-utils/promise";
-import { z } from "zod";
-import {
- ffmpegPathPlaceholder,
- inputPathPlaceholder,
- outputPathPlaceholder,
-} from "./constants";
-
-let _ffmpeg: Promise<FFmpeg> | undefined;
-
-// Interleaved ffmpeg.wasm calls can corrupt its memory.
-const _ffmpegTaskQueue = new PromiseQueue<Uint8Array<ArrayBuffer> | number>();
-
-const ffmpegLazy = (): Promise<FFmpeg> => (_ffmpeg ??= createFFmpeg());
-
-const createFFmpeg = async () => {
- const ffmpeg = new FFmpeg();
- await ffmpeg.load({
- coreURL: "https://assets.ente.com/ffmpeg-core-0.12.10/ffmpeg-core.js",
- wasmURL: "https://assets.ente.com/ffmpeg-core-0.12.10/ffmpeg-core.wasm",
- });
- return ffmpeg;
-};
-
-export const ffmpegExecWeb = async (
- command: FFmpegCommand,
- blob: Blob,
- outputFileExtension: string,
-): Promise<Uint8Array<ArrayBuffer>> => {
- const ffmpeg = await ffmpegLazy();
- return _ffmpegTaskQueue.add(() =>
- ffmpegExec(ffmpeg, command, outputFileExtension, blob),
- ) as Promise<Uint8Array<ArrayBuffer>>;
-};
-
-export const determineVideoDurationWeb = async (
- blob: Blob,
-): Promise<number> => {
- const ffmpeg = await ffmpegLazy();
- return _ffmpegTaskQueue.add(() =>
- ffprobeExecVideoDuration(ffmpeg, blob),
- ) as Promise<number>;
-};
-
-const ffmpegExec = async (
- ffmpeg: FFmpeg,
- command: FFmpegCommand,
- outputFileExtension: string,
- blob: Blob,
-): Promise<Uint8Array<ArrayBuffer>> => {
- const outputSuffix = outputFileExtension ? "." + outputFileExtension : "";
- const outputPath = newID("out_") + outputSuffix;
-
- let status: number | undefined;
-
- return withInputMount(ffmpeg, blob, async (inputPath) => {
- try {
- const startTime = Date.now();
-
- let resolvedCommand: string[];
- if (Array.isArray(command)) {
- resolvedCommand = command;
- } else {
- const isHDR = await isHDRVideo(ffmpeg, inputPath);
- resolvedCommand = isHDR ? command.hdr : command.default;
- }
-
- const cmd = substitutePlaceholders(
- resolvedCommand,
- inputPath,
- outputPath,
- );
-
- status = await ffmpeg.exec(cmd);
- if (status !== 0) {
- log.info(
- `[wasm] ffmpeg command failed with exit code ${status}: ${cmd.join(" ")}`,
- );
- throw new Error(
- `ffmpeg command failed with exit code ${status}`,
- );
- }
-
- const result = await ffmpeg.readFile(outputPath);
- if (typeof result == "string")
- throw new Error("Expected binary data");
-
- const ms = Date.now() - startTime;
- log.debug(() => `[wasm] ffmpeg ${cmd.join(" ")} (${ms} ms)`);
- return ensureArrayBufferBacked(result);
- } finally {
- try {
- await ffmpeg.deleteFile(outputPath);
- } catch (e) {
- // A failed command may not create the output file.
- if (status === 0) {
- log.error(`Failed to remove output ${outputPath}`, e);
- }
- }
- }
- });
-};
-
-const withInputMount = async <T>(
- ffmpeg: FFmpeg,
- blob: Blob,
- f: (inputPath: string) => Promise<T>,
-): Promise<T> => {
- const mountDir = "/mount";
- const inputFileName = newID("in_");
- const inputPath = joinPath(mountDir, inputFileName);
-
- const inputFile = new File([blob], inputFileName);
-
- try {
- await ffmpeg.createDir(mountDir);
- await ffmpeg.mount(FFFSType.WORKERFS, { files: [inputFile] }, mountDir);
-
- return await f(inputPath);
- } finally {
- try {
- await ffmpeg.unmount(mountDir);
- } catch (e) {
- log.error(`Failed to remove mount ${mountDir}`, e);
- }
- try {
- await ffmpeg.deleteDir(mountDir);
- } catch (e) {
- log.error(`Failed to delete mount directory ${mountDir}`, e);
- }
- }
-};
-
-const substitutePlaceholders = (
- command: string[],
- inputFilePath: string,
- outputFilePath: string,
-) =>
- command
- .map((segment) => {
- if (segment == ffmpegPathPlaceholder) {
- return undefined;
- } else if (segment == inputPathPlaceholder) {
- return inputFilePath;
- } else if (segment == outputPathPlaceholder) {
- return outputFilePath;
- } else {
- return segment;
- }
- })
- .filter((s) => s !== undefined);
-
-const FFProbeOutputIsHDR = z.object({
- streams: z.array(z.object({ color_transfer: z.string().optional() })),
-});
-
-const isHDRVideo = async (ffmpeg: FFmpeg, inputFilePath: string) => {
- let jsonString: string | undefined;
- try {
- jsonString = await ffprobeOutput(
- ffmpeg,
- [
- ["-i", inputFilePath],
- "-show_streams",
- // FFmpeg may auto-select another stream in multi-stream files.
- ["-select_streams", "v:0"],
- ["-of", "json"],
- ["-o", "output.json"],
- ].flat(),
- "output.json",
- );
-
- const output = FFProbeOutputIsHDR.parse(JSON.parse(jsonString));
- switch (output.streams[0]?.color_transfer) {
- case "smpte2084":
- case "arib-std-b67":
- return true;
- default:
- return false;
- }
- } catch (e) {
- log.warn("Could not detect HDR status", e);
- if (jsonString) log.debug(() => ["ffprobe-output", jsonString]);
- return false;
- }
-};
-
-const ffprobeOutput = async (
- ffmpeg: FFmpeg,
- cmd: string[],
- outputPath: string,
-) => {
- let status: number | undefined;
-
- try {
- status = await ffmpeg.ffprobe(cmd);
- // ffmpeg.wasm currently returns -1 on success.
- if (status !== 0 && status != -1) {
- log.info(
- `[wasm] ffprobe command failed with exit code ${status}: ${cmd.join(" ")}`,
- );
- throw new Error(`ffprobe command failed with exit code ${status}`);
- }
-
- const result = await ffmpeg.readFile(outputPath, "utf8");
- if (typeof result != "string") throw new Error("Expected text data");
-
- return result;
- } finally {
- try {
- await ffmpeg.deleteFile(outputPath);
- } catch (e) {
- // Output file might not even exist if the command did not succeed,
- // so only log on success.
- if (status === 0 || status == -1) {
- log.error(`Failed to remove output ${outputPath}`, e);
- }
- }
- }
-};
-
-const FFProbeOutputDuration = z.object({
- format: z.object({ duration: z.string() }),
-});
-
-const ffprobeExecVideoDuration = async (ffmpeg: FFmpeg, blob: Blob) =>
- withInputMount(ffmpeg, blob, async (inputPath) => {
- // Scalar output can contain extra lines; JSON is more reliable.
- const jsonString = await ffprobeOutput(
- ffmpeg,
- [
- ["-i", inputPath],
- ["-v", "error"],
- ["-show_entries", "format=duration"],
- ["-of", "json"],
- ["-o", "output.json"],
- ].flat(),
- "output.json",
- );
-
- const durationString = FFProbeOutputDuration.parse(
- JSON.parse(jsonString),
- ).format.duration;
-
- const duration = parseFloat(durationString);
- if (isNaN(duration)) {
- const msg = "Could not parse video duration";
- log.warn(msg, durationString);
- throw new Error(msg);
- }
- return duration;
- });
+export {
+ determineVideoDurationWeb,
+ ffmpegExecWeb,
+} from "ente-media/ffmpeg/web";
diff --git a/web/packages/media/ffmpeg/constants.ts b/web/packages/media/ffmpeg/constants.ts
new file mode 100644
index 0000000000..dae03a9976
--- /dev/null
+++ b/web/packages/media/ffmpeg/constants.ts
@@ -0,0 +1,5 @@
+/* Ditto in the desktop app's code (used by the native FFmpeg invocation). */
+
+export const ffmpegPathPlaceholder = "FFMPEG";
+export const inputPathPlaceholder = "INPUT";
+export const outputPathPlaceholder = "OUTPUT";
diff --git a/web/packages/media/ffmpeg/web-codecs.ts b/web/packages/media/ffmpeg/web-codecs.ts
new file mode 100644
index 0000000000..582ed4f717
--- /dev/null
+++ b/web/packages/media/ffmpeg/web-codecs.ts
@@ -0,0 +1,442 @@
+import type { FFmpeg } from "@ffmpeg/ffmpeg";
+import { newID } from "ente-base/id";
+import { ensureArrayBufferBacked } from "ente-utils/bytes";
+import { z } from "zod";
+
+export interface VideoTranscodeEdit {
+ start: number;
+ end: number;
+ muted: boolean;
+}
+
+const Probe = z.object({
+ format: z.object({ format_name: z.string() }),
+ streams: z.array(
+ z.object({
+ codec_name: z.string(),
+ codec_tag_string: z.string(),
+ width: z.number().int().positive(),
+ height: z.number().int().positive(),
+ pix_fmt: z.string(),
+ color_transfer: z.string().optional(),
+ color_primaries: z.string().optional(),
+ sample_aspect_ratio: z.string().optional(),
+ start_time: z.string().optional(),
+ side_data_list: z
+ .array(
+ z.object({
+ rotation: z.number().optional(),
+ displaymatrix: z.string().optional(),
+ }),
+ )
+ .optional(),
+ }),
+ ),
+ packets: z.array(
+ z.object({
+ pts_time: z.coerce.number(),
+ duration_time: z.coerce.number().positive(),
+ pos: z.coerce.number().int().nonnegative(),
+ size: z.coerce.number().int().positive(),
+ flags: z.string(),
+ }),
+ ),
+});
+
+type Packet = z.infer<typeof Probe>["packets"][number];
+
+const frameRate = 30;
+const frameTime = (index: number) =>
+ Math.round((index * 1_000_000) / frameRate);
+
+export const encodeVideoWithWebCodecs = async (
+ ffmpeg: FFmpeg,
+ inputPath: string,
+ blob: Blob,
+ edit: VideoTranscodeEdit,
+ signal?: AbortSignal,
+): Promise<Uint8Array<ArrayBuffer> | undefined> => {
+ signal?.throwIfAborted();
+ const frameCount = Math.floor(
+ (edit.end - edit.start) * frameRate + 0.000001,
+ );
+ if (frameCount < 1) return;
+ const directory = newID("webcodecs_");
+ await ffmpeg.createDir(directory);
+ const probePath = `${directory}/probe.json`;
+ const descriptionPath = `${directory}/codec.bin`;
+ const videoPath = `${directory}/video.h264`;
+ const outputPath = `${directory}/video.mp4`;
+ const exec = async (command: string[]) => {
+ signal?.throwIfAborted();
+ const status = await ffmpeg.exec(command);
+ if (status !== 0) throw new Error(`FFmpeg exited with ${status}`);
+ };
+ const readBytes = async (path: string) => {
+ const bytes = await ffmpeg.readFile(path);
+ if (typeof bytes === "string") throw new Error("Expected binary data");
+ return ensureArrayBufferBacked(bytes);
+ };
+ try {
+ const status = await ffmpeg.ffprobe([
+ "-v",
+ "error",
+ "-select_streams",
+ "v:0",
+ "-show_entries",
+ "format=format_name:stream=codec_name,codec_tag_string,width,height,pix_fmt,color_transfer,color_primaries,sample_aspect_ratio,start_time:stream_side_data=rotation,displaymatrix:packet=pts_time,duration_time,pos,size,flags",
+ "-of",
+ "json",
+ "-o",
+ probePath,
+ inputPath,
+ ]);
+ if (status !== 0 && status !== -1)
+ throw new Error("Couldn't read video packets");
+ const json = await ffmpeg.readFile(probePath, "utf8");
+ if (typeof json !== "string")
+ throw new Error("Expected video information");
+ const probe = Probe.parse(JSON.parse(json));
+ const stream = probe.streams[0];
+ if (
+ !probe.format.format_name.split(",").includes("mov") ||
+ stream?.codec_name !== "h264" ||
+ !["avc1", "avc3"].includes(stream.codec_tag_string) ||
+ stream.pix_fmt !== "yuv420p" ||
+ ["smpte2084", "arib-std-b67"].includes(
+ stream.color_transfer ?? "",
+ ) ||
+ stream.color_primaries === "bt2020" ||
+ (stream.sample_aspect_ratio &&
+ stream.sample_aspect_ratio !== "1:1") ||
+ Number(stream.start_time ?? 0) !== 0
+ )
+ return;
+ const display = stream.side_data_list?.find(
+ (data) => data.displaymatrix,
+ );
+ const rotation = display?.rotation ?? 0;
+ if (![0, 90, -90, 180, -180].includes(rotation)) return;
+ if (display?.displaymatrix) {
+ const matrix = display.displaymatrix
+ .trim()
+ .split("\n")
+ .flatMap((line) =>
+ line
+ .slice(line.indexOf(":") + 1)
+ .trim()
+ .split(/\s+/)
+ .map(Number),
+ );
+ const angle = (rotation * Math.PI) / 180;
+ const expected = [
+ Math.round(Math.cos(angle) * 65536),
+ -Math.round(Math.sin(angle) * 65536),
+ 0,
+ Math.round(Math.sin(angle) * 65536),
+ Math.round(Math.cos(angle) * 65536),
+ 0,
+ 0,
+ 0,
+ 1073741824,
+ ];
+ if (
+ matrix.length !== 9 ||
+ matrix.some((value, index) => value !== expected[index])
+ )
+ return;
+ }
+ if (
+ !probe.packets.length ||
+ probe.packets.some((packet) => packet.pos + packet.size > blob.size)
+ )
+ return;
+ await exec([
+ "-dump_attachment:v:0",
+ descriptionPath,
+ "-i",
+ inputPath,
+ "-t",
+ "0",
+ "-c",
+ "copy",
+ "-f",
+ "null",
+ "-",
+ ]);
+ const description = await readBytes(descriptionPath);
+ if (description.length < 7 || description[0] !== 1) return;
+ const decoderConfig: VideoDecoderConfig = {
+ codec:
+ "avc1." +
+ Array.from(description.slice(1, 4), (value) =>
+ value.toString(16).padStart(2, "0"),
+ ).join(""),
+ description,
+ hardwareAcceleration: "prefer-hardware",
+ };
+ const sideways = Math.abs(rotation) === 90;
+ const width = sideways ? stream.height : stream.width;
+ const height = sideways ? stream.width : stream.height;
+ const scale = Math.min(
+ 1,
+ 1920 / Math.max(width, height),
+ 1080 / Math.min(width, height),
+ );
+ const encoderConfig: VideoEncoderConfig = {
+ codec: "avc1.640028",
+ width: Math.floor((width * scale) / 2) * 2,
+ height: Math.floor((height * scale) / 2) * 2,
+ bitrate: 8_000_000,
+ bitrateMode: "variable",
+ framerate: frameRate,
+ latencyMode: "realtime",
+ hardwareAcceleration: "prefer-hardware",
+ avc: { format: "annexb" },
+ };
+ const [decoderSupport, encoderSupport] = await Promise.all([
+ VideoDecoder.isConfigSupported(decoderConfig),
+ VideoEncoder.isConfigSupported(encoderConfig),
+ ]);
+ if (!decoderSupport.supported || !encoderSupport.supported) return;
+ const video = await encodeFrames(
+ blob,
+ probe.packets,
+ edit.start,
+ frameCount,
+ rotation,
+ decoderConfig,
+ encoderConfig,
+ signal,
+ );
+ signal?.throwIfAborted();
+ await ffmpeg.writeFile(
+ videoPath,
+ new Uint8Array(await video.arrayBuffer()),
+ );
+ await exec([
+ "-ss",
+ String(edit.start),
+ "-i",
+ inputPath,
+ "-r",
+ String(frameRate),
+ "-i",
+ videoPath,
+ "-t",
+ String(frameCount / frameRate),
+ "-map",
+ "1:v:0",
+ "-c:v",
+ "copy",
+ "-bsf:v",
+ "setts=ts=N*1000:duration=1000:time_base=1/30000",
+ "-video_track_timescale",
+ "30000",
+ ...(edit.muted
+ ? ["-an"]
+ : ["-map", "0:a:0?", "-c:a", "aac", "-b:a", "192k"]),
+ "-map_metadata",
+ "-1",
+ "-map_metadata:s",
+ "-1",
+ "-map_chapters",
+ "-1",
+ "-movflags",
+ "+faststart",
+ outputPath,
+ ]);
+ return await readBytes(outputPath);
+ } finally {
+ if (ffmpeg.loaded) {
+ for (const file of await ffmpeg.listDir(directory)) {
+ if (!file.isDir)
+ await ffmpeg.deleteFile(`${directory}/${file.name}`);
+ }
+ await ffmpeg.deleteDir(directory);
+ }
+ }
+};
+
+const encodeFrames = async (
+ blob: Blob,
+ packets: Packet[],
+ start: number,
+ frameCount: number,
+ rotation: number,
+ decoderConfig: VideoDecoderConfig,
+ encoderConfig: VideoEncoderConfig,
+ signal?: AbortSignal,
+) => {
+ signal?.throwIfAborted();
+ const startTime = Math.round(start * 1_000_000);
+ const durations = new Map(
+ packets.map((packet) => [
+ Math.round(packet.pts_time * 1_000_000),
+ Math.round(packet.duration_time * 1_000_000),
+ ]),
+ );
+ let first = -1;
+ for (const [index, packet] of packets.entries()) {
+ if (
+ packet.flags.includes("K") &&
+ Math.round(packet.pts_time * 1_000_000) <= startTime
+ )
+ first = index;
+ }
+ if (first < 0) throw new Error("No keyframe before the selected clip");
+ let failure: Error | undefined;
+ let wake: (() => void) | undefined;
+ const notify = () => {
+ wake?.();
+ wake = undefined;
+ };
+ const fail = (error: Error) => {
+ failure = error;
+ notify();
+ };
+ const checkError = () => {
+ signal?.throwIfAborted();
+ if (failure) throw failure;
+ };
+ let previous: VideoFrame | undefined;
+ let encoded = 0;
+ const chunks: Uint8Array<ArrayBuffer>[] = [];
+ const canvas = rotation
+ ? new OffscreenCanvas(encoderConfig.width, encoderConfig.height)
+ : undefined;
+ const context = canvas?.getContext("2d");
+ if (canvas && !context) throw new Error("Couldn't rotate the video");
+ const encoder = new VideoEncoder({
+ output(chunk) {
+ if (chunk.timestamp !== frameTime(chunks.length)) {
+ fail(new Error("Video encoder reordered the output frames"));
+ return;
+ }
+ const bytes = new Uint8Array(chunk.byteLength);
+ chunk.copyTo(bytes);
+ chunks.push(bytes);
+ notify();
+ },
+ error: fail,
+ });
+ const encodeUntil = (time: number) => {
+ if (!previous) return;
+ while (encoded < frameCount && startTime + frameTime(encoded) < time) {
+ checkError();
+ const timestamp = frameTime(encoded);
+ if (canvas && context) {
+ const sideways = Math.abs(rotation) === 90;
+ const width = sideways ? canvas.height : canvas.width;
+ const height = sideways ? canvas.width : canvas.height;
+ context.setTransform(
+ 1,
+ 0,
+ 0,
+ 1,
+ canvas.width / 2,
+ canvas.height / 2,
+ );
+ context.rotate((-rotation * Math.PI) / 180);
+ context.drawImage(
+ previous,
+ -width / 2,
+ -height / 2,
+ width,
+ height,
+ );
+ }
+ const frame = new VideoFrame(canvas ?? previous, {
+ timestamp,
+ duration: frameTime(encoded + 1) - timestamp,
+ });
+ try {
+ encoder.encode(frame, { keyFrame: encoded % 60 === 0 });
+ encoded++;
+ } finally {
+ frame.close();
+ }
+ }
+ };
+ const decoder = new VideoDecoder({
+ output(frame) {
+ if (failure || encoded === frameCount) {
+ frame.close();
+ return;
+ }
+ try {
+ if (!previous && frame.timestamp > startTime)
+ throw new Error("Video starts after the selected clip");
+ encodeUntil(frame.timestamp);
+ previous?.close();
+ previous = frame;
+ } catch (error) {
+ frame.close();
+ fail(error as Error);
+ }
+ notify();
+ },
+ error: fail,
+ });
+ const abort = () => {
+ fail(new DOMException("Canceled", "AbortError"));
+ if (decoder.state !== "closed") decoder.close();
+ if (encoder.state !== "closed") encoder.close();
+ };
+ decoder.addEventListener("dequeue", notify);
+ encoder.addEventListener("dequeue", notify);
+ signal?.addEventListener("abort", abort, { once: true });
+ try {
+ signal?.throwIfAborted();
+ encoder.configure(encoderConfig);
+ decoder.configure(decoderConfig);
+ for (
+ let index = first;
+ index < packets.length && encoded < frameCount;
+ index++
+ ) {
+ while (decoder.decodeQueueSize > 8 || encoder.encodeQueueSize > 8) {
+ checkError();
+ await new Promise<void>((resolve) => {
+ wake = resolve;
+ });
+ }
+ checkError();
+ const packet = packets[index]!;
+ const data = await blob
+ .slice(packet.pos, packet.pos + packet.size)
+ .arrayBuffer();
+ checkError();
+ decoder.decode(
+ new EncodedVideoChunk({
+ type: packet.flags.includes("K") ? "key" : "delta",
+ timestamp: Math.round(packet.pts_time * 1_000_000),
+ duration: Math.round(packet.duration_time * 1_000_000),
+ data,
+ }),
+ );
+ }
+ await decoder.flush();
+ checkError();
+ if (previous)
+ encodeUntil(
+ previous.timestamp + (durations.get(previous.timestamp) ?? 0),
+ );
+ await encoder.flush();
+ checkError();
+ if (chunks.length !== frameCount)
+ throw new Error("Video export is missing frames");
+ return new Blob(chunks);
+ } finally {
+ signal?.removeEventListener("abort", abort);
+ decoder.removeEventListener("dequeue", notify);
+ encoder.removeEventListener("dequeue", notify);
+ previous?.close();
+ if (decoder.state !== "closed") decoder.close();
+ if (encoder.state !== "closed") encoder.close();
+ if (canvas) {
+ canvas.width = 1;
+ canvas.height = 1;
+ }
+ }
+};
diff --git a/web/packages/media/ffmpeg/web.ts b/web/packages/media/ffmpeg/web.ts
new file mode 100644
index 0000000000..32d1718cf8
--- /dev/null
+++ b/web/packages/media/ffmpeg/web.ts
@@ -0,0 +1,344 @@
+import { FFFSType, FFmpeg } from "@ffmpeg/ffmpeg";
+import { joinPath } from "ente-base/file-name";
+import { newID } from "ente-base/id";
+import log from "ente-base/log";
+import type { FFmpegCommand } from "ente-base/types/ipc";
+import { ensureArrayBufferBacked } from "ente-utils/bytes";
+import { PromiseQueue } from "ente-utils/promise";
+import { z } from "zod";
+import {
+ ffmpegPathPlaceholder,
+ inputPathPlaceholder,
+ outputPathPlaceholder,
+} from "./constants";
+import type { VideoTranscodeEdit } from "./web-codecs";
+
+let _ffmpeg: Promise<FFmpeg> | undefined;
+
+// Interleaved ffmpeg.wasm calls can corrupt its memory.
+const _ffmpegTaskQueue = new PromiseQueue<unknown>();
+
+const ffmpegLazy = (signal?: AbortSignal): Promise<FFmpeg> =>
+ (_ffmpeg ??= createFFmpeg(signal));
+
+const createFFmpeg = async (signal?: AbortSignal) => {
+ const ffmpeg = new FFmpeg();
+ try {
+ await ffmpeg.load(
+ {
+ coreURL:
+ "https://assets.ente.com/ffmpeg-core-0.12.10/ffmpeg-core.js",
+ wasmURL:
+ "https://assets.ente.com/ffmpeg-core-0.12.10/ffmpeg-core.wasm",
+ },
+ { signal },
+ );
+ return ffmpeg;
+ } catch (error) {
+ ffmpeg.terminate();
+ _ffmpeg = undefined;
+ throw error;
+ }
+};
+
+export const ffmpegExecWeb = async (
+ command: FFmpegCommand,
+ blob: Blob,
+ outputFileExtension: string,
+ options?: { signal?: AbortSignal; onProgress?: (seconds: number) => void },
+): Promise<Uint8Array<ArrayBuffer>> => {
+ return runFFmpegTask(async (ffmpeg) => {
+ const progress = ({ time }: { time: number }) =>
+ options?.onProgress?.(time / 1_000_000);
+ ffmpeg.on("progress", progress);
+ try {
+ return await ffmpegExec(ffmpeg, command, outputFileExtension, blob);
+ } finally {
+ ffmpeg.off("progress", progress);
+ }
+ }, options?.signal);
+};
+
+export const determineVideoDurationWeb = async (
+ blob: Blob,
+ signal?: AbortSignal,
+): Promise<number> =>
+ runFFmpegTask((ffmpeg) => ffprobeExecVideoDuration(ffmpeg, blob), signal);
+
+export const transcodeVideoWeb = (
+ blob: Blob,
+ edit: VideoTranscodeEdit,
+ fallbackCommand: FFmpegCommand,
+ signal?: AbortSignal,
+) =>
+ runFFmpegTask(async (ffmpeg) => {
+ if (
+ typeof VideoDecoder !== "undefined" &&
+ typeof VideoEncoder !== "undefined"
+ ) {
+ try {
+ const { encodeVideoWithWebCodecs } =
+ await import("./web-codecs");
+ const result = await withInputMount(ffmpeg, blob, (inputPath) =>
+ encodeVideoWithWebCodecs(
+ ffmpeg,
+ inputPath,
+ blob,
+ edit,
+ signal,
+ ),
+ );
+ if (result) {
+ log.debug(() => "Encoded video with WebCodecs");
+ return result;
+ }
+ } catch (error) {
+ signal?.throwIfAborted();
+ log.warn("WebCodecs export failed, using FFmpeg", error);
+ }
+ }
+ signal?.throwIfAborted();
+ return ffmpegExec(ffmpeg, fallbackCommand, "mp4", blob);
+ }, signal);
+
+const runFFmpegTask = <T>(
+ task: (ffmpeg: FFmpeg) => Promise<T>,
+ signal?: AbortSignal,
+): Promise<T> => {
+ const pending = _ffmpegTaskQueue.add(async () => {
+ signal?.throwIfAborted();
+ const ffmpeg = await ffmpegLazy(signal);
+ signal?.throwIfAborted();
+ const abort = () => {
+ ffmpeg.terminate();
+ _ffmpeg = undefined;
+ };
+ signal?.addEventListener("abort", abort, { once: true });
+ try {
+ return await task(ffmpeg);
+ } catch (error) {
+ signal?.throwIfAborted();
+ throw error;
+ } finally {
+ signal?.removeEventListener("abort", abort);
+ }
+ }) as Promise<T>;
+ if (!signal) return pending;
+ return new Promise<T>((resolve, reject) => {
+ const abort = () => reject(new DOMException("Canceled", "AbortError"));
+ signal.addEventListener("abort", abort, { once: true });
+ void pending
+ .then(resolve, reject)
+ .finally(() => signal.removeEventListener("abort", abort));
+ if (signal.aborted) abort();
+ });
+};
+
+const ffmpegExec = async (
+ ffmpeg: FFmpeg,
+ command: FFmpegCommand,
+ outputFileExtension: string,
+ blob: Blob,
+): Promise<Uint8Array<ArrayBuffer>> => {
+ const outputSuffix = outputFileExtension ? "." + outputFileExtension : "";
+ const outputPath = newID("out_") + outputSuffix;
+
+ let status: number | undefined;
+
+ return withInputMount(ffmpeg, blob, async (inputPath) => {
+ try {
+ const startTime = Date.now();
+
+ let resolvedCommand: string[];
+ if (Array.isArray(command)) {
+ resolvedCommand = command;
+ } else {
+ const isHDR = await isHDRVideo(ffmpeg, inputPath);
+ resolvedCommand = isHDR ? command.hdr : command.default;
+ }
+
+ const cmd = substitutePlaceholders(
+ resolvedCommand,
+ inputPath,
+ outputPath,
+ );
+
+ status = await ffmpeg.exec(cmd);
+ if (status !== 0) {
+ log.info(
+ `[wasm] ffmpeg command failed with exit code ${status}: ${cmd.join(" ")}`,
+ );
+ throw new Error(
+ `ffmpeg command failed with exit code ${status}`,
+ );
+ }
+
+ const result = await ffmpeg.readFile(outputPath);
+ if (typeof result == "string")
+ throw new Error("Expected binary data");
+
+ const ms = Date.now() - startTime;
+ log.debug(() => `[wasm] ffmpeg ${cmd.join(" ")} (${ms} ms)`);
+ return ensureArrayBufferBacked(result);
+ } finally {
+ try {
+ if (ffmpeg.loaded) await ffmpeg.deleteFile(outputPath);
+ } catch (e) {
+ // A failed command may not create the output file.
+ if (status === 0) {
+ log.error(`Failed to remove output ${outputPath}`, e);
+ }
+ }
+ }
+ });
+};
+
+const withInputMount = async <T>(
+ ffmpeg: FFmpeg,
+ blob: Blob,
+ f: (inputPath: string) => Promise<T>,
+): Promise<T> => {
+ const mountDir = "/mount";
+ const inputFileName = newID("in_");
+ const inputPath = joinPath(mountDir, inputFileName);
+
+ const inputFile = new File([blob], inputFileName);
+
+ try {
+ await ffmpeg.createDir(mountDir);
+ await ffmpeg.mount(FFFSType.WORKERFS, { files: [inputFile] }, mountDir);
+
+ return await f(inputPath);
+ } finally {
+ try {
+ if (ffmpeg.loaded) await ffmpeg.unmount(mountDir);
+ } catch (e) {
+ log.error(`Failed to remove mount ${mountDir}`, e);
+ }
+ try {
+ if (ffmpeg.loaded) await ffmpeg.deleteDir(mountDir);
+ } catch (e) {
+ log.error(`Failed to delete mount directory ${mountDir}`, e);
+ }
+ }
+};
+
+const substitutePlaceholders = (
+ command: string[],
+ inputFilePath: string,
+ outputFilePath: string,
+) =>
+ command
+ .map((segment) => {
+ if (segment == ffmpegPathPlaceholder) {
+ return undefined;
+ } else if (segment == inputPathPlaceholder) {
+ return inputFilePath;
+ } else if (segment == outputPathPlaceholder) {
+ return outputFilePath;
+ } else {
+ return segment;
+ }
+ })
+ .filter((s) => s !== undefined);
+
+const FFProbeOutputIsHDR = z.object({
+ streams: z.array(z.object({ color_transfer: z.string().optional() })),
+});
+
+const isHDRVideo = async (ffmpeg: FFmpeg, inputFilePath: string) => {
+ let jsonString: string | undefined;
+ try {
+ jsonString = await ffprobeOutput(
+ ffmpeg,
+ [
+ ["-i", inputFilePath],
+ "-show_streams",
+ // FFmpeg may auto-select another stream in multi-stream files.
+ ["-select_streams", "v:0"],
+ ["-of", "json"],
+ ["-o", "output.json"],
+ ].flat(),
+ "output.json",
+ );
+
+ const output = FFProbeOutputIsHDR.parse(JSON.parse(jsonString));
+ switch (output.streams[0]?.color_transfer) {
+ case "smpte2084":
+ case "arib-std-b67":
+ return true;
+ default:
+ return false;
+ }
+ } catch (e) {
+ log.warn("Could not detect HDR status", e);
+ if (jsonString) log.debug(() => ["ffprobe-output", jsonString]);
+ return false;
+ }
+};
+
+const ffprobeOutput = async (
+ ffmpeg: FFmpeg,
+ cmd: string[],
+ outputPath: string,
+) => {
+ let status: number | undefined;
+
+ try {
+ status = await ffmpeg.ffprobe(cmd);
+ // ffmpeg.wasm currently returns -1 on success.
+ if (status !== 0 && status != -1) {
+ log.info(
+ `[wasm] ffprobe command failed with exit code ${status}: ${cmd.join(" ")}`,
+ );
+ throw new Error(`ffprobe command failed with exit code ${status}`);
+ }
+
+ const result = await ffmpeg.readFile(outputPath, "utf8");
+ if (typeof result != "string") throw new Error("Expected text data");
+
+ return result;
+ } finally {
+ try {
+ if (ffmpeg.loaded) await ffmpeg.deleteFile(outputPath);
+ } catch (e) {
+ // Output file might not even exist if the command did not succeed,
+ // so only log on success.
+ if (status === 0 || status == -1) {
+ log.error(`Failed to remove output ${outputPath}`, e);
+ }
+ }
+ }
+};
+
+const FFProbeOutputDuration = z.object({
+ format: z.object({ duration: z.string() }),
+});
+
+const ffprobeExecVideoDuration = async (ffmpeg: FFmpeg, blob: Blob) =>
+ withInputMount(ffmpeg, blob, async (inputPath) => {
+ // Scalar output can contain extra lines; JSON is more reliable.
+ const jsonString = await ffprobeOutput(
+ ffmpeg,
+ [
+ ["-i", inputPath],
+ ["-v", "error"],
+ ["-show_entries", "format=duration"],
+ ["-of", "json"],
+ ["-o", "output.json"],
+ ].flat(),
+ "output.json",
+ );
+
+ const durationString = FFProbeOutputDuration.parse(
+ JSON.parse(jsonString),
+ ).format.duration;
+
+ const duration = parseFloat(durationString);
+ if (isNaN(duration)) {
+ const msg = "Could not parse video duration";
+ log.warn(msg, durationString);
+ throw new Error(msg);
+ }
+ return duration;
+ });
diff --git a/web/packages/media/package.json b/web/packages/media/package.json
index bfa9d5494b..48630304cf 100644
--- a/web/packages/media/package.json
+++ b/web/packages/media/package.json
@@ -3,6 +3,7 @@
"version": "0.0.0",
"private": true,
"dependencies": {
+ "@ffmpeg/ffmpeg": "0.12.15",
"ente-base": "*",
"ente-utils": "*",
"file-type": "21.3.4",
diff --git a/web/packages/wasm/space/index.ts b/web/packages/wasm/space/index.ts
index 65a3e13de8..f1d5e686a6 100644
--- a/web/packages/wasm/space/index.ts
+++ b/web/packages/wasm/space/index.ts
@@ -20,6 +20,7 @@ export type {
SpaceKeyResponse,
SpaceLinkCtxHandle,
UpdateSpaceProfileResponse,
+ UploadedPostAsset,
} from "./pkg/ente_space_wasm";
const wasm = () => import("./pkg/ente_space_wasm");