File: file.go

package info (click to toggle)
dmarc-cat 0.9.2-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 164 kB
  • sloc: xml: 104; makefile: 27
file content (61 lines) | stat: -rw-r--r-- 1,184 bytes parent folder | download
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
package main

import (
	"encoding/xml"
	"fmt"
	"path/filepath"
	"regexp"

	"github.com/keltia/archive"
	"github.com/pkg/errors"
)

const (
	reFileName = `^([\w\.]+)!([\w\.]+)!([\d]+)!([\d]+)(\.xml)*(\.(gz|zip))*$`
)

func checkFilename(file string) (ok bool) {
	base := filepath.Base(file)
	re := regexp.MustCompile(reFileName)

	return re.MatchString(base)
}

// HandleSingleFile creates a tempdir and dispatch csv/zip files to handler.
func HandleSingleFile(ctx *Context, file string) (string, error) {
	var myfile string

	debug("file=%s", file)
	if !checkFilename(file) {
		return "", fmt.Errorf("bad filename")
	}

	// We want the full path
	myfile, err := filepath.Abs(file)
	if err != nil {
		return "", errors.Wrapf(err, "Abs(%s)", file)
	}

	a, err := archive.New(myfile)
	if err != nil {
		return "", errors.Wrap(err, "newarchive")
	}

	body, err := a.Extract(".xml")
	if err != nil {
		return "", errors.Wrap(err, "extract")
	}

	debug("xml=%#v", body)
	verbose("Analyzing %s", myfile)

	var report Feedback

	if err := xml.Unmarshal(body, &report); err != nil {
		return "", errors.Wrap(err, "unmarshall")
	}

	debug("report=%v\n", report)

	return Analyze(ctx, report)
}