fix: s3 browser preview (#580)

pull/580/merge
withchao 1 year ago committed by GitHub
parent cb59f05734
commit b95420ef8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -74,7 +74,7 @@ api:
object:
enable: "minio" #使用minio
apiURL: "http://127.0.0.1:10002/object/"
apiURL: "http://127.0.0.1:10002/object/" #地址需要app能访问到
minio:
bucket: "openim" #不建议修改
endpoint: "http://127.0.0.1:10005" #minio对外服务的ip和端口app要能访问此ip和端口

@ -77,9 +77,7 @@ func (s *s3Database) AccessURL(ctx context.Context, name string, expire time.Dur
}
opt := &s3.AccessURLOption{
ContentType: obj.ContentType,
}
if filename := filepath.Base(obj.Name); filename != "" {
opt.ContentDisposition = `attachment; filename=` + filename
Filename: filepath.Base(obj.Name),
}
expireTime := time.Now().Add(expire)
rawURL, err := s.s3.AccessURL(ctx, obj.Key, expire, opt)

@ -248,21 +248,27 @@ func (c *Cos) ListUploadedParts(ctx context.Context, uploadID string, name strin
}
func (c *Cos) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
//reqParams := make(url.Values)
//if opt != nil {
// if opt.ContentType != "" {
// reqParams.Set("Content-Type", opt.ContentType)
// }
// if opt.ContentDisposition != "" {
// reqParams.Set("Content-Disposition", opt.ContentDisposition)
// }
//}
var option *cos.PresignedURLOptions
if opt != nil {
query := make(url.Values)
if opt.ContentType != "" {
query.Set("response-content-type", opt.ContentType)
}
if opt.Filename != "" {
query.Set("response-content-disposition", `attachment; filename="`+opt.Filename+`"`)
}
if len(query) > 0 {
option = &cos.PresignedURLOptions{
Query: &query,
}
}
}
if expire <= 0 {
expire = time.Hour * 24 * 365 * 99 // 99 years
} else if expire < time.Second {
expire = time.Second
}
rawURL, err := c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, nil)
rawURL, err := c.client.Object.GetPresignedURL(ctx, http.MethodGet, name, c.credential.SecretID, c.credential.SecretKey, expire, option)
if err != nil {
return "", err
}

@ -253,21 +253,21 @@ func (m *Minio) ListUploadedParts(ctx context.Context, uploadID string, name str
}
func (m *Minio) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
//reqParams := make(url.Values)
//if opt != nil {
// if opt.ContentType != "" {
// reqParams.Set("Content-Type", opt.ContentType)
// }
// if opt.ContentDisposition != "" {
// reqParams.Set("Content-Disposition", opt.ContentDisposition)
// }
//}
reqParams := make(url.Values)
if opt != nil {
if opt.ContentType != "" {
reqParams.Set("response-content-type", opt.ContentType)
}
if opt.Filename != "" {
reqParams.Set("response-content-disposition", `attachment; filename="`+opt.Filename+`"`)
}
}
if expire <= 0 {
expire = time.Hour * 24 * 365 * 99 // 99 years
} else if expire < time.Second {
expire = time.Second
}
u, err := m.core.Client.PresignedGetObject(ctx, m.bucket, name, expire, nil)
u, err := m.core.Client.PresignedGetObject(ctx, m.bucket, name, expire, reqParams)
if err != nil {
return "", err
}

@ -261,15 +261,15 @@ func (o *OSS) ListUploadedParts(ctx context.Context, uploadID string, name strin
}
func (o *OSS) AccessURL(ctx context.Context, name string, expire time.Duration, opt *s3.AccessURLOption) (string, error) {
//var opts []oss.Option
//if opt != nil {
// if opt.ContentType != "" {
// opts = append(opts, oss.ContentType(opt.ContentType))
// }
// if opt.ContentDisposition != "" {
// opts = append(opts, oss.ContentDisposition(opt.ContentDisposition))
// }
//}
var opts []oss.Option
if opt != nil {
if opt.ContentType != "" {
opts = append(opts, oss.ResponseContentType(opt.ContentType))
}
if opt.Filename != "" {
opts = append(opts, oss.ResponseContentDisposition(`attachment; filename="`+opt.Filename+`"`))
}
}
if expire <= 0 {
expire = time.Hour * 24 * 365 * 99 // 99 years
} else if expire < time.Second {

@ -117,8 +117,8 @@ type ListUploadedPartsResult struct {
}
type AccessURLOption struct {
ContentType string `json:"contentType"`
ContentDisposition string `json:"contentDisposition"`
ContentType string `json:"contentType"`
Filename string `json:"filename"`
}
type Interface interface {

Loading…
Cancel
Save