File: mapper.go

package info (click to toggle)
golang-github-viant-toolbox 0.33.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,280 kB
  • sloc: makefile: 16
file content (136 lines) | stat: -rw-r--r-- 3,469 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package storage

import (
	"fmt"
	"github.com/viant/toolbox"
	"io"
	"io/ioutil"
	"os"
	"path"
	"strings"
)

type StorageMapping struct {
	SourceURL        string
	SourceCredential string
	DestinationURI   string
	TargetFile       string
	TargetPackage    string
	UseTextFormat    bool
}

var binaryFormats = map[string]bool{
	".png":  true,
	".jpeg": true,
	".ico":  true,
	".jpg":  true,
}

//GenerateStorageCode create a *.go files with statically scanned content from source URL.
func GenerateStorageCode(mappings ...*StorageMapping) error {
	destinationService := NewMemoryService()
	for _, mapping := range mappings {
		sourceService, err := NewServiceForURL(mapping.SourceURL, mapping.SourceCredential)
		if err != nil {
			return err
		}
		handler, writer, err := NewStorageMapperHandler(mapping.TargetFile, mapping.TargetPackage, mapping.UseTextFormat, binaryFormats)
		if err != nil {
			return err
		}
		defer writer.Close()
		destinationURL := "mem://" + mapping.DestinationURI
		err = copyStorageContent(sourceService, mapping.SourceURL, destinationService, destinationURL, nil, "", handler)
		if err != nil {
			return err
		}
	}
	return nil
}

//NewStorageMapperHandler creates a template handler for generating go file that write static content into memory service.
func NewStorageMapperHandler(filename, pkg string, useTextFormat bool, binaryFormat map[string]bool) (CopyHandler, io.WriteCloser, error) {
	_ = toolbox.RemoveFileIfExist(filename)
	writer, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644)
	if err != nil {
		return nil, nil, err
	}
	template := &templateWriter{writer}
	_ = template.Init(pkg)
	return func(sourceObject Object, source io.Reader, destinationService Service, destinationURL string) error {

		_, name := toolbox.URLSplit(destinationURL)
		if strings.HasPrefix(name, ".") {
			//skip hidden files
			return nil
		}
		content, err := ioutil.ReadAll(source)
		if err != nil {
			return err
		}
		isText := useTextFormat
		ext := path.Ext(destinationURL)
		if binaryFormat[ext] {
			isText = false
		}

		template.WriteStorageContent(destinationURL, content, isText)
		return nil
	}, template, nil
}

type templateWriter struct {
	io.WriteCloser
}

func (t *templateWriter) Init(pkg string) error {
	var begin = `package %v

import (
	"bytes"
	"github.com/viant/toolbox/storage"
	"log"
)

func init() {
	var memStorage = storage.NewMemoryService();
`
	_, err := t.Write([]byte(fmt.Sprintf(begin, pkg)))
	return err
}

func (t *templateWriter) WriteStorageContent(URL string, content []byte, useText bool) error {
	if useText {
		text := string(content)
		var count = strings.Count(text, "`")
		if count > 0 {
			text = strings.Replace(text, "`", "`+\"`\"+`", count)
			content = []byte(text)
		}
	}
	var contentReader = fmt.Sprintf("bytes.NewReader([]byte(`%s`))", content)
	if !toolbox.IsASCIIText(contentReader) && !useText {
		var byteArray = make([]string, 0)
		for _, b := range content {
			byteArray = append(byteArray, fmt.Sprintf("%d", b))
		}
		contentReader = fmt.Sprintf("bytes.NewReader([]byte{%v})", strings.Join(byteArray, ","))
	}
	var payload = `	{
		err := memStorage.Upload("%v", %v)
		if err != nil {
			log.Printf("failed to upload: %v %v", err)
		}
	}
`
	payload = fmt.Sprintf(payload, URL, contentReader, URL, "%v")
	_, err := t.Write([]byte(payload))
	return err
}

func (t *templateWriter) Close() error {
	var end = "}\n"
	_, err := t.Write([]byte(end))
	t.WriteCloser.Close()
	return err
}