Skip to content

Commit 88ccd67

Browse files
KirCutexrgzs
andauthored
feat(share): support more secure file sharing (#156)
* feat(share): support more secure file sharing * feat(share): share operation in folder page * feat(i18n): add openlist_share driver addition english translation * fix(archive): decompress disappear in folder context menu * fix(copy_link): incorrect behavior when copy single file share link * refactor(MuseUser): use onMount instead of IIFE * refactor(archive): replace indexOf with includes for better readability * refactor(share): simplify randomPwd function using Array.from * fix(share): use local var instead of store in edit page initialization --------- Signed-off-by: MadDogOwner <[email protected]> Co-authored-by: MadDogOwner <[email protected]>
1 parent ff9f72e commit 88ccd67

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1408
-129
lines changed

.github/workflows/sync_repo.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
run: |
2828
git config user.name "GitHub Actions"
2929
git config user.email "[email protected]"
30-
30+
3131
# Create a new branch
3232
git checkout --orphan new-main
3333
git add .

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@openlist-frontend/openlist-frontend",
33
"version": "4.1.1",
4+
"type": "module",
45
"author": {
56
"name": "The OpenList Projects Contributors",
67
"url": "https://github.com/OpenListTeam"
@@ -80,6 +81,7 @@
8081
"clsx": "^2.1.1",
8182
"copy-to-clipboard": "^3.3.3",
8283
"crypto-js": "^4.2.0",
84+
"handlebars": "^4.7.8",
8385
"hash-wasm": "^4.12.0",
8486
"hls.js": "^1.6.5",
8587
"ini": "^5.0.0",

pnpm-lock.yaml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/App.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { setSettings } from "~/store"
1616
import { setArchiveExtensions } from "~/store/archive"
1717
import { Resp } from "~/types"
1818
import { base_path, bus, handleRespWithoutAuthAndNotify, r } from "~/utils"
19-
import { MustUser } from "./MustUser"
19+
import { MustUser, UserOrGuest } from "./MustUser"
2020
import "./index.css"
2121
import { globalStyles } from "./theme"
2222

@@ -91,6 +91,14 @@ const App: Component = () => {
9191
</MustUser>
9292
}
9393
/>
94+
<Route
95+
path={["/@s/*", "/%40s/*"]}
96+
element={
97+
<UserOrGuest>
98+
<Home />
99+
</UserOrGuest>
100+
}
101+
/>
94102
<Route
95103
path="*"
96104
element={

src/app/MustUser.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { createSignal, JSXElement, Match, Switch } from "solid-js"
1+
import { createSignal, JSXElement, Match, onMount, Switch } from "solid-js"
22
import { Error, FullScreenLoading } from "~/components"
33
import { useFetch, useT } from "~/hooks"
44
import { Me, setMe } from "~/store"
55
import { PResp } from "~/types"
6-
import { r, handleResp } from "~/utils"
6+
import { r, handleResp, handleRespWithoutAuthAndNotify } from "~/utils"
77

88
const MustUser = (props: { children: JSXElement }) => {
99
const t = useT()
@@ -25,4 +25,32 @@ const MustUser = (props: { children: JSXElement }) => {
2525
)
2626
}
2727

28-
export { MustUser }
28+
const UserOrGuest = (props: { children: JSXElement }) => {
29+
const [loading, data] = useFetch((): PResp<Me> => r.get("/me"))
30+
const [skipLogin, setSkipLogin] = createSignal(false)
31+
onMount(async () => {
32+
handleRespWithoutAuthAndNotify(await data(), setMe, (_msg, _code) => {
33+
setMe({
34+
id: 2,
35+
username: "guest",
36+
password: "",
37+
base_path: "/",
38+
role: 1,
39+
disabled: false,
40+
permission: 0,
41+
sso_id: "",
42+
otp: false,
43+
})
44+
setSkipLogin(true)
45+
})
46+
})
47+
return (
48+
<Switch fallback={props.children}>
49+
<Match when={!skipLogin() && loading()}>
50+
<FullScreenLoading />
51+
</Match>
52+
</Switch>
53+
)
54+
}
55+
56+
export { MustUser, UserOrGuest }

src/hooks/useDownload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ async function getSaveDir(rpc_url: string, rpc_secret: string) {
4040
export const useDownload = () => {
4141
const { rawLinks } = useSelectedLink()
4242
const t = useT()
43-
const { pathname } = useRouter()
43+
const { pathname, isShare } = useRouter()
4444
return {
4545
batchDownloadSelected: () => {
4646
const urls = rawLinks(true)
@@ -63,6 +63,7 @@ export const useDownload = () => {
6363
pathJoin(pathname(), pre),
6464
obj,
6565
"direct",
66+
isShare(),
6667
true,
6768
),
6869
name: obj.name,

src/hooks/useLink.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import { objStore, selectedObjs, State, me } from "~/store"
22
import { Obj } from "~/types"
3-
import { api, encodePath, pathDir, pathJoin, standardizePath } from "~/utils"
3+
import {
4+
base_path,
5+
api,
6+
encodePath,
7+
pathDir,
8+
pathJoin,
9+
standardizePath,
10+
} from "~/utils"
411
import { useRouter, useUtil } from "."
12+
import { cookieStorage } from "@solid-primitives/storage"
513

614
type URLType = "preview" | "direct" | "proxy"
715

@@ -10,33 +18,46 @@ export const getLinkByDirAndObj = (
1018
dir: string,
1119
obj: Obj,
1220
type: URLType = "direct",
21+
isShare: boolean,
1322
encodeAll?: boolean,
1423
) => {
15-
if (type !== "preview") {
16-
dir = pathJoin(me().base_path, dir)
24+
let path
25+
if (isShare) {
26+
path = standardizePath(obj.path, true)
27+
if (type === "preview") path = `/@s${path}`
28+
} else {
29+
if (type !== "preview") dir = pathJoin(me().base_path, dir)
30+
dir = standardizePath(dir, true)
31+
path = `${dir}/${obj.name}`
1732
}
18-
dir = standardizePath(dir, true)
19-
let path = `${dir}/${obj.name}`
33+
2034
path = encodePath(path, encodeAll)
2135
let host = api
22-
let prefix = type === "direct" ? "/d" : "/p"
36+
let prefix = isShare ? "/sd" : type === "direct" ? "/d" : "/p"
2337
if (type === "preview") {
2438
prefix = ""
25-
if (!api.startsWith(location.origin)) host = location.origin
39+
if (!api.startsWith(location.origin + base_path))
40+
host = location.origin + base_path
2641
}
2742
let ans = `${host}${prefix}${path}`
28-
if (type !== "preview" && obj.sign) {
43+
if (type !== "preview" && !isShare && obj.sign) {
2944
ans += `?sign=${obj.sign}`
3045
}
46+
if (type !== "preview" && isShare) {
47+
const pwd = cookieStorage.getItem("browser-password") || ""
48+
if (pwd) {
49+
ans += `?pwd=${pwd}`
50+
}
51+
}
3152
return ans
3253
}
3354

3455
// get download link by current state and pathname
3556
export const useLink = () => {
36-
const { pathname } = useRouter()
57+
const { pathname, isShare } = useRouter()
3758
const getLinkByObj = (obj: Obj, type?: URLType, encodeAll?: boolean) => {
3859
const dir = objStore.state !== State.File ? pathname() : pathDir(pathname())
39-
return getLinkByDirAndObj(dir, obj, type, encodeAll)
60+
return getLinkByDirAndObj(dir, obj, type, isShare(), encodeAll)
4061
}
4162
const rawLink = (obj: Obj, encodeAll?: boolean) => {
4263
return getLinkByObj(obj, "direct", encodeAll)

src/hooks/useRouter.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ const useRouter = () => {
1717
const pathname = createMemo(() => {
1818
return trimBase(decodeURIComponent(location.pathname))
1919
})
20+
const isShare = createMemo(() => {
21+
return pathname().startsWith("/@s")
22+
})
2023
return {
2124
to: (
2225
path: string,
@@ -45,6 +48,7 @@ const useRouter = () => {
4548
navigate(1)
4649
},
4750
pathname: pathname,
51+
isShare: isShare,
4852
search: location.search,
4953
searchParams: location.query,
5054
setSearchParams: (

src/lang/en/drivers.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,12 @@
729729
"url": "Url",
730730
"username": "Username"
731731
},
732+
"OpenListShare": {
733+
"forward_archive_requests": "Forward archive requests",
734+
"pwd": "Share code",
735+
"sid": "Share ID",
736+
"url": "Url"
737+
},
732738
"PikPak": {
733739
"captcha_token": "Captcha token",
734740
"device_id": "Device id",

src/lang/en/entry.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import manage from "./manage.json"
99
import metas from "./metas.json"
1010
import settings_other from "./settings_other.json"
1111
import settings from "./settings.json"
12+
import shares from "./shares.json"
1213
import storages from "./storages.json"
1314
import tasks from "./tasks.json"
1415
import users from "./users.json"
@@ -25,6 +26,7 @@ export const dict = {
2526
metas,
2627
settings_other,
2728
settings,
29+
shares,
2830
storages,
2931
tasks,
3032
users,

0 commit comments

Comments
 (0)