-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzip_funcs.go
75 lines (66 loc) · 1.71 KB
/
zip_funcs.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
// Copyright © 2019-2020 A Bunch Tell LLC. and contributors.
//
// This is free software: you can redistribute it and/or modify
// it under the terms of the Mozilla Public License, included
// in the LICENSE file in this source code package.
package wfimport
import (
"archive/zip"
"bufio"
"path/filepath"
"strings"
"github.com/writeas/go-writeas/v2"
)
// TopLevelZipFunc returns a pointer to a writeas.PostParams for any parseable
// zip.File that is not a directory. It does not traverse children.
//
// This is an example of a ZipFunc that can be used to filter files parse from
// a zip archive.
func TopLevelZipFunc(f *zip.File) (*writeas.PostParams, error) {
if !f.FileInfo().IsDir() {
return openAndParse(f)
}
return nil, nil
}
// TextFileZipFunc parses .txt files into PostParams
func TextFileZipFunc(f *zip.File) (*writeas.PostParams, error) {
if !f.FileInfo().IsDir() && filepath.Ext(f.FileHeader.Name) == ".txt" {
return openAndParse(f)
}
return nil, nil
}
func openAndParse(f *zip.File) (*writeas.PostParams, error) {
rc, err := f.Open()
if err != nil {
return nil, err
}
defer rc.Close()
r := bufio.NewReader(rc)
b := make([]byte, f.FileInfo().Size())
_, err = r.Read(b)
if err != nil {
return nil, err
}
p, err := fromBytes(b)
if err != nil {
return nil, err
}
p.Created = &f.Modified
p.ID, p.Slug, p.Collection = filenameParts(f.FileHeader.Name)
return p, nil
}
func filenameParts(filename string) (id, slug, coll string) {
filename = strings.TrimSuffix(filename, ".txt")
seg := strings.Split(filename, "/")
if len(seg) > 1 {
coll = seg[0]
filename = seg[1]
}
seg = strings.Split(filename, "_")
if len(seg) > 1 {
slug = seg[0]
filename = seg[1]
}
id = filename
return
}