Skip to content

Commit

Permalink
Merge pull request #747 from illacloud/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
AruSeito authored Dec 30, 2022
2 parents 84216ef + 372beff commit bff8f05
Show file tree
Hide file tree
Showing 10 changed files with 8,224 additions and 8,033 deletions.
1 change: 1 addition & 0 deletions apps/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"unit": "npx jest"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.238.0",
"@emotion/react": "^11.10.0",
"@fontsource/fira-code": "^4.5.10",
"@illa-design/react": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useCallback, useEffect, useMemo, useState } from "react"
import { FC, useCallback, useMemo, useState } from "react"
import { useTranslation } from "react-i18next"
import { useDispatch, useSelector } from "react-redux"
import {
Expand Down Expand Up @@ -35,13 +35,6 @@ import {
IDEditorType,
QueryContentType,
} from "@/redux/currentApp/action/elasticSearchAction"
import {
S3Action,
S3ActionRequestType,
S3ActionTypeContent,
UploadContent,
UploadMultipleContent,
} from "@/redux/currentApp/action/s3Action"
import { SMPTAction } from "@/redux/currentApp/action/smtpAction"
import { getAppInfo } from "@/redux/currentApp/appInfo/appInfoSelector"
import { ActionTitleBarProps } from "./interface"
Expand All @@ -63,24 +56,6 @@ const getCanRunAction = (cachedAction: ActionItem<ActionContent> | null) => {
return [true, ""]
}
switch (cachedAction.actionType) {
case "s3":
const content = cachedAction.content as S3Action<S3ActionTypeContent>
let commandArgs
switch (content.commands) {
case S3ActionRequestType.UPLOAD:
commandArgs = content.commandArgs as UploadContent
return [
!isFileOversize(commandArgs.objectData),
i18n.t("editor.action.panel.error.max_file"),
]
case S3ActionRequestType.UPLOAD_MULTIPLE:
commandArgs = content.commandArgs as UploadMultipleContent
return [
!isFileOversize(commandArgs.objectDataList),
i18n.t("editor.action.panel.error.max_file"),
]
}
break
case "smtp":
const smtpContent = cachedAction.content as SMPTAction
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import {
getSelectedAction,
} from "@/redux/config/configSelector"
import { configActions } from "@/redux/config/configSlice"
import { ActionItem } from "@/redux/currentApp/action/actionState"
import {
BodyContent,
BodyType,
RestApiAction,
} from "@/redux/currentApp/action/restapiAction"
import { Resource } from "@/redux/resource/resourceState"
Expand All @@ -36,8 +38,12 @@ import {
export const RestApiPanel: FC = () => {
const { t } = useTranslation()

const cachedAction = useSelector(getCachedAction)!!
const selectedAction = useSelector(getSelectedAction)!!
const cachedAction = useSelector(getCachedAction) as ActionItem<
RestApiAction<BodyContent>
>
const selectedAction = useSelector(getSelectedAction) as ActionItem<
RestApiAction<BodyContent>
>

const content = cachedAction.content as RestApiAction<BodyContent>
const dispatch = useDispatch()
Expand All @@ -62,7 +68,7 @@ export const RestApiPanel: FC = () => {
maxW="160px"
options={["GET", "POST", "PUT", "PATCH", "DELETE"]}
onChange={(value) => {
let newBodyType = "none"
let newBodyType: BodyType = "none"
let newBody = null

if (value !== "GET") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const DownloadOnePart: FC<S3ActionPartProps> = (props) => {
S3Action<S3ActionTypeContent>
>
const commandArgs = props.commandArgs as DownloadOneContent

const handleValueChange = (value: string, name: string) => {
dispatch(
configActions.updateCachedAction({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from "@/redux/currentApp/action/s3Action"
import { DeleteMultiplePart } from "./DeleteMultiplePart"
import { DeleteOnePart } from "./DeleteOnePart"
import { DownloadOnePart } from "./DownlodOnePart"
import { DownloadOnePart } from "./DownloadOnePart"
import { ListAllPart } from "./ListAllPart"
import { ReadOnePart } from "./ReadOnePart"
import { UploadMultiplePart } from "./UploadMultiplePart"
Expand All @@ -40,9 +40,7 @@ export const S3Panel: FC = () => {
S3Action<S3ActionTypeContent>
>
const selectedAction = useSelector(getSelectedAction)!

const dispatch = useDispatch()

let content = cachedAction.content as S3Action<S3ActionTypeContent>

const renderInputBody = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import {
GetObjectCommand,
PutObjectCommand,
S3Client,
} from "@aws-sdk/client-s3"
import { S3ActionRequestType } from "@/redux/currentApp/action/s3Action"
import { Resource, ResourceContent } from "@/redux/resource/resourceState"
import { S3Resource } from "@/redux/resource/s3Resource"

export const ClientS3 = [
S3ActionRequestType.READ_ONE,
S3ActionRequestType.DOWNLOAD_ONE,
S3ActionRequestType.UPLOAD,
S3ActionRequestType.UPLOAD_MULTIPLE,
]

export const s3ClientInitialMap = new Map()

export const downloadActionResult = (
contentType: string,
fileName: string,
data: string,
) => {
const isBase64 = isValidBase64(data)
let href = ""
if (!isBase64) {
let blob = new Blob([data], { type: contentType })
href = URL.createObjectURL(blob)
}
const a = document.createElement("a")
a.download = fileName
a.style.display = "none"
a.href = isBase64 ? `data:${contentType};base64,${data}` : href
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
}

export const isValidBase64 = (str: string) => {
try {
window.atob(str)
return true
} catch (e) {
return false
}
}

export function base64ToArrayBuffer(base64: string) {
let binary_string = window.atob(base64)
let len = binary_string.length
let bytes = new Uint8Array(len)
for (let i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i)
}
return bytes
}

const fromCharCode = String.fromCharCode
export const encodeToBase64 = (uint8array: Uint8Array) => {
const output = []
for (let i = 0, length = uint8array.length; i < length; i++) {
output.push(fromCharCode(uint8array[i]))
}
return btoa(output.join(""))
}

export const initS3Client = (resourceList: Resource<ResourceContent>[]) => {
resourceList.forEach((resource) => {
if (resource.resourceType === "s3") {
const content = resource.content
const { accessKeyID, secretAccessKey, region, bucketName } =
content as S3Resource
const s3Client = new S3Client({
region,
credentials: {
accessKeyId: accessKeyID,
secretAccessKey,
},
})
const operations = {
getObject(key: string) {
return s3Client.send(
new GetObjectCommand({
Bucket: bucketName,
Key: key,
}),
)
},
putObject(key: string, body: string, contentType?: string) {
return s3Client.send(
new PutObjectCommand({
Bucket: bucketName,
Key: key,
Body: isValidBase64(body) ? base64ToArrayBuffer(body) : body,
...(contentType && {
ContentType: contentType,
}),
}),
)
},
}
s3ClientInitialMap.set(resource.resourceId, operations)
}
})
}
Loading

0 comments on commit bff8f05

Please sign in to comment.