snapshot of https://github.com/cortezaproject/corteza.git (2024.9.x, 2026-09-18; blobs > 131072B omitted)
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
This commit is contained in:
227
server/compose/automation/attachment_handler.go
Normal file
227
server/compose/automation/attachment_handler.go
Normal file
@@ -0,0 +1,227 @@
|
||||
package automation
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/cortezaproject/corteza/server/compose/types"
|
||||
)
|
||||
|
||||
type (
|
||||
attachmentService interface {
|
||||
FindByID(ctx context.Context, namespaceID, attachmentID uint64) (*types.Attachment, error)
|
||||
CreateRecordAttachment(ctx context.Context, namespaceID uint64, name string, size int64, fh io.ReadSeeker, moduleID, recordID uint64, fieldName string) (att *types.Attachment, err error)
|
||||
DeleteByID(ctx context.Context, namespaceID uint64, attachmentID uint64) error
|
||||
OpenOriginal(att *types.Attachment) (io.ReadSeekCloser, error)
|
||||
OpenPreview(att *types.Attachment) (io.ReadSeekCloser, error)
|
||||
}
|
||||
|
||||
attachmentHandler struct {
|
||||
reg modulesHandlerRegistry
|
||||
svc attachmentService
|
||||
}
|
||||
|
||||
attachmentLookup interface {
|
||||
GetAttachment() (bool, uint64, *types.Attachment)
|
||||
}
|
||||
)
|
||||
|
||||
func AttachmentHandler(reg modulesHandlerRegistry, svc attachmentService) *attachmentHandler {
|
||||
h := &attachmentHandler{
|
||||
reg: reg,
|
||||
svc: svc,
|
||||
}
|
||||
|
||||
h.register()
|
||||
return h
|
||||
}
|
||||
|
||||
func (h attachmentHandler) lookup(ctx context.Context, args *attachmentLookupArgs) (results *attachmentLookupResults, err error) {
|
||||
results = &attachmentLookupResults{}
|
||||
results.Attachment, err = h.svc.FindByID(ctx, 0, args.Attachment)
|
||||
return
|
||||
}
|
||||
|
||||
func (h attachmentHandler) delete(ctx context.Context, args *attachmentDeleteArgs) error {
|
||||
return h.svc.DeleteByID(ctx, 0, args.Attachment)
|
||||
}
|
||||
|
||||
func (h attachmentHandler) openOriginal(ctx context.Context, args *attachmentOpenOriginalArgs) (*attachmentOpenOriginalResults, error) {
|
||||
att, err := lookupAttachment(ctx, h.svc, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &attachmentOpenOriginalResults{}
|
||||
r.Content, err = h.svc.OpenOriginal(att)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// @todo we need to call Close() when file is read (or at the end of the workflow)
|
||||
// some kind of workflow-cleanup facility is needed
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (h attachmentHandler) openPreview(ctx context.Context, args *attachmentOpenPreviewArgs) (*attachmentOpenPreviewResults, error) {
|
||||
att, err := lookupAttachment(ctx, h.svc, args)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r := &attachmentOpenPreviewResults{}
|
||||
r.Content, err = h.svc.OpenOriginal(att)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// @todo we need to call Close() when file is read (or at the end of the workflow)
|
||||
// some kind of workflow-cleanup facility is needed
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (h attachmentHandler) create(ctx context.Context, args *attachmentCreateArgs) (*attachmentCreateResults, error) {
|
||||
var (
|
||||
err error
|
||||
att *types.Attachment
|
||||
|
||||
fh io.ReadSeeker
|
||||
size int64
|
||||
)
|
||||
|
||||
switch {
|
||||
case len(args.contentBytes) > 0:
|
||||
size = int64(len(args.contentBytes))
|
||||
fh = bytes.NewReader(args.contentBytes)
|
||||
|
||||
case args.contentStream != nil:
|
||||
if rs, is := args.contentStream.(io.ReadSeeker); is {
|
||||
_, err = rs.Seek(0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
size, err = getReaderSize(rs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = rs.Seek(0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fh = rs
|
||||
} else {
|
||||
// In case we only got a reader...
|
||||
//
|
||||
// For future proofing, for handling larger attachment, we create a temp.
|
||||
// file which we then use as a reader
|
||||
|
||||
// Preparations
|
||||
tmpf, err := ioutil.TempFile("", "reader")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tmpf.Close()
|
||||
defer os.Remove(tmpf.Name())
|
||||
|
||||
// Writing content to file
|
||||
w := bufio.NewWriter(tmpf)
|
||||
r := args.contentStream
|
||||
buf := make([]byte, 1024)
|
||||
for {
|
||||
// read
|
||||
n, err := r.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return nil, err
|
||||
}
|
||||
if n == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
// on-the-fly size calculation
|
||||
size += int64(n)
|
||||
|
||||
// write
|
||||
if _, err := w.Write(buf[:n]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
if err = w.Flush(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, err = tmpf.Seek(0, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fh = tmpf
|
||||
}
|
||||
|
||||
default:
|
||||
fh = strings.NewReader(args.contentString)
|
||||
size = int64(len(args.contentString))
|
||||
}
|
||||
|
||||
switch {
|
||||
case args.Resource != nil:
|
||||
att, err = h.svc.CreateRecordAttachment(
|
||||
ctx,
|
||||
args.Resource.NamespaceID,
|
||||
args.Name,
|
||||
size,
|
||||
fh,
|
||||
args.Resource.ModuleID,
|
||||
args.Resource.ID,
|
||||
args.FieldName,
|
||||
)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown resource")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &attachmentCreateResults{att}, nil
|
||||
}
|
||||
|
||||
func lookupAttachment(ctx context.Context, svc attachmentService, args attachmentLookup) (*types.Attachment, error) {
|
||||
_, ID, attachment := args.GetAttachment()
|
||||
|
||||
switch {
|
||||
case attachment != nil:
|
||||
return attachment, nil
|
||||
case ID > 0:
|
||||
return svc.FindByID(ctx, 0, ID)
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("empty attachment lookup params")
|
||||
}
|
||||
|
||||
func getReaderSize(r io.Reader) (size int64, err error) {
|
||||
buf := make([]byte, 1024)
|
||||
var n int
|
||||
for {
|
||||
// read a chunk
|
||||
n, err = r.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return 0, err
|
||||
}
|
||||
if n == 0 {
|
||||
break
|
||||
}
|
||||
size += int64(n)
|
||||
}
|
||||
|
||||
return size, nil
|
||||
}
|
||||
Reference in New Issue
Block a user