Skip to content

Commit

Permalink
Merge branch 'comment-webm'
Browse files Browse the repository at this point in the history
  • Loading branch information
sharunkumar committed Nov 8, 2023
2 parents 2c98cbf + ca9720e commit 131a797
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
10 changes: 2 additions & 8 deletions src/features/gallery/GalleryImg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import "photoswipe/dist/photoswipe.css";
import { PostView } from "lemmy-js-client";
import { GalleryContext } from "./GalleryProvider";
import { PreparedPhotoSwipeOptions } from "photoswipe";
import mime from "mime";
import Video from "../shared/Video";
import { isUrlVideo } from "../../helpers/lemmy";

export interface GalleryImgProps {
src?: string;
Expand Down Expand Up @@ -35,11 +35,7 @@ export function GalleryImg({
const imgRef = useRef<HTMLImageElement>(null);
const { open } = useContext(GalleryContext);

const mt = mime.getType(`${src}`);

const isVideo = mt?.startsWith("video/");

const InnerComponent = !isVideo ? (
return !isUrlVideo(`${src}`) ? (
<img
ref={imgRef}
draggable="false"
Expand All @@ -63,6 +59,4 @@ export function GalleryImg({
) : (
<Video src={`${src}`} />
);

return InnerComponent;
}
29 changes: 8 additions & 21 deletions src/helpers/lemmy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from "lemmy-js-client";
import { Share } from "@capacitor/share";
import { escapeStringForRegex } from "./regex";
import mime from "mime";

export interface LemmyJWT {
sub: number;
Expand Down Expand Up @@ -237,36 +238,22 @@ export function getFlattenedChildren(comment: CommentNodeI): CommentView[] {
return flattenedChildren;
}

export function isUrlImage(url: string): boolean {
let parsedUrl;

function isValidUrl(url: string): boolean {
try {
parsedUrl = new URL(url);
new URL(url);
return true;
} catch (error) {
console.error(error);
return false;
}
}

return (
parsedUrl.pathname.endsWith(".jpeg") ||
parsedUrl.pathname.endsWith(".png") ||
parsedUrl.pathname.endsWith(".gif") ||
parsedUrl.pathname.endsWith(".jpg") ||
parsedUrl.pathname.endsWith(".webp")
);
export function isUrlImage(url: string): boolean {
return (isValidUrl(url) && mime.getType(url)?.startsWith("image/")) ?? false;
}

export function isUrlVideo(url: string): boolean {
let parsedUrl;

try {
parsedUrl = new URL(url);
} catch (error) {
console.error(error);
return false;
}

return parsedUrl.pathname.endsWith(".mp4");
return (isValidUrl(url) && mime.getType(url)?.startsWith("video/")) ?? false;
}

export function share(item: Post | Comment) {
Expand Down

0 comments on commit 131a797

Please sign in to comment.