]> saetta.ns0.it Git - zakegg_go/commitdiff
Allegati: aggiunta funzione Salva.
authorAndrea Zagli <azagli@libero.it>
Thu, 11 Aug 2022 08:57:58 +0000 (10:57 +0200)
committerAndrea Zagli <azagli@libero.it>
Thu, 11 Aug 2022 08:57:58 +0000 (10:57 +0200)
.gitignore
allegati.go [new file with mode: 0644]
go.mod

index b3d54b347fbdbf0fe93dd1a732e519013e8cb6f7..4a78d3de8ca3753dac1e0106a3142e0e7749dd01 100644 (file)
@@ -1 +1,2 @@
-*.~*~
\ No newline at end of file
+*.~*~
+go.sum
\ No newline at end of file
diff --git a/allegati.go b/allegati.go
new file mode 100644 (file)
index 0000000..f49d33c
--- /dev/null
@@ -0,0 +1,110 @@
+package zakegg
+
+import (
+       "fmt"
+       "os"
+       "time"
+       "path/filepath"
+       "mime/multipart"
+       "math/rand"
+       "io"
+       "database/sql"
+
+       "github.com/feiin/sqlstring"
+)
+
+func AllegatoSalva(allegati_dir string, file multipart.File, file_header *multipart.FileHeader, db *sql.DB, tabella_allegati string, id_gruppi int, campo_id_tabella string, id_tabella int, session_token string, tabella string, campo_tabella string) (new_id int, new_id_gruppi int){
+       if allegati_dir == "" {
+               fmt.Println("Directory allegati non impostata nella configurazione.")
+               return 0, 0
+       }
+
+       /* controllo che la directory esista */
+       _, err := os.Stat(allegati_dir)
+       if os.IsNotExist(err) {
+               /* la creo */
+               err = os.MkdirAll(allegati_dir, 0755)
+               if err != nil {
+                       fmt.Println("Impossibile creare la directory «%s» per gli allegati.", allegati_dir)
+               return 0, 0
+               }
+       }
+
+       ora := time.Now()
+       filename := filepath.Join(allegati_dir,
+               fmt.Sprintf("%d%02d%02d%02d-%s",
+                       ora.Year(),
+                       ora.Month(),
+                       ora.Day(),
+                       rand.Intn(10),
+                       file_header.Filename))
+
+       tmpfile, err := os.Create(filename)
+       defer tmpfile.Close()
+       if err != nil {
+               fmt.Println(err)
+               return 0, 0
+       }
+       _, err = io.Copy(tmpfile, file)
+       if err != nil {
+               fmt.Println(err)
+               return 0, 0
+       }
+
+       sql := ""
+       token := ""
+       if id_tabella == 0 {
+               token = session_token
+       }
+       if id_gruppi == 0 {
+               sql = fmt.Sprintf(`INSERT INTO %s
+                       (id, caricamento_data, sid, id_gruppi, %s, allegato_filename, tabella_nome, campo_nome)
+                       VALUES
+                       ((SELECT COALESCE(MAX(id), 0) + 1 FROM %s),
+                       NOW(),
+                       %s,
+                       (SELECT COALESCE(MAX(id_gruppi), 0) + 1 FROM %s),
+                       %d,
+                       %s, %s, %s)
+                       RETURNING id, id_gruppi`,
+                       tabella_allegati,
+                       campo_id_tabella,
+                       tabella_allegati,
+                       sqlstring.Escape(token),
+                       tabella_allegati,
+                       id_tabella,
+                       sqlstring.Escape(filename),
+                       sqlstring.Escape(tabella),
+                       sqlstring.Escape(campo_tabella));
+       } else {
+               sql = fmt.Sprintf(`INSERT INTO %s
+                       (id, caricamento_data, sid, id_gruppi, %s, allegato_filename, tabella_nome, campo_nome)
+                       VALUES
+                       ((SELECT COALESCE(MAX(id), 0) + 1 FROM %s),
+                       NOW(),
+                       %s, %d, %d, %s, %s, %s)
+                       RETURNING id, id_gruppi`,
+                       tabella_allegati,
+                       campo_id_tabella,
+                       tabella_allegati,
+                       sqlstring.Escape(token),
+                       id_gruppi,
+                       id_tabella,
+                       sqlstring.Escape(filename),
+                       sqlstring.Escape(tabella),
+                       sqlstring.Escape(campo_tabella));
+       }
+
+       rows, err := db.Query(sql)
+       if err != nil {
+               // handle err
+               fmt.Println(sql, err)
+               return 0, 0
+       }
+
+       rows.Next()
+       rows.Scan(&new_id, &new_id_gruppi)
+       rows.Close()
+
+       return new_id, new_id_gruppi
+}
diff --git a/go.mod b/go.mod
index f9a51bc67a9b6409b980131813a515e890be800e..4d462651e90e7f57c7abb54e8c2133fab8a12d46 100644 (file)
--- a/go.mod
+++ b/go.mod
@@ -1,3 +1,18 @@
 module saetta.ns0.it/git/zakegg_go.git
 
 go 1.18
+
+require (
+       github.com/feiin/sqlstring v0.3.0
+       github.com/go-playground/universal-translator v0.18.0
+       github.com/go-playground/validator/v10 v10.11.0
+       github.com/squeeze69/codicefiscale v1.0.2
+)
+
+require (
+       github.com/go-playground/locales v0.14.0 // indirect
+       github.com/leodido/go-urn v1.2.1 // indirect
+       golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
+       golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069 // indirect
+       golang.org/x/text v0.3.7 // indirect
+)