Skip to content

Commit

Permalink
fix: Fix /swagger /flutter not works on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Jan 7, 2025
1 parent 7b6e70a commit 3f8117e
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions routes/_middleware.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FreshContext } from "$fresh/server.ts";
import { basename, join } from "@std/path";
import { basename, join, normalize } from "@std/path";
import {
get_file_response,
GetFileResponseOptions,
Expand Down Expand Up @@ -55,12 +55,11 @@ async function default_handler(req: Request, ctx: FreshContext) {
} else {
flutter_base = m.cfg.flutter_frontend;
}
const u = new URL(req.url);
let p = join(flutter_base, u.pathname.slice(8));
if (!(await exists(p)) || p === flutter_base) {
let p = join(flutter_base, url.pathname.slice(8));
if (!(await exists(p)) || normalize(p) === normalize(flutter_base)) {
p = join(flutter_base, "/index.html");
}
if (u.pathname == "/flutter/manifest.json") {
if (url.pathname == "/flutter/manifest.json") {
const lang = i18n_get_lang(req, ["zh-cn"]);
if (lang !== "en") {
const tp = join(flutter_base, `/manifest.${lang}.json`);
Expand All @@ -70,10 +69,10 @@ async function default_handler(req: Request, ctx: FreshContext) {
}
}
if (
u.pathname.startsWith("/flutter/gallery/") &&
u.searchParams.has("share")
url.pathname.startsWith("/flutter/gallery/") &&
url.searchParams.has("share")
) {
const token = u.searchParams.get("share")!;
const token = url.searchParams.get("share")!;
const st = m.db.get_shared_token(token);
const now = Date.now();
if (
Expand All @@ -82,7 +81,9 @@ async function default_handler(req: Request, ctx: FreshContext) {
) {
const b = `/flutter/gallery/${st.info.gid}`;
const g = m.db.get_gmeta_by_gid(st.info.gid);
if ((u.pathname == b || u.pathname.startsWith(b + "/")) && g) {
if (
(url.pathname == b || url.pathname.startsWith(b + "/")) && g
) {
const html = await Deno.readTextFile(p);
await initDOMParser();
try {
Expand Down Expand Up @@ -166,8 +167,7 @@ async function default_handler(req: Request, ctx: FreshContext) {
} else {
flutter_base = m.cfg.flutter_frontend;
}
const u = new URL(req.url);
const p = join(flutter_base, "canvaskit", u.pathname.slice(11));
const p = join(flutter_base, "canvaskit", url.pathname.slice(11));
const opts: GetFileResponseOptions = {};
opts.range = req.headers.get("range");
opts.if_modified_since = req.headers.get("If-Modified-Since");
Expand All @@ -179,12 +179,11 @@ async function default_handler(req: Request, ctx: FreshContext) {
if (Deno.build.os === "windows") {
swagger_base = swagger_base.slice(1);
}
const u = new URL(req.url);
let p = join(swagger_base, u.pathname.slice(9));
let p = join(swagger_base, url.pathname.slice(9));
if (basename(p) == "swagger-initializer.js") {
p = join(swagger_base, "../swagger-initializer.js");
}
if (!(await exists(p)) || p === swagger_base) {
if (!(await exists(p)) || normalize(p) === normalize(swagger_base)) {
p = join(swagger_base, "/index.html");
}
if (basename(p) == "index.html") {
Expand Down Expand Up @@ -221,7 +220,7 @@ async function default_handler(req: Request, ctx: FreshContext) {
);
} catch (e) {
logger.warn("Failed to handle swagger index.html:", e);
if (u.pathname == "/swagger") {
if (url.pathname == "/swagger") {
return Response.redirect(
`${get_host(req)}/swagger/index.html`,
302,
Expand Down

0 comments on commit 3f8117e

Please sign in to comment.