-
Notifications
You must be signed in to change notification settings - Fork 0
/
databio.go
113 lines (100 loc) · 3.33 KB
/
databio.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Databio - automated data parsing, integration, and identifier translation service.
// Copyright (C) 2019 Jeremy Jay <jeremy@pbnjay.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package databio
import (
"encoding/json"
"os"
"path/filepath"
)
var (
// UploadDirectory is the path to place uploaded files.
UploadDirectory, _ = filepath.Abs("./uploads")
// ResultDirectory is the path to place result metadata files.
ResultDirectory, _ = filepath.Abs("./results")
// DownloadDirectory is the path to place downloadable files.
DownloadDirectory, _ = filepath.Abs("./downloads")
// SourceMapsDirectory is the path to source mapping files.
SourceMapsDirectory, _ = filepath.Abs("./sources")
)
// CheckDirectories creates necessary data directories for processing uploads.
func CheckDirectories() error {
err := os.MkdirAll(UploadDirectory, 0777)
if err != nil {
return err
}
err = os.MkdirAll(ResultDirectory, 0755)
if err != nil {
return err
}
err = os.MkdirAll(DownloadDirectory, 0755)
if err != nil {
return err
}
_, err = os.Stat(SourceMapsDirectory)
return err
}
// GetUploadPath returns the full path to an uploaded file.
func GetUploadPath(filename string) string {
return filepath.Clean(filepath.Join(UploadDirectory, filename))
}
// GetResultPath returns the full path to a result file.
func GetResultPath(token string) string {
return filepath.Clean(filepath.Join(ResultDirectory, token+".json"))
}
// GetDownloadPath returns the full path and URL to a downloadable file.
func GetDownloadPath(filename string) string {
return filepath.Clean(filepath.Join(DownloadDirectory, filename))
}
// OpenSourceMap opens a source mapping file and returns it.
func OpenSourceMap(filename string) (*os.File, error) {
return os.Open(filepath.Join(SourceMapsDirectory, filename))
}
// PutResult writes a result to the specified token.
// Either a single json-serializable data argument can be provided,
// or multiple interleaved key-value pairs.
func PutResult(token, resType string, data ...interface{}) error {
fn := GetResultPath(token + "." + resType)
f, err := os.Create(fn)
if err != nil {
return err
}
if len(data) == 1 {
err = json.NewEncoder(f).Encode(data[0])
} else {
m := make(map[interface{}]interface{})
for i := 0; i < len(data); i += 2 {
m[data[i]] = data[i+1]
}
err = json.NewEncoder(f).Encode(m)
}
f.Close()
return err
}
// GetResult reads a result from the specified token.
// Data argument should be a pointer receiver.
func GetResult(token, resType string, data interface{}) (bool, error) {
fn := GetResultPath(token + "." + resType)
f, err := os.Open(fn)
if err != nil {
if os.IsNotExist(err) {
return true, err
}
return false, err
}
err = json.NewDecoder(f).Decode(data)
f.Close()
return false, err
}