Skip to content

Commit

Permalink
add determine base share url
Browse files Browse the repository at this point in the history
  • Loading branch information
lihsai0 committed May 23, 2024
1 parent f835f54 commit b318a37
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 9 deletions.
6 changes: 0 additions & 6 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,6 @@ ipcMain.on("UploaderManager", (event, message) => {
{
cwd: root,
silent: false,
execArgv: [
"--inspect=9222"
]
},
);
forkedWorkers.set(processName, uploaderProcess);
Expand Down Expand Up @@ -297,9 +294,6 @@ ipcMain.on("DownloaderManager", (event, message) => {
{
cwd: root,
silent: false,
execArgv: [
"--inspect=9223"
]
},
);
forkedWorkers.set(processName, downloaderProcess);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {ALPHANUMERIC} from "@common/const/str";
import {Alphanumeric} from "@renderer/const/patterns";

import {useI18n} from "@renderer/modules/i18n";
import {useAuth} from "@renderer/modules/auth";
import {EndpointType, useAuth} from "@renderer/modules/auth";
import {createShare, FileItem} from "@renderer/modules/qiniu-client";
import * as DefaultDict from "@renderer/modules/default-dict";

Expand Down Expand Up @@ -95,6 +95,17 @@ const CreateDirShareLink: React.FC<ModalProps & CreateDirShareLinkProps> = (prop
return;
}

const apiUrls: string[] = [];
const customShareUrl = currentUser.endpointType === EndpointType.Private
? DefaultDict.get("BASE_SHARE_URL")
: undefined;
if (customShareUrl) {
const customApiHost = new URL(customShareUrl).searchParams.get("apiHost");
if (customApiHost) {
apiUrls.push(customApiHost);
}
}

const p = createShare(
{
bucket: memoBucketName,
Expand All @@ -104,15 +115,15 @@ const CreateDirShareLink: React.FC<ModalProps & CreateDirShareLinkProps> = (prop
permission: "READONLY",
},
{
apiUrls: [],
apiUrls,
accessKey: currentUser.accessKey,
accessSecret: currentUser.accessSecret,
endpointType: currentUser.endpointType,
},
);

p.then(shareInfo => {
const baseShareUrl = DefaultDict.get("BASE_SHARE_URL") || `${DEFAULT_PORTAL_URL}/kodo-shares/verify`;
const baseShareUrl = customShareUrl || `${DEFAULT_PORTAL_URL}/kodo-shares/verify`;
const shareURL = new URL(baseShareUrl);
shareURL.searchParams.set("id", shareInfo.id);
shareURL.searchParams.set("token", shareInfo.token);
Expand Down
1 change: 1 addition & 0 deletions src/renderer/modules/i18n/lang/dict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export default interface Dictionary {
label: string,
feedback: {
invalidFormat: string,
invalidPrivateFormat: string,
},
},
extractCode: {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/modules/i18n/lang/en-us.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const dict: Dictionary = {
label: "Share Link:",
feedback: {
invalidFormat: "Invalid share link format",
invalidPrivateFormat: "Invalid share link format. Please configure share base link if it's a share link for private cloud.",
},
},
extractCode: {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/modules/i18n/lang/ja-jp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const dict: Dictionary = {
label: "共有リンク:",
feedback: {
invalidFormat: "リンクの形式が正しくありません",
invalidPrivateFormat: "リンクの形式が正しくありません。非公開クラウドユーザーの場合は、プライベートクラウド共有アドレスを設定してください",
},
},
extractCode: {
Expand Down
1 change: 1 addition & 0 deletions src/renderer/modules/i18n/lang/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const dict: Dictionary = {
label: "分享链接:",
feedback: {
invalidFormat: "链接格式不正确",
invalidPrivateFormat: "链接格式不正确,非公有云用户请配置私有云分享地址",
},
},
extractCode: {
Expand Down
34 changes: 34 additions & 0 deletions src/renderer/pages/sign-in/sign-in-share-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import {useNavigate} from "react-router-dom";
import {toast} from "react-hot-toast";
import {SubmitHandler, useForm} from "react-hook-form";
import {Button, Col, Form, Row, Spinner} from "react-bootstrap";
import {DEFAULT_PORTAL_URL} from "kodo-s3-adapter-sdk/dist/region";

import {translate} from "@renderer/modules/i18n";
import {useAuth} from "@renderer/modules/auth";
import * as AuditLog from "@renderer/modules/audit-log";
import * as DefaultDict from "@renderer/modules/default-dict";

import RoutePath from "@renderer/pages/route-path";

Expand All @@ -21,6 +23,24 @@ interface ParseShareURLResult {
shareToken: string,
}

function isPublicShareURL(url: string): boolean {
const shareURL = new URL(url.trim());
const defaultPortalURL = new URL(DEFAULT_PORTAL_URL);
return shareURL.host === defaultPortalURL.host
}

function getCustomApiHost(): string | undefined {
const customShareUrl = DefaultDict.get("BASE_SHARE_URL");
if (!customShareUrl) {
return;
}
const customApiHost = new URL(customShareUrl).searchParams.get("apiHost");
if (!customApiHost) {
return;
}
return customApiHost;
}

function parseShareURL(url: string): ParseShareURLResult | null {
const shareURL = new URL(url.trim());

Expand Down Expand Up @@ -69,6 +89,20 @@ const SignInShareForm: React.FC<SignInShareFormProps> = ({
toast.error(translate("signIn.formShareLink.shareLink.feedback.invalidFormat"));
return;
}

// try to determine the api host from private cloud share link
const customDefaultApiHost = getCustomApiHost();
if (
!isPublicShareURL(data.shareLink) &&
!parsedShareURL.apiHost &&
!customDefaultApiHost
) {
toast.error(translate("signIn.formShareLink.shareLink.feedback.invalidPrivateFormat"));
return;
}
parsedShareURL.apiHost = parsedShareURL.apiHost || customDefaultApiHost;

// send sign in request
const p = signInWithShareLink({
apiHost: parsedShareURL.apiHost,
shareId: parsedShareURL.shareId,
Expand Down

0 comments on commit b318a37

Please sign in to comment.