add domain parameter to createS3Storage and update related components

This commit is contained in:
nyne
2025-05-16 14:57:54 +08:00
parent 1b31af411d
commit a827b67c41
4 changed files with 103 additions and 77 deletions

View File

@@ -448,8 +448,14 @@ class Network {
}
}
async createS3Storage(name: string, endPoint: string, accessKeyID: string,
secretAccessKey: string, bucketName: string, maxSizeInMB: number): Promise<Response<any>> {
async createS3Storage(
name: string,
endPoint: string,
accessKeyID: string,
secretAccessKey: string,
bucketName: string,
maxSizeInMB: number,
domain: string): Promise<Response<any>> {
try {
const response = await axios.post(`${this.apiBaseUrl}/storage/s3`, {
name,
@@ -457,7 +463,8 @@ class Network {
accessKeyID,
secretAccessKey,
bucketName,
maxSizeInMB
maxSizeInMB,
domain
});
return response.data;
} catch (e: any) {

View File

@@ -163,6 +163,7 @@ function NewStorageDialog({onAdded}: { onAdded: () => void }) {
secretAccessKey: "",
bucketName: "",
maxSizeInMB: 0,
domain: "",
});
const [isLoading, setIsLoading] = useState(false);
@@ -189,7 +190,7 @@ function NewStorageDialog({onAdded}: { onAdded: () => void }) {
setIsLoading(false);
return;
}
response = await network.createS3Storage(params.name, params.endPoint, params.accessKeyID, params.secretAccessKey, params.bucketName, params.maxSizeInMB);
response = await network.createS3Storage(params.name, params.endPoint, params.accessKeyID, params.secretAccessKey, params.bucketName, params.maxSizeInMB, params.domain);
}
if (response!.success) {
@@ -316,6 +317,15 @@ function NewStorageDialog({onAdded}: { onAdded: () => void }) {
})
}} />
</label>
<label className="input w-full my-2">
{t("Domain")}
<input type="text" placeholder={t("Optional")} className="w-full" value={params.domain} onChange={(e) => {
setParams({
...params,
domain: e.target.value,
})
}} />
</label>
<label className="input w-full my-2">
{t("Max Size (MB)")}
<input

View File

@@ -14,6 +14,7 @@ type CreateS3StorageParams struct {
EndPoint string `json:"endPoint"`
AccessKeyID string `json:"accessKeyID"`
SecretAccessKey string `json:"secretAccessKey"`
Domain string `json:"domain"`
BucketName string `json:"bucketName"`
MaxSizeInMB uint `json:"maxSizeInMB"`
}
@@ -32,6 +33,7 @@ func CreateS3Storage(uid uint, params CreateS3StorageParams) error {
AccessKeyID: params.AccessKeyID,
SecretAccessKey: params.SecretAccessKey,
BucketName: params.BucketName,
Domain: params.Domain,
}
s := model.Storage{
Name: params.Name,

View File

@@ -5,12 +5,13 @@ import (
"encoding/json"
"errors"
"fmt"
"net/url"
"time"
"github.com/gofiber/fiber/v3/log"
"github.com/google/uuid"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"net/url"
"time"
)
type S3Storage struct {
@@ -18,6 +19,7 @@ type S3Storage struct {
AccessKeyID string
SecretAccessKey string
BucketName string
Domain string
}
func (s *S3Storage) Upload(filePath string, fileName string) (string, error) {
@@ -43,6 +45,10 @@ func (s *S3Storage) Upload(filePath string, fileName string) (string, error) {
}
func (s *S3Storage) Download(storageKey string, fileName string) (string, error) {
if s.Domain != "" {
return s.Domain + "/" + storageKey, nil
}
minioClient, err := minio.New(s.EndPoint, &minio.Options{
Creds: credentials.NewStaticV4(s.AccessKeyID, s.SecretAccessKey, ""),
Secure: true,
@@ -80,6 +86,7 @@ func (s *S3Storage) FromString(config string) error {
s.AccessKeyID = s3Config.AccessKeyID
s.SecretAccessKey = s3Config.SecretAccessKey
s.BucketName = s3Config.BucketName
s.Domain = s3Config.Domain
if s.EndPoint == "" || s.AccessKeyID == "" || s.SecretAccessKey == "" || s.BucketName == "" {
return errors.New("invalid S3 configuration")
}