mirror of
https://github.com/documize/community.git
synced 2025-08-07 14:35:28 +02:00
first pass of code move to new API
This commit is contained in:
parent
5e2c96bf5b
commit
e284a46f86
6 changed files with 513 additions and 30 deletions
124
domain/conversion/store/local.go
Normal file
124
domain/conversion/store/local.go
Normal file
|
@ -0,0 +1,124 @@
|
|||
// 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"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/documize/community/core/api/convert"
|
||||
api "github.com/documize/community/core/convapi"
|
||||
"github.com/documize/community/core/log"
|
||||
)
|
||||
|
||||
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)
|
||||
log.Info("Temporary upload directory: " + folderPath)
|
||||
log.IfErr(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 {
|
||||
log.Error(fmt.Sprintf("Cannot create local folder %s", destination), err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(destination+filename, file, 0666)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Cannot write to local file %s", destination+filename), err)
|
||||
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() { log.IfErr(os.RemoveAll(inputFolder)) }()
|
||||
|
||||
for _, v := range list {
|
||||
|
||||
if v.Size() > 0 && !strings.HasPrefix(v.Name(), ".") && v.Mode().IsRegular() {
|
||||
filename = inputFolder + v.Name()
|
||||
log.Info(fmt.Sprintf("Fetching document %s", filename))
|
||||
|
||||
fileData, err := ioutil.ReadFile(filename)
|
||||
|
||||
if err != nil {
|
||||
log.Error(fmt.Sprintf("Unable to fetch document %s", filename), err)
|
||||
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
|
||||
}
|
90
domain/conversion/store/local_test.go
Normal file
90
domain/conversion/store/local_test.go
Normal file
|
@ -0,0 +1,90 @@
|
|||
// 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
|
||||
|
||||
import (
|
||||
"github.com/documize/community/core/api/plugins"
|
||||
"github.com/documize/community/core/api/util"
|
||||
api "github.com/documize/community/core/convapi"
|
||||
"github.com/documize/community/core/log"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var lsp LocalStorageProvider
|
||||
|
||||
func TestUpload(t *testing.T) {
|
||||
jb := "job" + uniqueid.Generate()
|
||||
fn := "file.txt"
|
||||
cont := "content\n"
|
||||
err := lsp.Upload(jb, fn, []byte(cont))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
b, e := ioutil.ReadFile(folderPath + jb + string(os.PathSeparator) + fn)
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
}
|
||||
if string(b) != cont {
|
||||
t.Error("wrong content:" + string(b))
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvert(t *testing.T) {
|
||||
_, _, err :=
|
||||
lsp.Convert(api.ConversionJobRequest{})
|
||||
if err == nil {
|
||||
t.Error("there should have been a convert error")
|
||||
}
|
||||
|
||||
err = plugins.LibSetup()
|
||||
if err == nil {
|
||||
// t.Error("did not error with missing config.json")
|
||||
}
|
||||
defer log.IfErr(plugins.Lib.KillSubProcs())
|
||||
|
||||
jb := "job" + uniqueid.Generate()
|
||||
|
||||
_, _, err =
|
||||
lsp.Convert(api.ConversionJobRequest{
|
||||
Job: jb,
|
||||
IndexDepth: 9,
|
||||
OrgID: "Documize",
|
||||
})
|
||||
if err == nil {
|
||||
t.Error("there should have been an error - directory not found")
|
||||
}
|
||||
|
||||
fn := "content.html"
|
||||
cont := "content\n"
|
||||
err = lsp.Upload(jb, fn, []byte(cont))
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
filename, fileResult, err :=
|
||||
lsp.Convert(api.ConversionJobRequest{
|
||||
Job: jb,
|
||||
IndexDepth: 9,
|
||||
OrgID: "Documize",
|
||||
})
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !strings.HasSuffix(filename, fn) {
|
||||
t.Error("wrong filename:" + filename)
|
||||
}
|
||||
if fileResult.Excerpt != "content." {
|
||||
t.Error("wrong excerpt:" + fileResult.Excerpt)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue