github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/store/local.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 // Package store provides the implementation for a file system based storage provider. 13 // This enables all document upload previews to be processed AND stored locally. 14 package store 15 16 import ( 17 "errors" 18 "fmt" 19 "io/ioutil" 20 "os" 21 "strings" 22 23 "github.com/documize/community/core/api/convert" 24 api "github.com/documize/community/core/convapi" 25 "github.com/documize/community/core/log" 26 ) 27 28 var folderPath string 29 30 func init() { 31 tempDir := os.TempDir() 32 if !strings.HasSuffix(tempDir, string(os.PathSeparator)) { 33 tempDir += string(os.PathSeparator) 34 } 35 folderPath = tempDir + "documize" + string(os.PathSeparator) + "_uploads" + string(os.PathSeparator) 36 log.Info("Temporary upload directory: " + folderPath) 37 log.IfErr(os.MkdirAll(folderPath, os.ModePerm)) 38 } 39 40 // LocalStorageProvider provides an implementation of StorageProvider. 41 type LocalStorageProvider struct { 42 } 43 44 // Upload a flie and store it locally. 45 func (store *LocalStorageProvider) Upload(job string, filename string, file []byte) (err error) { 46 destination := folderPath + job + string(os.PathSeparator) 47 48 err = os.MkdirAll(destination, os.ModePerm) 49 50 if err != nil { 51 log.Error(fmt.Sprintf("Cannot create local folder %s", destination), err) 52 return err 53 } 54 55 err = ioutil.WriteFile(destination+filename, file, 0666) 56 57 if err != nil { 58 log.Error(fmt.Sprintf("Cannot write to local file %s", destination+filename), err) 59 return err 60 } 61 62 return nil 63 } 64 65 // Convert a file from its native format into Documize internal format. 66 func (store *LocalStorageProvider) Convert(params api.ConversionJobRequest) (filename string, fileResult *api.DocumentConversionResponse, err error) { 67 fileResult = &api.DocumentConversionResponse{} 68 err = nil 69 path := folderPath 70 71 if params.Job == "" { 72 return filename, fileResult, errors.New("no job to convert") 73 } 74 75 inputFolder := path + params.Job + string(os.PathSeparator) 76 77 list, err := ioutil.ReadDir(inputFolder) 78 79 if err != nil { 80 return filename, fileResult, err 81 } 82 83 if len(list) == 0 { 84 return filename, fileResult, errors.New("no file to convert") 85 } 86 87 // remove temporary directory on exit 88 defer func() { log.IfErr(os.RemoveAll(inputFolder)) }() 89 90 for _, v := range list { 91 92 if v.Size() > 0 && !strings.HasPrefix(v.Name(), ".") && v.Mode().IsRegular() { 93 filename = inputFolder + v.Name() 94 log.Info(fmt.Sprintf("Fetching document %s", filename)) 95 96 fileData, err := ioutil.ReadFile(filename) 97 98 if err != nil { 99 log.Error(fmt.Sprintf("Unable to fetch document %s", filename), err) 100 return filename, fileResult, err 101 } 102 103 if len(fileData) > 0 { 104 fileRequest := api.DocumentConversionRequest{} 105 fileRequest.Filename = filename 106 fileRequest.Filedata = fileData 107 fileRequest.PageBreakLevel = params.IndexDepth 108 //fileRequest.Job = params.OrgID + string(os.PathSeparator) + params.Job 109 //fileRequest.OrgID = params.OrgID 110 111 bits := strings.Split(filename, ".") 112 xtn := strings.ToLower(bits[len(bits)-1]) 113 114 fileResult, err = convert.Convert(nil, xtn, &fileRequest) 115 return filename, fileResult, err 116 } 117 } 118 } 119 120 return filename, fileResult, nil 121 }