-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdocument_handler.go
89 lines (75 loc) · 1.76 KB
/
document_handler.go
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
package proxy
import (
"encoding/xml"
"github.com/goamz/goamz/aws"
"github.com/goamz/goamz/s3"
"path/filepath"
"strings"
)
type SolrDocument struct {
Core string
Id string
Name string
content []byte
}
type Document struct {
Field []DocField `xml:"field"`
}
type Add struct {
Doc Document `xml:"doc"`
content []byte
}
type DocField struct {
Name string `xml:"name,attr"`
Value string `xml:",chardata"`
}
func ParseXMLDocument(content []byte) (*Add, error) {
a := new(Add)
a.content = content
var err error
if len(content) > 0 {
err = xml.Unmarshal(content, a)
}
return a, err
}
func (a *Add) getFieldValue(fieldName string) string {
for _, field := range a.Doc.Field {
if field.Name == fieldName {
return field.Value
}
}
return ""
}
func (d *Add) GetSolrDocument(CoreName string) *SolrDocument {
name, id := d.GetNameAndId()
return &SolrDocument{
Core: CoreName,
Id: id,
Name: name,
content: d.content,
}
}
func (d *Add) GetNameAndId() (string, string) {
value := d.getFieldValue("id")
splits := strings.Split(value, " ")
if len(splits) < 2 {
return "", ""
}
return splits[0], splits[1]
}
func (d *SolrDocument) Cache(awsConfig *AWSConfig) error {
if d.Name == "" {
// TODO: how should this case be handled?
return nil
}
documentName := d.GetDocumentName(awsConfig)
auth, _ := aws.EnvAuth()
region := aws.Region{Name: awsConfig.RegionName, S3Endpoint: awsConfig.S3Endpoint}
svc := s3.New(auth, region)
bucketName := awsConfig.BucketName
bucket := svc.Bucket(bucketName)
return bucket.Put(documentName, d.content, "text/xml", s3.AuthenticatedRead, s3.Options{})
}
func (d *SolrDocument) GetDocumentName(awsConfig *AWSConfig) string {
return filepath.Join(awsConfig.BucketPrefix, d.Core, d.Name, d.Id)
}