Browse Source

download:content to url

tags/vopendata0.1.2
e 5 years ago
parent
commit
31e9d11d47
4 changed files with 42 additions and 1 deletions
  1. +4
    -0
      modules/storage/local.go
  2. +19
    -0
      modules/storage/minio.go
  3. +1
    -0
      modules/storage/storage.go
  4. +18
    -1
      routers/repo/attachment.go

+ 4
- 0
modules/storage/local.go View File

@@ -64,3 +64,7 @@ func (l *LocalStorage) Delete(path string) error {
p := filepath.Join(l.dir, path) p := filepath.Join(l.dir, path)
return os.Remove(p) return os.Remove(p)
} }

func (l *LocalStorage) PresignedGetURL(path string, fileName string) (string,error) {
return "",nil
}

+ 19
- 0
modules/storage/minio.go View File

@@ -6,8 +6,10 @@ package storage


import ( import (
"io" "io"
"net/url"
"path" "path"
"strings" "strings"
"time"


"github.com/minio/minio-go" "github.com/minio/minio-go"
) )
@@ -16,6 +18,8 @@ var (
_ ObjectStorage = &MinioStorage{} _ ObjectStorage = &MinioStorage{}
) )


const PRESIGNED_URL_EXPIRE_TIME = time.Hour * 24 * 7

// MinioStorage returns a minio bucket storage // MinioStorage returns a minio bucket storage
type MinioStorage struct { type MinioStorage struct {
client *minio.Client client *minio.Client
@@ -68,3 +72,18 @@ func (m *MinioStorage) Save(path string, r io.Reader) (int64, error) {
func (m *MinioStorage) Delete(path string) error { func (m *MinioStorage) Delete(path string) error {
return m.client.RemoveObject(m.bucket, m.buildMinioPath(path)) return m.client.RemoveObject(m.bucket, m.buildMinioPath(path))
} }

//Get Presigned URL
func (m *MinioStorage) PresignedGetURL(path string, fileName string) (string,error) {
// Set request parameters for content-disposition.
reqParams := make(url.Values)
reqParams.Set("response-content-disposition", "attachment; filename=\"" + fileName + "\"")

var preURL *url.URL
preURL,err := m.client.PresignedGetObject(m.bucket, m.buildMinioPath(path), PRESIGNED_URL_EXPIRE_TIME, reqParams)
if err != nil {
return "",err
}

return preURL.String(),nil
}

+ 1
- 0
modules/storage/storage.go View File

@@ -16,6 +16,7 @@ type ObjectStorage interface {
Save(path string, r io.Reader) (int64, error) Save(path string, r io.Reader) (int64, error)
Open(path string) (io.ReadCloser, error) Open(path string) (io.ReadCloser, error)
Delete(path string) error Delete(path string) error
PresignedGetURL(path string, fileName string) (string, error)
} }


// Copy copys a file from source ObjectStorage to dest ObjectStorage // Copy copys a file from source ObjectStorage to dest ObjectStorage


+ 18
- 1
routers/repo/attachment.go View File

@@ -5,6 +5,7 @@
package repo package repo


import ( import (
"code.gitea.io/gitea/modules/storage"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@@ -13,7 +14,6 @@ import (
"code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
"code.gitea.io/gitea/modules/upload" "code.gitea.io/gitea/modules/upload"
) )


@@ -124,6 +124,7 @@ func GetAttachment(ctx *context.Context) {
} }


//If we have matched and access to release or issue //If we have matched and access to release or issue
/*
fr, err := storage.Attachments.Open(attach.RelativePath()) fr, err := storage.Attachments.Open(attach.RelativePath())
if err != nil { if err != nil {
ctx.ServerError("Open", err) ctx.ServerError("Open", err)
@@ -131,13 +132,29 @@ func GetAttachment(ctx *context.Context) {
} }
defer fr.Close() defer fr.Close()


*/

url, err := storage.Attachments.PresignedGetURL(attach.RelativePath(), attach.Name)
if err != nil {
ctx.ServerError("PresignedGetURL", err)
return
}

if err := attach.IncreaseDownloadCount(); err != nil { if err := attach.IncreaseDownloadCount(); err != nil {
ctx.ServerError("Update", err) ctx.ServerError("Update", err)
return return
} }


ctx.JSON(200, map[string]string{
"name": attach.Name,
"url": url,
})

/*
if err = ServeData(ctx, attach.Name, fr); err != nil { if err = ServeData(ctx, attach.Name, fr); err != nil {
ctx.ServerError("ServeData", err) ctx.ServerError("ServeData", err)
return return
} }

*/
} }

Loading…
Cancel
Save