Skip to content

Commit

Permalink
Support bypassing the proxy for some Lemmy instances (#338)
Browse files Browse the repository at this point in the history
* Support bypassing the proxy for some Lemmy instances

Since LemmyNet/lemmy#3421 was merged, Lemmy instances on 0.18.1
and newer allow cross-origin requests.

* Fix image uploading not being possible cross origin (yet)

---------

Co-authored-by: Alexander Harding <2166114+aeharding@users.noreply.github.com>
  • Loading branch information
geneccx and aeharding authored Jul 9, 2023
1 parent 19cceb0 commit 351fa29
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/services/lemmy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
import { LemmyHttp } from "lemmy-js-client";
import { reduceFileSize } from "../helpers/imageCompress";

const UNPROXIED_LEMMY_SERVERS = [
"lemmy.ml",
"beehaw.org",
"sh.itjust.works",
"lemm.ee",
"feddit.de",
"midwest.social",
"lemmynsfw.com",
"lemmy.ca",
"lemmy.sdf.org",
];

function buildBaseUrl(url: string): string {
// if (url === "lemmy.world") {
// return `https://lemmy.world`;
// }
if (UNPROXIED_LEMMY_SERVERS.includes(url)) {
return `https://${url}`;
}

return buildProxiedBaseUrl(url);
}

function buildProxiedBaseUrl(url: string): string {
return `${location.origin}/api/${url}`;
}

Expand Down Expand Up @@ -37,8 +53,10 @@ export async function uploadImage(url: string, auth: string, image: File) {

formData.append("images[]", compressedImageIfNeeded);

// All requests for image upload must be proxied due to Lemmy not accepting
// parameterized JWT for this request (see: https://github.com/LemmyNet/lemmy/issues/3567)
const response = await fetch(
`${buildBaseUrl(url)}${PICTRS_URL}?${new URLSearchParams({ auth })}`,
`${buildProxiedBaseUrl(url)}${PICTRS_URL}?${new URLSearchParams({ auth })}`,
{
method: "POST",
body: formData,
Expand Down

0 comments on commit 351fa29

Please sign in to comment.