mirror of
https://git.freecumextremist.com/grumbulon/pomme.git
synced 2024-11-22 10:03:45 +00:00
amended the Zone schema, fixed file upload size too small issue, refactoring, and zone file parsing works now
This commit is contained in:
parent
e363c83009
commit
da5bd54b3e
5 changed files with 67 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -23,4 +23,5 @@ go.work
|
|||
|
||||
pomme
|
||||
test.db
|
||||
test.sqlite
|
||||
.dccache
|
|
@ -43,6 +43,7 @@ func API() http.Handler {
|
|||
|
||||
api.Use(jwtauth.Authenticator)
|
||||
api.With(SetDBMiddleware).Post("/upload", RecieveFile)
|
||||
api.With(SetDBMiddleware).Post("/parse", ZoneFiles)
|
||||
})
|
||||
|
||||
// Open routes
|
||||
|
|
|
@ -6,8 +6,10 @@ import (
|
|||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"git.freecumextremist.com/grumbulon/pomme/internal"
|
||||
"git.freecumextremist.com/grumbulon/pomme/internal/util"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"github.com/miekg/dns"
|
||||
|
@ -24,8 +26,9 @@ type ZoneRequest struct {
|
|||
// Zone struct represents a zonefile.
|
||||
type Zone struct {
|
||||
gorm.Model
|
||||
FileName string `json:"name"`
|
||||
Body string `json:"body"`
|
||||
FileName string `json:"name"`
|
||||
RawFileName string `json:"rawname"`
|
||||
Body string `json:"body,omitempty"`
|
||||
}
|
||||
|
||||
func RecieveFile(w http.ResponseWriter, r *http.Request) {
|
||||
|
@ -33,7 +36,7 @@ func RecieveFile(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
var buf bytes.Buffer
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, .5*1024*1024) // approx 500 kb max upload
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 1*1024*1024) // approx 500 kb max upload
|
||||
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
|
@ -58,12 +61,41 @@ func RecieveFile(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// ugly
|
||||
zoneReq := newZoneRequest(fmt.Sprintf("tmpfile-%s-%s", name[0], claims["username"].(string)),
|
||||
claims["username"].(string),
|
||||
)
|
||||
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
|
||||
if !ok {
|
||||
http.Error(w, "internal server error", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
db.Create(
|
||||
&ZoneRequest{User: claims["username"].(string),
|
||||
Zone: &Zone{
|
||||
FileName: fmt.Sprintf("tmpfile-%s-%s", name[0], claims["username"].(string)),
|
||||
RawFileName: name[0],
|
||||
}})
|
||||
|
||||
buf.Reset()
|
||||
}
|
||||
|
||||
func ZoneFiles(w http.ResponseWriter, r *http.Request) {
|
||||
var result internal.ZoneRequest
|
||||
_, claims, _ := jwtauth.FromContext(r.Context())
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
http.Error(w, "Unable to parse request", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
filename := r.Form.Get("filename")
|
||||
|
||||
if filename == "" {
|
||||
http.Error(w, "No filename parsed", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
db, ok := r.Context().Value(keyPrincipalContextID).(*gorm.DB)
|
||||
if !ok {
|
||||
|
@ -72,20 +104,36 @@ func RecieveFile(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
db.Create(zoneReq)
|
||||
|
||||
if err = zoneReq.Parse(); err != nil {
|
||||
http.Error(w, fmt.Sprintf("unable to parse zonefile: %v", err), http.StatusInternalServerError)
|
||||
db.Where("raw_file_name = ?", filename).First(&result)
|
||||
log.Println(result.RawFileName)
|
||||
if result.RawFileName == "" {
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
zoneFile := newZoneRequest(result.RawFileName, claims["username"].(string))
|
||||
log.Println(zoneFile.FileName)
|
||||
if err := zoneFile.Parse(); err != nil {
|
||||
http.Error(w, "Unable to parse zonefile", http.StatusInternalServerError)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func newZoneRequest(filename string, user string) *ZoneRequest {
|
||||
dat, err := os.ReadFile(fmt.Sprintf("/tmp/tmpfile-%s-%s", filename, user))
|
||||
if err != nil {
|
||||
return &ZoneRequest{}
|
||||
}
|
||||
|
||||
return &ZoneRequest{
|
||||
User: user,
|
||||
Zone: &Zone{
|
||||
FileName: filename,
|
||||
FileName: fmt.Sprintf("tmpfile-%s-%s", filename, user),
|
||||
RawFileName: filename,
|
||||
Body: string(dat),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +141,7 @@ func newZoneRequest(filename string, user string) *ZoneRequest {
|
|||
// Parse will be used to parse zonefiles.
|
||||
func (zone *ZoneRequest) Parse() error {
|
||||
zp := dns.NewZoneParser(strings.NewReader(zone.Body), "", "")
|
||||
|
||||
log.Println(zone.Body)
|
||||
for rr, ok := zp.Next(); ok; rr, ok = zp.Next() {
|
||||
log.Println(rr)
|
||||
}
|
||||
|
|
|
@ -35,8 +35,9 @@ type ZoneRequest struct {
|
|||
// Zone struct represents a zonefile in the database.
|
||||
type Zone struct {
|
||||
gorm.Model
|
||||
FileName string `json:"name"`
|
||||
Body string `json:"body"`
|
||||
FileName string `json:"name"`
|
||||
RawFileName string `json:"rawfilename"`
|
||||
Body string `json:"body"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
|
|
|
@ -8,7 +8,7 @@ import (
|
|||
|
||||
// InitDb is the init function for the database.
|
||||
func InitDb() *gorm.DB {
|
||||
db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
|
||||
db, err := gorm.Open(sqlite.Open("test.sqlite"), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue