2022-07-28 15:00:43 +00:00
|
|
|
package templategen
|
|
|
|
|
|
|
|
import (
|
2022-09-15 13:53:55 +00:00
|
|
|
"fmt"
|
|
|
|
docTemp "github.com/opencontrol/doc-template"
|
2022-07-28 15:00:43 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2022-09-15 13:53:55 +00:00
|
|
|
"time"
|
2022-07-28 15:00:43 +00:00
|
|
|
)
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
const (
|
2022-11-24 19:37:47 +00:00
|
|
|
PathExamples = "./static/examples/docx"
|
2022-09-15 13:53:55 +00:00
|
|
|
TempDownloaded = "./tmp/downloaded"
|
|
|
|
TempGenerated = "./tmp/generated"
|
|
|
|
)
|
|
|
|
|
2022-11-12 10:58:54 +00:00
|
|
|
func GenerateDocName(name string) string {
|
2022-11-21 19:15:59 +00:00
|
|
|
return fmt.Sprintf("%v_%v.docx", name, time.Now().UnixNano())
|
2022-09-15 13:53:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateFile(filename string, data interface{}) error {
|
|
|
|
dc, err := docTemp.GetTemplate(TempDownloaded + "/" + filename)
|
2022-11-24 19:37:47 +00:00
|
|
|
|
|
|
|
dc.Template.Option("missingkey=zero")
|
|
|
|
|
2022-09-15 13:53:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dc.Parse()
|
|
|
|
|
|
|
|
return dc.Execute(TempGenerated+"/"+filename, data)
|
|
|
|
}
|
|
|
|
|
2022-07-28 15:00:43 +00:00
|
|
|
// DownloadDocument - загружает документ в filepath из url
|
|
|
|
func DownloadDocument(filepath, url string) error {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// Create the file
|
|
|
|
out, err := os.Create(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
// Write the body to file
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteDocument(filepath string) error {
|
|
|
|
return os.Remove(filepath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ShareDocument(filepath string) (error, string) {
|
|
|
|
return nil, ""
|
|
|
|
}
|