-
Notifications
You must be signed in to change notification settings - Fork 206
/
local.go
116 lines (90 loc) · 3.16 KB
/
local.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
114
115
116
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
// Package store provides the implementation for a file system based storage provider.
// This enables all document upload previews to be processed AND stored locally.
package store
import (
"errors"
"io/ioutil"
"os"
"strings"
"github.com/documize/community/core/api/convert"
api "github.com/documize/community/core/convapi"
)
var folderPath string
func init() {
tempDir := os.TempDir()
if !strings.HasSuffix(tempDir, string(os.PathSeparator)) {
tempDir += string(os.PathSeparator)
}
folderPath = tempDir + "documize" + string(os.PathSeparator) + "_uploads" + string(os.PathSeparator)
os.MkdirAll(folderPath, os.ModePerm)
}
// LocalStorageProvider provides an implementation of StorageProvider.
type LocalStorageProvider struct {
}
// Upload a flie and store it locally.
func (store *LocalStorageProvider) Upload(job string, filename string, file []byte) (err error) {
destination := folderPath + job + string(os.PathSeparator)
err = os.MkdirAll(destination, os.ModePerm)
if err != nil {
return err
}
err = ioutil.WriteFile(destination+filename, file, 0666)
if err != nil {
return err
}
return nil
}
// Convert a file from its native format into Documize internal format.
func (store *LocalStorageProvider) Convert(params api.ConversionJobRequest) (filename string, fileResult *api.DocumentConversionResponse, err error) {
fileResult = &api.DocumentConversionResponse{}
err = nil
path := folderPath
if params.Job == "" {
return filename, fileResult, errors.New("no job to convert")
}
inputFolder := path + params.Job + string(os.PathSeparator)
list, err := ioutil.ReadDir(inputFolder)
if err != nil {
return filename, fileResult, err
}
if len(list) == 0 {
return filename, fileResult, errors.New("no file to convert")
}
// remove temporary directory on exit
defer func() { os.RemoveAll(inputFolder) }()
for _, v := range list {
if v.Size() > 0 && !strings.HasPrefix(v.Name(), ".") && v.Mode().IsRegular() {
filename = inputFolder + v.Name()
fileData, err := ioutil.ReadFile(filename)
if err != nil {
return filename, fileResult, err
}
if len(fileData) > 0 {
fileRequest := api.DocumentConversionRequest{}
fileRequest.Filename = filename
fileRequest.Filedata = fileData
fileRequest.PageBreakLevel = params.IndexDepth
fileRequest.LicenseKey = params.LicenseKey
fileRequest.LicenseSignature = params.LicenseSignature
fileRequest.ServiceEndpoint = params.ServiceEndpoint
//fileRequest.Job = params.OrgID + string(os.PathSeparator) + params.Job
//fileRequest.OrgID = params.OrgID
bits := strings.Split(filename, ".")
xtn := strings.ToLower(bits[len(bits)-1])
fileResult, err = convert.Convert(nil, xtn, &fileRequest)
return filename, fileResult, err
}
}
}
return filename, fileResult, nil
}