mirror of
https://github.com/documize/community.git
synced 2025-07-18 20:59:43 +02:00
removed experimental code
This commit is contained in:
parent
fddaf9effe
commit
7455a027fc
38 changed files with 1 additions and 499578 deletions
|
@ -1,3 +1,3 @@
|
||||||
Please see this document for contribution guidelines and process:
|
Please see this document for contribution guidelines and process:
|
||||||
|
|
||||||
https://developers.documize.com/s/leFaXwlU/open-source/d/VzO94aMOCgABGyfe/contributing
|
https://docs.documize.com/s/leFaXwlU/open-source/d/VzO94aMOCgABGyfe/contributing
|
||||||
|
|
|
@ -1,18 +0,0 @@
|
||||||
#! /bin/bash
|
|
||||||
|
|
||||||
NOW=$(date)
|
|
||||||
echo "Build process started $NOW"
|
|
||||||
|
|
||||||
for arch in amd64 ; do
|
|
||||||
for os in darwin linux windows ; do
|
|
||||||
if [ "$os" == "windows" ] ; then
|
|
||||||
echo "Compiling wordconvert.exe"
|
|
||||||
env GOOS=$os GOARCH=$arch go build -o bin/wordconvert.exe ./cmd/wordconvert
|
|
||||||
else
|
|
||||||
echo "Compiling wordconvert-$os"
|
|
||||||
env GOOS=$os GOARCH=$arch go build -o bin/wordconvert-$os ./cmd/wordconvert
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
done
|
|
||||||
|
|
||||||
echo "Finished."
|
|
|
@ -1,2 +0,0 @@
|
||||||
This directory contains a command line utility to convert multiple word files using
|
|
||||||
[the Documize word conversion API](https://documize.com/word-convert).
|
|
|
@ -1,241 +0,0 @@
|
||||||
// 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 main contains a command line utility to convert multiple word documents using api.documize.com
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"archive/zip"
|
|
||||||
"bytes"
|
|
||||||
"crypto/tls"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"mime/multipart"
|
|
||||||
"net"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
const serverURLfmt = "https://%s/api/1/word"
|
|
||||||
|
|
||||||
var server = flag.String("s", "api.documize.com:443", "the server")
|
|
||||||
var outputDir = flag.String("o", ".", "specify the directory to hold the output")
|
|
||||||
var ignoreBadCert = flag.Bool("k", false, "ignore bad certificate errors")
|
|
||||||
var verbose = flag.Bool("v", false, "verbose progress messages")
|
|
||||||
var stayziped = flag.Bool("z", false, "do not automatically unzip content")
|
|
||||||
var token = flag.String("t", "", "authorization token (if you use your e-mail address here during preview period, we will tell you before changes are made)")
|
|
||||||
var ignoreErrs = flag.Bool("e", false, "report errors on individual files, but continue")
|
|
||||||
var version = flag.Bool("version", false, "display the version of this code")
|
|
||||||
|
|
||||||
// does the file have a valid extension
|
|
||||||
func validXtn(fn string) bool {
|
|
||||||
lcfn := strings.ToLower(fn)
|
|
||||||
for _, xtn := range []string{".doc", ".docx", ".pdf"} {
|
|
||||||
if strings.HasSuffix(lcfn, xtn) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
// errCanContinue is the mechanism to print errors yet continue, if that command line option is chosen
|
|
||||||
func errCanContinue(can bool, err error) bool {
|
|
||||||
if err == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
if *ignoreErrs && can {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
os.Exit(0)
|
|
||||||
return true // never reached
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if *version {
|
|
||||||
fmt.Println("Version: 0.1 preview")
|
|
||||||
}
|
|
||||||
|
|
||||||
if *outputDir != "." {
|
|
||||||
if err := os.Mkdir(*outputDir, 0777); err != nil && !os.IsExist(err) {
|
|
||||||
errCanContinue(false, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
host, _, err := net.SplitHostPort(*server)
|
|
||||||
errCanContinue(false, err)
|
|
||||||
|
|
||||||
tlc := &tls.Config{
|
|
||||||
InsecureSkipVerify: *ignoreBadCert,
|
|
||||||
ServerName: host,
|
|
||||||
}
|
|
||||||
|
|
||||||
transport := &http.Transport{TLSClientConfig: tlc}
|
|
||||||
hclient := &http.Client{Transport: transport}
|
|
||||||
|
|
||||||
processFiles(hclient)
|
|
||||||
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
func processFiles(hclient *http.Client) {
|
|
||||||
|
|
||||||
for _, fileName := range flag.Args() {
|
|
||||||
|
|
||||||
if validXtn(fileName) {
|
|
||||||
|
|
||||||
if *verbose {
|
|
||||||
fmt.Println("processing", fileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
content, err := ioutil.ReadFile(fileName)
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
bodyBuf := &bytes.Buffer{}
|
|
||||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
|
||||||
|
|
||||||
_, fn := path.Split(fileName)
|
|
||||||
fileWriter, err := bodyWriter.CreateFormFile("wordfile", fn) // name as expected by the API
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(fileWriter, bytes.NewReader(content))
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
contentType := bodyWriter.FormDataContentType()
|
|
||||||
err = bodyWriter.Close()
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
target := fmt.Sprintf(serverURLfmt, *server)
|
|
||||||
if *token != "" {
|
|
||||||
target += "?token=" + *token // NOTE: after the preview phase, token will not be optional
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST",
|
|
||||||
target,
|
|
||||||
bodyBuf)
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
req.Header.Set("Content-Type", contentType)
|
|
||||||
resp, err := hclient.Do(req)
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
zipdata, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
|
||||||
if errCanContinue(true, errors.New("server returned status: "+resp.Status)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
targetDir := *outputDir + "/" + fn + ".content"
|
|
||||||
if *stayziped {
|
|
||||||
if err := ioutil.WriteFile(targetDir+".zip", zipdata, 0666); err != nil {
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if errCanContinue(true, unzipFiles(zipdata, targetDir)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
if *verbose {
|
|
||||||
fmt.Println("ignored", fileName)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// simple unzip
|
|
||||||
func unzipFiles(zipdata []byte, targetDir string) error {
|
|
||||||
|
|
||||||
rdr, err := zip.NewReader(bytes.NewReader(zipdata), int64(len(zipdata)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.Mkdir(targetDir, 0777); err != nil && !os.IsExist(err) { // make sure the target directory exists
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
fileLoop:
|
|
||||||
for _, zf := range rdr.File {
|
|
||||||
frc, err := zf.Open()
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
filedata, err := ioutil.ReadAll(frc)
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
subTarget := targetDir + "/" + zf.Name
|
|
||||||
|
|
||||||
subDir := path.Dir(subTarget)
|
|
||||||
|
|
||||||
if subDir != targetDir {
|
|
||||||
rump := strings.TrimPrefix(subDir, targetDir)
|
|
||||||
tree := strings.Split(rump, "/")
|
|
||||||
built := ""
|
|
||||||
for _, thisPart := range tree[1:] { // make sure we have a directory at each level of the tree
|
|
||||||
built += "/" + thisPart
|
|
||||||
if err := os.Mkdir(targetDir+built, 0777); err != nil && !os.IsExist(err) {
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue fileLoop
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ioutil.WriteFile(subTarget, filedata, 0666); err != nil {
|
|
||||||
if errCanContinue(true, err) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if *verbose {
|
|
||||||
fmt.Println("wrote", subTarget)
|
|
||||||
}
|
|
||||||
frc.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -14,11 +14,9 @@ package convert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/documize/community/core/api/convert/excerpt"
|
|
||||||
"github.com/documize/community/core/api/convert/html"
|
"github.com/documize/community/core/api/convert/html"
|
||||||
"github.com/documize/community/core/api/plugins"
|
"github.com/documize/community/core/api/plugins"
|
||||||
api "github.com/documize/community/core/convapi"
|
api "github.com/documize/community/core/convapi"
|
||||||
"github.com/documize/community/core/utility"
|
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
)
|
)
|
||||||
|
@ -49,32 +47,6 @@ func Convert(ctx context.Context, xtn string, fileRequest *api.DocumentConversio
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
if fileResult.Excerpt != "" {
|
|
||||||
//fmt.Println("DEBUG supplied excerpt: " + fileResult.Excerpt)
|
|
||||||
} else {
|
|
||||||
titleWds := []string{}
|
|
||||||
bodyWds := []string{}
|
|
||||||
for p := range fileResult.Pages {
|
|
||||||
var wds []string
|
|
||||||
var err error
|
|
||||||
if p > 0 { // title 0 is already the title of the document
|
|
||||||
wds, _, err = utility.Words(utility.HTML(fileResult.Pages[p].Title), 0, false)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
titleWds = append(titleWds, wds...)
|
|
||||||
titleWds = append(titleWds, ".")
|
|
||||||
}
|
|
||||||
wds, _, err = utility.Words(utility.HTML(string(fileResult.Pages[p].Body)), 0, false)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
bodyWds = append(bodyWds, wds...)
|
|
||||||
bodyWds = append(bodyWds, ".")
|
|
||||||
}
|
|
||||||
fileResult.Excerpt = excerpt.Excerpt(titleWds, bodyWds)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fileResult, nil
|
return fileResult, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,228 +0,0 @@
|
||||||
// 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 excerpt provides basic functionality to create excerpts of text in English.
|
|
||||||
package excerpt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
words "github.com/documize/community/core/wordlists/en-2012"
|
|
||||||
|
|
||||||
"github.com/rookii/paicehusk"
|
|
||||||
)
|
|
||||||
|
|
||||||
type extractItem struct {
|
|
||||||
sequence int
|
|
||||||
score float64
|
|
||||||
count int
|
|
||||||
sentance string
|
|
||||||
}
|
|
||||||
|
|
||||||
type extractList []extractItem
|
|
||||||
|
|
||||||
// the Sort interface
|
|
||||||
// Len is the number of elements in the collection.
|
|
||||||
func (a extractList) Len() int { return len(a) }
|
|
||||||
|
|
||||||
// Less reports whether the element with
|
|
||||||
// index i should sort before the element with index j.
|
|
||||||
func (a extractList) Less(i, j int) bool {
|
|
||||||
return (a[i].score / float64(a[i].count)) > (a[j].score / float64(a[j].count))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap swaps the elements with indexes i and j.
|
|
||||||
func (a extractList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
|
|
||||||
type presentItem struct {
|
|
||||||
sequence int
|
|
||||||
text string
|
|
||||||
}
|
|
||||||
|
|
||||||
type presentList []presentItem
|
|
||||||
|
|
||||||
// the Sort interface
|
|
||||||
// Len is the number of elements in the collection.
|
|
||||||
func (a presentList) Len() int { return len(a) }
|
|
||||||
|
|
||||||
// Less reports whether the element with
|
|
||||||
// index i should sort before the element with index j.
|
|
||||||
func (a presentList) Less(i, j int) bool {
|
|
||||||
return a[i].sequence < a[j].sequence
|
|
||||||
}
|
|
||||||
|
|
||||||
// Swap swaps the elements with indexes i and j.
|
|
||||||
func (a presentList) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
||||||
|
|
||||||
func addWd(sentance, wd string) (string, bool) {
|
|
||||||
var isStop bool
|
|
||||||
if len(sentance) == 0 {
|
|
||||||
if wd != "[" {
|
|
||||||
sentance = wd
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
switch wd {
|
|
||||||
case "[": //NoOp
|
|
||||||
case "0", "1", "2", "3", "4", "5", "6", "7", "8", "9":
|
|
||||||
if unicode.IsDigit(rune(sentance[len(sentance)-1])) {
|
|
||||||
sentance += wd
|
|
||||||
} else {
|
|
||||||
sentance += " " + wd
|
|
||||||
}
|
|
||||||
case ".", "!", "?":
|
|
||||||
isStop = true
|
|
||||||
fallthrough
|
|
||||||
default:
|
|
||||||
if isPunct(wd) {
|
|
||||||
sentance += wd
|
|
||||||
} else {
|
|
||||||
sentance += " " + wd
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return sentance, isStop
|
|
||||||
}
|
|
||||||
|
|
||||||
func isPunct(s string) bool {
|
|
||||||
for _, r := range s {
|
|
||||||
if !unicode.IsPunct(r) {
|
|
||||||
switch r {
|
|
||||||
case '`', '\'', '"', '(', '/': // still punct
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
// Excerpt returns the most statically significant 100 or so words of text for use in the Excerpt field
|
|
||||||
func Excerpt(titleWords, bodyWords []string) string {
|
|
||||||
var el extractList
|
|
||||||
|
|
||||||
//fmt.Println("DEBUG Excerpt ", len(titleWords), len(bodyWords))
|
|
||||||
|
|
||||||
// populate stemMap
|
|
||||||
stemMap := make(map[string]uint64)
|
|
||||||
for _, wd := range bodyWords {
|
|
||||||
stem := paicehusk.DefaultRules.Stem(wd) // find the stem of the word
|
|
||||||
stemMap[stem]++
|
|
||||||
}
|
|
||||||
for _, wd := range titleWords {
|
|
||||||
stem := paicehusk.DefaultRules.Stem(wd) // find the stem of the word
|
|
||||||
stemMap[stem]++ // TODO are words in titles more important?
|
|
||||||
}
|
|
||||||
|
|
||||||
wds := append(titleWords, bodyWords...)
|
|
||||||
|
|
||||||
sentance := ""
|
|
||||||
score := 0.0
|
|
||||||
count := 0
|
|
||||||
seq := 0
|
|
||||||
for _, wd := range wds {
|
|
||||||
var isStop bool
|
|
||||||
|
|
||||||
sentance, isStop = addWd(sentance, wd)
|
|
||||||
|
|
||||||
if isStop {
|
|
||||||
//fmt.Printf(" DEBUG sentance: %3d %3.2f %s\n",
|
|
||||||
// seq, score*10000/float64(count), sentance)
|
|
||||||
var ei extractItem
|
|
||||||
ei.count = count + 1 // must be at least 1
|
|
||||||
ei.score = score
|
|
||||||
ei.sentance = sentance
|
|
||||||
ei.sequence = seq
|
|
||||||
el = append(el, ei)
|
|
||||||
sentance = ""
|
|
||||||
score = 0.0
|
|
||||||
seq++
|
|
||||||
} else {
|
|
||||||
uncommon := true
|
|
||||||
// TODO Discuss correct level or maybe find a better algorithem for this
|
|
||||||
ent, ok := words.Words[wd]
|
|
||||||
if ok {
|
|
||||||
if ent.Rank <= 100 {
|
|
||||||
// do not score very common words
|
|
||||||
uncommon = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if uncommon {
|
|
||||||
stem := paicehusk.DefaultRules.Stem(wd) // find the stem of the word
|
|
||||||
usage, used := stemMap[stem]
|
|
||||||
if used {
|
|
||||||
relativeStemFreq := (float64(usage) / float64(len(wds))) - words.Stems[stem]
|
|
||||||
if relativeStemFreq > 0.0 {
|
|
||||||
score += relativeStemFreq
|
|
||||||
}
|
|
||||||
}
|
|
||||||
count++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Sort(el)
|
|
||||||
|
|
||||||
return present(el)
|
|
||||||
}
|
|
||||||
|
|
||||||
func present(el extractList) (ret string) {
|
|
||||||
var pl presentList
|
|
||||||
words := 0
|
|
||||||
|
|
||||||
const excerptWords = 50
|
|
||||||
|
|
||||||
for s, e := range el {
|
|
||||||
if (words < excerptWords || s == 0) && len(e.sentance) > 1 &&
|
|
||||||
notEmpty(e.sentance) {
|
|
||||||
words += e.count
|
|
||||||
pl = append(pl, presentItem{sequence: e.sequence, text: e.sentance})
|
|
||||||
//fmt.Printf("DEBUG With score %3.2f on page %d // %s \n",
|
|
||||||
// 1000*e.score/float64(e.count), e.sequence, e.sentance)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sort.Sort(pl)
|
|
||||||
|
|
||||||
var lastSeq int
|
|
||||||
for p := range pl {
|
|
||||||
txt := strings.TrimPrefix(pl[p].text, ". ")
|
|
||||||
if p == 0 {
|
|
||||||
ret = txt
|
|
||||||
lastSeq = pl[0].sequence
|
|
||||||
} else {
|
|
||||||
thisSeq := pl[p].sequence
|
|
||||||
if lastSeq+1 != thisSeq {
|
|
||||||
ret += " …" // Horizontal elipsis character
|
|
||||||
}
|
|
||||||
ret += " " + txt
|
|
||||||
lastSeq = thisSeq
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(ret) > 250 { // make sure the excerpt is not too long, shorten it if required
|
|
||||||
for len(ret) > 250 {
|
|
||||||
_, size := utf8.DecodeLastRuneInString(ret)
|
|
||||||
ret = ret[:len(ret)-size]
|
|
||||||
}
|
|
||||||
return ret + "…" // Horizontal elipsis character added after truncation
|
|
||||||
}
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func notEmpty(wds string) bool {
|
|
||||||
for _, r := range wds {
|
|
||||||
if !unicode.IsPunct(r) && !unicode.IsSpace(r) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
|
@ -1,130 +0,0 @@
|
||||||
// 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 excerpt_test
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
import "github.com/documize/community/core/api/convert/excerpt"
|
|
||||||
import "strings"
|
|
||||||
import "fmt"
|
|
||||||
|
|
||||||
func TestExerpt(t *testing.T) {
|
|
||||||
if excerpt.Excerpt(nil, nil) != "" ||
|
|
||||||
excerpt.Excerpt([]string{}, []string{}) != "" {
|
|
||||||
t.Error("empty lists do not return empty string")
|
|
||||||
}
|
|
||||||
qbf := strings.Split("The quick brown fox jumps over the lazy dog .", " ")
|
|
||||||
qbf2 := qbf
|
|
||||||
for i := 0; i < 200; i++ {
|
|
||||||
qbf2 = append(qbf2, qbf...)
|
|
||||||
}
|
|
||||||
tst := excerpt.Excerpt(qbf, qbf2)
|
|
||||||
if tst !=
|
|
||||||
"The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog." {
|
|
||||||
t.Error("'quick brown fox' did not work:", tst)
|
|
||||||
}
|
|
||||||
|
|
||||||
tt123 := strings.Split("Testing , testing ; 1 2 3 is fun ! Bracket [ anyone ? .", " ")
|
|
||||||
tt123a := tt123
|
|
||||||
for i := 0; i < 200; i++ {
|
|
||||||
tt123a = append(tt123a, fmt.Sprintf("%d", i))
|
|
||||||
tt123a = append(tt123a, tt123...)
|
|
||||||
}
|
|
||||||
tst2 := excerpt.Excerpt(tt123, tt123a)
|
|
||||||
if tst2 !=
|
|
||||||
"Testing, testing; 123 is fun! … Testing, testing; 123 is fun! … 0 Testing, testing; 123 is fun!" {
|
|
||||||
t.Error("'Testing testing 123' did not work:", tst2)
|
|
||||||
}
|
|
||||||
|
|
||||||
s := strings.Split(strings.Replace(`
|
|
||||||
It's supercalifragilisticexpialidocious
|
|
||||||
Even though the sound of it is something quite atrocious
|
|
||||||
If you say it loud enough, you'll always sound precocious
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
|
|
||||||
Because I was afraid to speak
|
|
||||||
When I was just a lad
|
|
||||||
My father gave me nose a tweak
|
|
||||||
And told me I was bad
|
|
||||||
|
|
||||||
But then one day I learned a word
|
|
||||||
That saved me achin' nose
|
|
||||||
The biggest word I ever heard
|
|
||||||
And this is how it goes, oh
|
|
||||||
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
Even though the sound of it is something quite atrocious
|
|
||||||
If you say it loud enough, you'll always sound precocious
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
|
|
||||||
He traveled all around the world
|
|
||||||
And everywhere he went
|
|
||||||
He'd use his word and all would say
|
|
||||||
There goes a clever gent
|
|
||||||
|
|
||||||
When Dukes and Maharajahs
|
|
||||||
Pass the time of day with me
|
|
||||||
I say me special word
|
|
||||||
And then they ask me out to tea
|
|
||||||
|
|
||||||
Oh, supercalifragilisticexpialidocious
|
|
||||||
Even though the sound of it is something quite atrocious
|
|
||||||
If you say it loud enough, you'll always sound precocious
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
Um diddle, diddle diddle, um diddle ay
|
|
||||||
|
|
||||||
No, you can say it backwards, which is dociousaliexpilisticfragicalirupus
|
|
||||||
But that's going a bit too far, don't you think?
|
|
||||||
|
|
||||||
So when the cat has got your tongue
|
|
||||||
There's no need for dismay
|
|
||||||
Just summon up this word
|
|
||||||
And then you've got a lot to say
|
|
||||||
|
|
||||||
But better use it carefully
|
|
||||||
Or it could change your life
|
|
||||||
For example, yes, one night I said it to me girl
|
|
||||||
And now me girl's my wife, oh, and a lovely thing she's too
|
|
||||||
|
|
||||||
She's, supercalifragilisticexpialidocious
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
Supercalifragilisticexpialidocious
|
|
||||||
. `, "\n", " . ", -1), " ")
|
|
||||||
ts := []string{"Supercalifragilisticexpialidocious", "song", "lyrics"}
|
|
||||||
st := excerpt.Excerpt(ts, s)
|
|
||||||
if st != "Supercalifragilisticexpialidocious song lyrics. … Um diddle, diddle diddle, um diddle ay. Um diddle, diddle diddle, um diddle ay." {
|
|
||||||
t.Error("'Supercalifragilisticexpialidocious song lyrics' did not work:", st)
|
|
||||||
}
|
|
||||||
|
|
||||||
ss := []string{"Supercalifragilisticexpialidocious", "!"}
|
|
||||||
ssa := ss
|
|
||||||
for i := 0; i < 100; i++ {
|
|
||||||
ssa = append(ssa, ss...)
|
|
||||||
}
|
|
||||||
sst := excerpt.Excerpt(ss, ssa)
|
|
||||||
if sst !=
|
|
||||||
"Supercalifragilisticexpialidocious! Supercalifragilisticexpialidocious! Supercalifragilisticexpialidocious! Supercalifragilisticexpialidocious! Supercalifragilisticexpialidocious! Supercalifragilisticexpialidocious! Supercalifragilisticexpialidocious…" {
|
|
||||||
t.Error("'Supercalifragilisticexpialidocious' did not work:", sst)
|
|
||||||
}
|
|
||||||
}
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,4 +0,0 @@
|
||||||
Total files: 23406
|
|
||||||
Unique word count: 521426
|
|
||||||
Total word count: 145376051
|
|
||||||
Overall word count: 193225723
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load diff
|
@ -1,149 +0,0 @@
|
||||||
// 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 main creates ordered lists of english words and their stems,
|
|
||||||
// based on their frequency.
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"sort"
|
|
||||||
|
|
||||||
"github.com/rookii/paicehusk"
|
|
||||||
)
|
|
||||||
|
|
||||||
type wordFreqEntry struct {
|
|
||||||
rawFreq int
|
|
||||||
Freq float64
|
|
||||||
}
|
|
||||||
|
|
||||||
type wordFreqMap map[string]wordFreqEntry
|
|
||||||
|
|
||||||
type wordFreqSortEntry struct {
|
|
||||||
Name string
|
|
||||||
Freq float64
|
|
||||||
}
|
|
||||||
type wordFreqSort []wordFreqSortEntry
|
|
||||||
|
|
||||||
// Len is the number of elements in the collection.
|
|
||||||
func (wfs wordFreqSort) Len() int { return len(wfs) }
|
|
||||||
|
|
||||||
// Less reports whether the element with
|
|
||||||
// index i should sort before the element with index j.
|
|
||||||
func (wfs wordFreqSort) Less(i, j int) bool { return wfs[i].Freq > wfs[j].Freq }
|
|
||||||
|
|
||||||
// Swap swaps the elements with indexes i and j.
|
|
||||||
func (wfs wordFreqSort) Swap(i, j int) { wfs[j], wfs[i] = wfs[i], wfs[j] }
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
txt, err := ioutil.ReadFile("./en-2012/en.txt")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
lines := bytes.Split(txt, []byte("\n"))
|
|
||||||
|
|
||||||
wfm := make(wordFreqMap)
|
|
||||||
rfTot := 0
|
|
||||||
for r, l := range lines {
|
|
||||||
words := bytes.Split(l, []byte(" "))
|
|
||||||
if len(words) >= 2 {
|
|
||||||
var rf int
|
|
||||||
_, err = fmt.Sscanf(string(words[1]), "%d", &rf)
|
|
||||||
if err == nil && len(words[0]) > 0 {
|
|
||||||
if r < 10000 { // only look at the most common 10k words, 100k makes go compile/link unworkable
|
|
||||||
stem := string(words[0]) // NOTE not stemming at present
|
|
||||||
entry, alredythere := wfm[stem]
|
|
||||||
if alredythere {
|
|
||||||
entry.rawFreq += rf
|
|
||||||
wfm[stem] = entry
|
|
||||||
} else {
|
|
||||||
wfm[stem] = wordFreqEntry{rawFreq: rf, Freq: 0.0}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
rfTot += rf
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for k, v := range wfm {
|
|
||||||
v.Freq = float64(v.rawFreq) / float64(rfTot)
|
|
||||||
wfm[k] = v
|
|
||||||
}
|
|
||||||
|
|
||||||
wfs := make(wordFreqSort, len(wfm))
|
|
||||||
idx := 0
|
|
||||||
for k, v := range wfm {
|
|
||||||
wfs[idx].Name = k
|
|
||||||
wfs[idx].Freq = v.Freq
|
|
||||||
idx++
|
|
||||||
}
|
|
||||||
sort.Sort(wfs)
|
|
||||||
writeWords(wfs, wfm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func writeWords(wfs wordFreqSort, wfm wordFreqMap) {
|
|
||||||
var goprog bytes.Buffer
|
|
||||||
var err error
|
|
||||||
|
|
||||||
fmt.Fprintf(&goprog, `
|
|
||||||
// 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 words was auto-generated !
|
|
||||||
// From base data at http://invokeit.wordpress.com/frequency-word-lists/ .
|
|
||||||
// The word stems were produced using github.com/rookii/paicehusk .
|
|
||||||
// DO NOT EDIT BY HAND.
|
|
||||||
package words
|
|
||||||
|
|
||||||
// Entry type describes the rank and frequency of a prarticular word.
|
|
||||||
type Entry struct {
|
|
||||||
Rank int // Word Rank order, 1 most frequent.
|
|
||||||
Freq float64 // Word Frequency, a fraction, larger is more frequent.
|
|
||||||
}
|
|
||||||
|
|
||||||
// Map type provides the Entry information for each word.
|
|
||||||
type Map map[string]Entry
|
|
||||||
|
|
||||||
// Words gives the Entry information on the most frequent words.
|
|
||||||
var Words = Map{
|
|
||||||
`)
|
|
||||||
for i, v := range wfs {
|
|
||||||
fmt.Fprintf(&goprog, "\t"+`"%s": Entry{Rank:%d,Freq:%g},`+"\n", v.Name, i+1, v.Freq)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&goprog, "}\n\n")
|
|
||||||
|
|
||||||
sfm := make(map[string]float64)
|
|
||||||
for k, v := range wfm {
|
|
||||||
sfm[paicehusk.DefaultRules.Stem(k)] += v.Freq
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&goprog, "// Stems gives the frequency of word-stems.\nvar Stems = map[string]float64{\n")
|
|
||||||
for k, v := range sfm {
|
|
||||||
fmt.Fprintf(&goprog, "\t"+`"%s": %g,`+"\n", k, v)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(&goprog, "}\n\n")
|
|
||||||
|
|
||||||
err = ioutil.WriteFile("./en-2012/englishwords.go", goprog.Bytes(), 0666)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,20 +0,0 @@
|
||||||
# SDK for the Documize system
|
|
||||||
|
|
||||||
The SDK is in development, please do not use in a live environment.
|
|
||||||
|
|
||||||
## documize command
|
|
||||||
|
|
||||||
The directory "documize" contains a command line utility to load files onto the Documize server.
|
|
||||||
Run the command with "--help" to see the available flags.
|
|
||||||
|
|
||||||
## test suite (currently disabled)
|
|
||||||
|
|
||||||
The directory "exttest" contains a set of tests that are used both to test this package and to test the main documize code.
|
|
||||||
|
|
||||||
In order to run these tests two environment variables must be set:
|
|
||||||
* DOCUMIZEAPI - the url of the endpoint, which must be http://localhost:5002 at present
|
|
||||||
* DOCUMIZEAUTH - the authorization credentials in the form ```domain:email:password```,
|
|
||||||
which must be of the form ```:mick@jagger.com:demo123``` at present,
|
|
||||||
with the Documize DB organistion record having the default (empty) subdomain.
|
|
||||||
|
|
||||||
There must also be a single folder named "Test" for code to find and use. TODO(Elliott) remove this restriction.
|
|
|
@ -1,31 +0,0 @@
|
||||||
// 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 documize_test
|
|
||||||
|
|
||||||
/* TODO(Elliott)
|
|
||||||
import "testing"
|
|
||||||
import "github.com/documize/community/sdk/exttest"
|
|
||||||
|
|
||||||
func TestAPItest(t *testing.T) {
|
|
||||||
exttest.APItest(t)
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkAPIbench(b *testing.B) {
|
|
||||||
for n := 0; n < b.N; n++ {
|
|
||||||
err := exttest.APIbenchmark()
|
|
||||||
if err != nil {
|
|
||||||
b.Error(err)
|
|
||||||
b.Fail()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
|
@ -1,144 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
const emptyBraces = "{}"
|
|
||||||
|
|
||||||
// GetAttachmentData get the data of a file attachement.
|
|
||||||
func (c *Client) GetAttachmentData(att *entity.Attachment) error {
|
|
||||||
url := fmt.Sprintf("%s/api/public/attachments/%s/%s/%s",
|
|
||||||
c.BaseURL, att.OrgID, att.Job, att.FileID)
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
att.Data = b
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteAttachment removes a file attachment.
|
|
||||||
func (c *Client) DeleteAttachment(att *entity.Attachment) error {
|
|
||||||
url := fmt.Sprintf("%s/api/documents/%s/attachments/%s",
|
|
||||||
c.BaseURL, att.DocumentID, att.RefID)
|
|
||||||
req, err := http.NewRequest("DELETE", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if string(b) != emptyBraces {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetAttachments gets a slice of the attachments for a document ID.
|
|
||||||
func (c *Client) GetAttachments(documentID string) (entAtts []entity.Attachment, err error) {
|
|
||||||
url := fmt.Sprintf("%s/api/documents/%s/attachments",
|
|
||||||
c.BaseURL, documentID)
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, &entAtts)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return entAtts, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddAttachment adds a new attachement to a document
|
|
||||||
func (c *Client) AddAttachment(documentID, filename string, data []byte) error {
|
|
||||||
url := fmt.Sprintf("%s/api/documents/%s/attachments",
|
|
||||||
c.BaseURL, documentID)
|
|
||||||
var buf bytes.Buffer
|
|
||||||
w := multipart.NewWriter(&buf)
|
|
||||||
|
|
||||||
writer, err := w.CreateFormFile("attachment", filename)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
n, err := writer.Write(data)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if n != len(data) {
|
|
||||||
return errors.New("incorrect length written")
|
|
||||||
}
|
|
||||||
err = w.Close()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", url, bytes.NewReader(buf.Bytes()))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
req.Header.Set("Content-Type",
|
|
||||||
"multipart/form-data; boundary="+w.Boundary())
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if string(b) != emptyBraces {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
95
sdk/auth.go
95
sdk/auth.go
|
@ -1,95 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/endpoint/models"
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Client holds the data for a sustained connection to Documize.
|
|
||||||
type Client struct {
|
|
||||||
BaseURL string
|
|
||||||
Domain string
|
|
||||||
Client *http.Client
|
|
||||||
Auth models.AuthenticationModel
|
|
||||||
}
|
|
||||||
|
|
||||||
// HeaderAuthTokenName is the name of the authorization token required in the http header
|
|
||||||
const HeaderAuthTokenName = "Authorization"
|
|
||||||
|
|
||||||
// NewClient authorizes the user on Documize and returns the Client type whose methods allow API access the Documize system.
|
|
||||||
func NewClient(baseurl, domainEmailPassword string) (*Client, error) {
|
|
||||||
c := new(Client)
|
|
||||||
c.Client = new(http.Client)
|
|
||||||
c.BaseURL = strings.TrimSuffix(baseurl, "/")
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/public/authenticate", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName,
|
|
||||||
"Basic "+base64.StdEncoding.EncodeToString([]byte(domainEmailPassword)))
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
msg, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(msg, &c.Auth)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(msg)) + " : " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = c.Validate(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Domain = strings.Split(domainEmailPassword, ":")[0]
|
|
||||||
|
|
||||||
return c, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate the current user credentials.
|
|
||||||
func (c *Client) Validate() error {
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/public/validate", nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add("Authorization", c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
var um entity.User
|
|
||||||
msg, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(msg, &um)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New(string(msg) + " : " + err.Error())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
209
sdk/document.go
209
sdk/document.go
|
@ -1,209 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// DeleteDocument removes the given document from the Documize database.
|
|
||||||
func (c *Client) DeleteDocument(documentID string) error {
|
|
||||||
req, err := http.NewRequest("DELETE",
|
|
||||||
c.BaseURL+"/api/documents/"+documentID,
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if string(b) == emptyBraces {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors.New(string(b))
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDocument gets the document information.
|
|
||||||
func (c *Client) GetDocument(documentID string) (*entity.Document, error) {
|
|
||||||
req, err := http.NewRequest("GET",
|
|
||||||
c.BaseURL+"/api/documents/"+documentID,
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var dm entity.Document
|
|
||||||
err = json.Unmarshal(b, &dm)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return &dm, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateDocument updates document information obtained from GetDocument.
|
|
||||||
func (c *Client) UpdateDocument(document *entity.Document) error {
|
|
||||||
if document == nil {
|
|
||||||
return errors.New("nil document passed to UpdateDocument")
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("PUT",
|
|
||||||
c.BaseURL+"/api/documents/"+document.RefID,
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
buf, err := json.Marshal(document)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Body = ioutil.NopCloser(bytes.NewReader(buf))
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
_, err = ioutil.ReadAll(resp.Body)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDocumentMeta gets the metadata for a document.
|
|
||||||
func (c *Client) GetDocumentMeta(documentID string) (*entity.DocumentMeta, error) {
|
|
||||||
req, err := http.NewRequest("GET",
|
|
||||||
c.BaseURL+"/api/documents/"+documentID+"/meta",
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var dm entity.DocumentMeta
|
|
||||||
err = json.Unmarshal(b, &dm)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return &dm, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDocumentsByFolder returns a slice of document information for a given folder.
|
|
||||||
func (c *Client) GetDocumentsByFolder(folderID string) ([]entity.Document, error) {
|
|
||||||
req, err := http.NewRequest("GET",
|
|
||||||
c.BaseURL+"/api/documents?folder="+folderID,
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var dm []entity.Document
|
|
||||||
err = json.Unmarshal(b, &dm)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return dm, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SearchDocuments returns a list of documements which contain the supplied keywords.
|
|
||||||
// TODO explain the format of the keywords string (when not just the single word sought).
|
|
||||||
func (c *Client) SearchDocuments(keywords string) ([]entity.DocumentSearch, error) {
|
|
||||||
req, err := http.NewRequest("GET",
|
|
||||||
c.BaseURL+"/api/search?keywords="+url.QueryEscape(keywords),
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var ds []entity.DocumentSearch
|
|
||||||
err = json.Unmarshal(b, &ds)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return ds, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDocumentAsDocx returns a file-name and content for the given documentID.
|
|
||||||
// TODO allow the selection of either HTML or DOCX format.
|
|
||||||
func (c *Client) GetDocumentAsDocx(documentID string) (string, []byte, error) {
|
|
||||||
req, err := http.NewRequest("GET",
|
|
||||||
c.BaseURL+"/api/documents/"+documentID+"/export",
|
|
||||||
nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return "", nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
filename := resp.Header.Get("Content-Disposition")
|
|
||||||
filename = strings.TrimSpace(strings.TrimPrefix(filename, "attachment; filename="))
|
|
||||||
if len(filename) == 0 {
|
|
||||||
return "", nil, errors.New("Unknown document to download")
|
|
||||||
}
|
|
||||||
|
|
||||||
return filename, b, nil
|
|
||||||
}
|
|
|
@ -1,86 +0,0 @@
|
||||||
// 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 main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
sdk "github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
|
|
||||||
flagSet := flag.NewFlagSet("documize client flags", flag.ExitOnError)
|
|
||||||
|
|
||||||
url, auth, folder, action := flagSet.String("api", os.Getenv("DOCUMIZEAPI"),
|
|
||||||
"the url of the endpoint (defaults to environment variable DOCUMIZEAPI)"), //e.g. http://localhost:5002
|
|
||||||
flagSet.String("auth", os.Getenv("DOCUMIZEAUTH"), //e.g. demo1:mick@jagger.com:demo123
|
|
||||||
"the authorization credentials in the form domain:email:password (defaults to the environment variable DOCUMIZEAUTH)"),
|
|
||||||
flagSet.String("folder", "", "the Documize folder to use"),
|
|
||||||
flagSet.String("action", "load", "the Documize action to take")
|
|
||||||
|
|
||||||
if err := flagSet.Parse(os.Args[1:]); err != nil {
|
|
||||||
fmt.Println("unable to parse Documize arguments:", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if *url == "" {
|
|
||||||
fmt.Println("Please set the environment variable DOCUMIZEAPI or use the -api flag")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if *auth == "" {
|
|
||||||
fmt.Println("Please set the environment variable DOCUMIZEAUTH or use the -auth flag")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
c, e := sdk.NewClient(*url, *auth)
|
|
||||||
if e != nil {
|
|
||||||
fmt.Println("unable to create Documize SDK client for", *auth, "Error:", e)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch *action {
|
|
||||||
case "load":
|
|
||||||
folderID := checkFolder(c, folder)
|
|
||||||
for _, arg := range flagSet.Args() {
|
|
||||||
_, ce := c.LoadFile(folderID, arg)
|
|
||||||
if ce == nil {
|
|
||||||
fmt.Println("Loaded file " + arg + " into Documize folder " + *folder)
|
|
||||||
} else {
|
|
||||||
fmt.Println("Failed to load file " + arg + " into Documize folder " + *folder + " Error: " + ce.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkFolder(c *sdk.Client, folder *string) string {
|
|
||||||
if *folder == "" {
|
|
||||||
*folder = os.Getenv("DOCUMIZEFOLDER")
|
|
||||||
if *folder == "" {
|
|
||||||
fmt.Println("Please set the environment variable DOCUMIZEFOLDER or use the -folder flag")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fids, err := c.GetNamedFolderIDs(*folder)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading folder IDs: " + err.Error())
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if len(fids) != 1 {
|
|
||||||
fmt.Println("There is no single folder called: " + *folder)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
return fids[0]
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
const (
|
|
||||||
errPfx = "{Error: '"
|
|
||||||
errSfx = "'}"
|
|
||||||
)
|
|
||||||
|
|
||||||
func trimErrors(e string) string {
|
|
||||||
return strings.TrimPrefix(strings.TrimSuffix(e, errSfx), errPfx)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isError(e string) bool {
|
|
||||||
return strings.HasPrefix(e, errPfx)
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
// APIbenchmark is the main entry point for the benchmark code, it is called by both internal and external tests.
|
|
||||||
func APIbenchmark() error {
|
|
||||||
testEndPt := os.Getenv("DOCUMIZEAPI") //e.g. "http://localhost:5002"
|
|
||||||
testAuth := os.Getenv("DOCUMIZEAUTH") //e.g. "demo1:jim@davidson.com:demo123"
|
|
||||||
c, err := documize.NewClient(testEndPt, testAuth) // should work
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
folders, err := c.GetNamedFolderIDs("Test")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if len(folders) == 0 {
|
|
||||||
return errors.New("no Test folder for test user")
|
|
||||||
}
|
|
||||||
testFolder := folders[0]
|
|
||||||
_ = testFolder
|
|
||||||
// TODO add benchmark code
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,339 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
var testFileName string
|
|
||||||
|
|
||||||
func setupTestFile() error {
|
|
||||||
testFileName = os.TempDir()
|
|
||||||
if !strings.HasSuffix(testFileName, string(os.PathSeparator)) {
|
|
||||||
testFileName += string(os.PathSeparator)
|
|
||||||
}
|
|
||||||
testFileName += "TESTDATA.html"
|
|
||||||
return ioutil.WriteFile(testFileName, []byte(`
|
|
||||||
<!DOCTYPE html>
|
|
||||||
<html>
|
|
||||||
<head>
|
|
||||||
<title>TESTDATA Title</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>TESTDATA Heading</h1>
|
|
||||||
<p>TESTDATA paragraph.</p>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
`), os.ModePerm)
|
|
||||||
}
|
|
||||||
|
|
||||||
// APItest is the main entry point for the test code, it is called by both internal and external tests.
|
|
||||||
func APItest(t *testing.T) {
|
|
||||||
setupTestFile()
|
|
||||||
defer os.Remove(testFileName) // ignore error
|
|
||||||
|
|
||||||
c, err := auth(t)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if c == nil {
|
|
||||||
t.Error("unable to log-in to Documize, nil pointer returned")
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Logf("INFO: Auth client=%#v\n", *c)
|
|
||||||
|
|
||||||
testMeta(t, c)
|
|
||||||
|
|
||||||
// create a testing folder here, with a defer to remove it
|
|
||||||
myFolder := &entity.Label{
|
|
||||||
Name: "Folder created during Go unit tests",
|
|
||||||
// OrgID is set by the endpoint
|
|
||||||
UserID: c.Auth.User.RefID,
|
|
||||||
Type: 2, // only the user can see it
|
|
||||||
}
|
|
||||||
err = c.AddFolder(myFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Error("c.AddFolder():", err)
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// find a folder to move the deleted myFolder's contents to
|
|
||||||
fids, err := c.GetNamedFolderIDs("Test")
|
|
||||||
if err != nil {
|
|
||||||
t.Error("c.GetNamedFolderIDs():", err)
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
if len(fids) == 0 {
|
|
||||||
t.Error("can't find a folder named Test")
|
|
||||||
t.Fail()
|
|
||||||
}
|
|
||||||
reserveFolder := fids[0] // use the 1st we found
|
|
||||||
defer func() {
|
|
||||||
err = c.RemoveFolder(myFolder.RefID, reserveFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
testFolder := myFolder.RefID
|
|
||||||
if testFolder == "" {
|
|
||||||
t.Error("myFolder.RefID is empty!")
|
|
||||||
t.Fail()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
t.Log("INFO: myFolder.RefID='" + testFolder + "'")
|
|
||||||
|
|
||||||
testFile := loadFile(c, t, testFolder, testFileName)
|
|
||||||
testData := loadData(c, t, testFolder)
|
|
||||||
|
|
||||||
testPages(t, c, testFolder, testFile, testData)
|
|
||||||
|
|
||||||
testFolderAPI(t, c, testFolder, testFile)
|
|
||||||
testSearch(t, c, testFolder, testFile)
|
|
||||||
testDownload(t, c, testFolder, testFile)
|
|
||||||
|
|
||||||
testGetUpdDocument(t, c, testFile, testData)
|
|
||||||
testGetDocumentMeta(t, c, testFile, testData)
|
|
||||||
testDocAttachments(t, c, testFile, testData)
|
|
||||||
testTemplates(t, c, testFolder, testFile, testData)
|
|
||||||
testDelete(t, c, testFile, testData)
|
|
||||||
|
|
||||||
testOrg(t, c)
|
|
||||||
testFolders(t, c, myFolder)
|
|
||||||
testUsers(t, c)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func testOrg(t *testing.T, c *documize.Client) {
|
|
||||||
orgs, err := c.GetOrganizations()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: organizations = %#v", orgs)
|
|
||||||
}
|
|
||||||
if len(orgs) > 0 {
|
|
||||||
err = c.UpdateOrganization(&orgs[0])
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
// TODO actually check that the update did what we expected
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testGetUpdDocument(t *testing.T, c *documize.Client, testFile, testData string) {
|
|
||||||
var err error
|
|
||||||
doc, err := c.GetDocument(testFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if doc.RefID != testFile {
|
|
||||||
t.Error("wrong RefId got", doc.RefID, "want", testFile)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.UpdateDocument(doc)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
// NOTE updates to unknown documents do not generate errors
|
|
||||||
|
|
||||||
docData, err := c.GetDocument(testData)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if docData.BaseEntity.RefID != testData {
|
|
||||||
t.Error("wrong RefID want", testData, "got", docData.RefID)
|
|
||||||
}
|
|
||||||
if _, err = c.GetDocument(""); err == nil {
|
|
||||||
t.Error("GetDocument did not error on empty DocumentID")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: GetDocument emptyDocID msg:", err)
|
|
||||||
}
|
|
||||||
if _, err = c.GetDocument("XXXXXXXXXXXXX"); err == nil {
|
|
||||||
t.Error("GetDocument did not error on bad DocumentID")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: GetDocument badDocID msg:", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testGetDocumentMeta(t *testing.T, c *documize.Client, testFile, testData string) {
|
|
||||||
var err error
|
|
||||||
docM, err := c.GetDocumentMeta(testFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if len(docM.Editors) < 2 { // TODO review
|
|
||||||
t.Error("wrong number of editors expected >=2 got", len(docM.Editors))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
docDataM, err := c.GetDocumentMeta(testData)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if len(docDataM.Editors) != 2 { // TODO review
|
|
||||||
t.Error("wrong number of editors expected 0 got", len(docDataM.Editors))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if _, err = c.GetDocumentMeta(""); err == nil {
|
|
||||||
t.Error("GetDocumentMeta did not error on empty DocumentID")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: GetDocumentMeta emptyDocID msg:", err)
|
|
||||||
}
|
|
||||||
/* TODO reivew
|
|
||||||
if _, err = c.GetDocumentMeta("XXXXXXXXXXXXX"); err == nil {
|
|
||||||
t.Error("GetDocumentMeta did not error on bad DocumentID")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: GetDocumentMeta badDocID msg:", err)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
func testDelete(t *testing.T, c *documize.Client, testFile, testData string) {
|
|
||||||
var err error
|
|
||||||
if err = c.DeleteDocument(testFile); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if err = c.DeleteDocument(testData); err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if err = c.DeleteDocument(""); err == nil {
|
|
||||||
t.Error("DeleteDocument did not error on empty DocumentID")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Delete Doc emptyDocID msg:", err)
|
|
||||||
}
|
|
||||||
/* TODO reivew
|
|
||||||
if err = c.DeleteDocument("XXXXXXXXXXXXX"); err == nil {
|
|
||||||
t.Error("DeleteDocument did not error on bad DocumentID")
|
|
||||||
} else {
|
|
||||||
t.Log("Delete Doc badDocID msg:", err)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
func testMeta(t *testing.T, c *documize.Client) {
|
|
||||||
sitemap, err := c.GetSitemap()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if sitemap == nil {
|
|
||||||
t.Error("sitemap []byte is nil")
|
|
||||||
} else {
|
|
||||||
if !bytes.Contains(sitemap, []byte("http://www.sitemaps.org/schemas/sitemap")) {
|
|
||||||
t.Errorf("Incorrect Site Map: %#v", string(sitemap))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
robots, err := c.GetRobots()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if robots == nil {
|
|
||||||
t.Error("robots []byte is nil")
|
|
||||||
} else {
|
|
||||||
if !bytes.HasPrefix(robots, []byte("User-agent:")) {
|
|
||||||
t.Errorf("Incorrect Robots data: %#v", string(robots))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testFolderAPI(t *testing.T, c *documize.Client, testFolder, testFile string) {
|
|
||||||
|
|
||||||
_, err := c.GetDocumentsByFolder("")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("did not error on blank folder ID")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: ", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
docs, err := c.GetDocumentsByFolder(testFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, doc := range docs {
|
|
||||||
if doc.RefID == testFile {
|
|
||||||
goto foundDoc
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Error("Unable to find " + testFile + " in the test folder")
|
|
||||||
return
|
|
||||||
foundDoc:
|
|
||||||
|
|
||||||
docs2, err := c.GetDocumentsByFolder("什么都没有")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("did not error on bad folder ID for GetDocumentsByFolder")
|
|
||||||
if len(docs2) != 0 {
|
|
||||||
t.Error("found documents in folder where there should be none")
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: " + err.Error())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testSearch(t *testing.T, c *documize.Client, testFolder, testFile string) {
|
|
||||||
|
|
||||||
// NOTE: search does not error on blank search term
|
|
||||||
srch, err := c.SearchDocuments("TESTDATA")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
for _, doc := range srch {
|
|
||||||
//t.Logf("DEBUG search %#v", doc)
|
|
||||||
if doc.DocumentID == testFile {
|
|
||||||
goto foundSrch
|
|
||||||
}
|
|
||||||
}
|
|
||||||
t.Error("Unable to find " + testFileName + " in the search list")
|
|
||||||
foundSrch:
|
|
||||||
|
|
||||||
srch2, err := c.SearchDocuments("石磊先生是谁?")
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if len(srch2) != 0 {
|
|
||||||
t.Error("found documents in search where there should be none")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testDownload(t *testing.T, c *documize.Client, testFolder, testFile string) {
|
|
||||||
|
|
||||||
nam, content, err := c.GetDocumentAsDocx(testFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
/* TODO
|
|
||||||
if !strings.HasPrefix(nam, "addpage") || len(content) < 10000 {
|
|
||||||
t.Error(" docAsDocx not as expected", nam, len(content))
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
nam, content, err = c.GetDocumentAsDocx("XXXXXXXXXX")
|
|
||||||
if err == nil {
|
|
||||||
t.Errorf("did not error on bad document id for get doc as docx, name: %s len(%d)",
|
|
||||||
nam, len(content))
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: get doc as docx bad doc id msg:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,107 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testDocAttachments(t *testing.T, c *documize.Client, testFile, testData string) {
|
|
||||||
atts, err := c.GetAttachments(testData)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
for a := range atts {
|
|
||||||
err = c.GetAttachmentData(&atts[a])
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if atts[a].Filename == "test.txt" {
|
|
||||||
if string(atts[a].Data) != "This is a test text file.\n" {
|
|
||||||
t.Error("incorrect content to attachment")
|
|
||||||
}
|
|
||||||
goto foundAtt
|
|
||||||
}
|
|
||||||
//t.Logf("DEBUG %d atts= %#v ; err=%v; data=%s", a, atts[a], err,string(atts[a].Data))
|
|
||||||
}
|
|
||||||
t.Error("Attachment test.txt not found")
|
|
||||||
foundAtt:
|
|
||||||
dingbat := "dingbat\n"
|
|
||||||
err = c.AddAttachment(testFile, "dingbat.txt", []byte(dingbat))
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
atts, err = c.GetAttachments(testFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if len(atts) != 1 {
|
|
||||||
t.Error("should be exactly 1 attachment")
|
|
||||||
} else {
|
|
||||||
err = c.GetAttachmentData(&atts[0])
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if string(atts[0].Data) != dingbat {
|
|
||||||
t.Error("Wrong data in attachement")
|
|
||||||
}
|
|
||||||
err = c.DeleteAttachment(&atts[0])
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
atts, err = c.GetAttachments(testFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if len(atts) != 0 {
|
|
||||||
t.Error("should be no attachments")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// errors
|
|
||||||
atts, err = c.GetAttachments("XXXXXXX")
|
|
||||||
if len(atts) != 0 {
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Get attachments of unknown file did not error")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: get attachments of unknown file msg:", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* TODO improve failure modes
|
|
||||||
att := &entity.Attachment{}
|
|
||||||
err = c.GetAttachmentData(att)
|
|
||||||
if len(att.Data) > 0 {
|
|
||||||
if err == nil {
|
|
||||||
t.Error("Get attachment data of blank file did not error")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: get attachments of blank file msg:", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = c.AddAttachment("YYYYYYYYYYYY", "dingbat.txt", []byte(dingbat))
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Did not error adding attachment to bad file id")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: add attachment to unknown file msg:", err)
|
|
||||||
}
|
|
||||||
err = c.DeleteAttachment(&entity.Attachment{})
|
|
||||||
if err != nil {
|
|
||||||
t.Error("Did not error deleting attachment of blank data")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: delete attachment to blank file msg:", err)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
|
@ -1,98 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
// auth provides authorization tests to be run locally or from the main Documize repo.
|
|
||||||
func auth(t *testing.T) (*documize.Client, error) {
|
|
||||||
|
|
||||||
testEndPt := os.Getenv("DOCUMIZEAPI") //e.g. "http://localhost:5002"
|
|
||||||
testAuth := os.Getenv("DOCUMIZEAUTH") //e.g. "demo1:jim@davidson.com:demo123"
|
|
||||||
|
|
||||||
testCreds(t, testEndPt, testAuth)
|
|
||||||
testEndpoint(t, testEndPt, testAuth)
|
|
||||||
|
|
||||||
//t.Log("Auth", testEndPt, testAuth)
|
|
||||||
|
|
||||||
c, err := documize.NewClient(testEndPt, testAuth) // should work
|
|
||||||
if err == nil && c == nil {
|
|
||||||
err = errors.New("unable to authorize, new client nil")
|
|
||||||
}
|
|
||||||
return c, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCreds(t *testing.T, testEndPt, testAuth string) {
|
|
||||||
_, err := documize.NewClient(testEndPt, "")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("ExtTestAuth did not error on empty auth string ")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Empty Auth string error:", err)
|
|
||||||
}
|
|
||||||
_, err = documize.NewClient(testEndPt, "AAA:BBB")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("ExtTestAuth did not error on AAA:BBB auth string ")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Malfomed auth string error:", err)
|
|
||||||
}
|
|
||||||
credentials := strings.SplitN(testAuth, ":", 3)
|
|
||||||
if len(credentials) == 3 {
|
|
||||||
base := []string{"XXX", "YYY", "ZZZ"}
|
|
||||||
for i := range credentials {
|
|
||||||
ta := make([]string, 3)
|
|
||||||
copy(ta, base)
|
|
||||||
for j := range ta {
|
|
||||||
if j != i { // make sure one of the three is wrong
|
|
||||||
ta[j] = credentials[j]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
as := strings.Join(ta, ":")
|
|
||||||
//t.Log(as)
|
|
||||||
if credentials[i] != "" { // to avoid the case where the sub-domain is empty
|
|
||||||
_, err = documize.NewClient(testEndPt, as)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("ExtTestAuth did not error on bad auth string: ", as)
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Bad component to auth string error:", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func testEndpoint(t *testing.T, testEndPt, testAuth string) {
|
|
||||||
_, err := documize.NewClient("", testAuth)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("ExtTestAuth did not error on empty end point")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Empty end-point error:", err)
|
|
||||||
}
|
|
||||||
_, err = documize.NewClient("XXXXX", testAuth)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("ExtTestAuth did not error on bad end point")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Bad end point error:", err)
|
|
||||||
}
|
|
||||||
_, err = documize.NewClient("http://XXXXXYYYYYYZZZZZZ.com", testAuth)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("ExtTestAuth did not error on invalid end point")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Invalid end point error:", err)
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,57 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testFolders(t *testing.T, c *documize.Client, myFolder *entity.Label) {
|
|
||||||
|
|
||||||
perms, err := c.GetFolderPermissions(myFolder.RefID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
//t.Logf("INFO: folder perms %#v", *perms)
|
|
||||||
}
|
|
||||||
err = c.SetFolderPermissions(myFolder.RefID, "test message", perms)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
//t.Logf("INFO: set empty folder perms")
|
|
||||||
}
|
|
||||||
|
|
||||||
fv, err := c.GetFoldersVisibility()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
//t.Logf("INFO: folder vis %#v", fv)
|
|
||||||
_ = fv
|
|
||||||
}
|
|
||||||
|
|
||||||
myFolder.Name += " - Modified"
|
|
||||||
err = c.UpdateFolder(myFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fi, err := c.GetFolder(myFolder.RefID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
//t.Logf("INFO: folder info %#v", fi)
|
|
||||||
_ = fi
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,92 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
api "github.com/documize/community/core/convapi"
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
// loadData provides data-loading tests to be run locally or from the main Documize repo.
|
|
||||||
func loadData(c *documize.Client, t *testing.T, testFolder string) string {
|
|
||||||
ret, err := c.LoadData(testFolder, "LoadDataTest", &api.DocumentConversionResponse{
|
|
||||||
//Err error
|
|
||||||
PagesHTML: []byte{}, // If empty, use Pages
|
|
||||||
Pages: []api.Page{
|
|
||||||
{
|
|
||||||
Level: 1, // uint64 // overall document is level 1, <H1> => level 2
|
|
||||||
Title: "Test Data Title top", // string
|
|
||||||
Body: []byte("This is the body of page 0"), // []byte
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Level: 2, // uint64 // overall document is level 1, <H1> => level 2
|
|
||||||
Title: "Test Data Title second", // string
|
|
||||||
Body: []byte("This is the body of page 1"), // []byte
|
|
||||||
},
|
|
||||||
},
|
|
||||||
EmbeddedFiles: []api.EmbeddedFile{
|
|
||||||
{
|
|
||||||
ID: "TestID1",
|
|
||||||
Type: "txt",
|
|
||||||
Name: "test.txt", // do not change, used in exttest
|
|
||||||
Data: []byte("This is a test text file.\n"), // do not change, used in exttest
|
|
||||||
},
|
|
||||||
{
|
|
||||||
ID: "TestID2",
|
|
||||||
Type: "go",
|
|
||||||
Name: "blob.go",
|
|
||||||
Data: []byte("// Package blob is a test go file.\npackage blob\n"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Excerpt: "Ext Test Load Data - Excerpt",
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = c.LoadData(testFolder, "LoadDataTest", &api.DocumentConversionResponse{
|
|
||||||
//Err error
|
|
||||||
PagesHTML: []byte{}, // If empty, use Pages
|
|
||||||
Pages: []api.Page{
|
|
||||||
{
|
|
||||||
Level: 1, // overall document is level 1, <H1> => level 2
|
|
||||||
Title: "Test Data Title top",
|
|
||||||
Body: []byte("This is the body of page 0"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
EmbeddedFiles: []api.EmbeddedFile{
|
|
||||||
{
|
|
||||||
ID: "TestID1",
|
|
||||||
Type: "txt",
|
|
||||||
Name: "test", // wrong, does not have correct extension
|
|
||||||
Data: []byte("This is a test text file.\n"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Excerpt: "Ext Test Load Data - Excerpt",
|
|
||||||
})
|
|
||||||
if err == nil {
|
|
||||||
t.Error("data load did not error on bad embedded file name")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Bad embedded file name error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = c.LoadData(testFolder, "LoadDataTest", &api.DocumentConversionResponse{})
|
|
||||||
if err == nil {
|
|
||||||
t.Error("data load did not error on no pages ")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: No pages error:", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret.BaseEntity.RefID
|
|
||||||
}
|
|
|
@ -1,45 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
// loadFile provides file load tests to be run locally or from the main Documize repo.
|
|
||||||
func loadFile(c *documize.Client, t *testing.T, testFolder, testFileName string) string {
|
|
||||||
docID := "" //NOT-FOUND
|
|
||||||
ret, err := c.LoadFile(testFolder, testFileName)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
if ret == nil {
|
|
||||||
t.Error("nil pointer returned for LoadFile")
|
|
||||||
} else {
|
|
||||||
docID = ret.BaseEntity.RefID
|
|
||||||
}
|
|
||||||
_, err = c.LoadFile("XXX", testFileName)
|
|
||||||
if err == nil {
|
|
||||||
t.Error("did not error on bad folder")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Bad folder error:", err)
|
|
||||||
}
|
|
||||||
_, err = c.LoadFile(testFolder, "XXX")
|
|
||||||
if err == nil {
|
|
||||||
t.Error("did not error on bad file name")
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Bad file name error:", err)
|
|
||||||
}
|
|
||||||
return docID
|
|
||||||
}
|
|
|
@ -1,114 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/endpoint/models"
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testPages(t *testing.T, c *documize.Client, testFolder, testFile, testData string) {
|
|
||||||
|
|
||||||
lastSeq := 0.0
|
|
||||||
dp, err := c.GetDocumentPages(testFile)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
var pageIDs []string
|
|
||||||
for _, v := range dp {
|
|
||||||
pageIDs = append(pageIDs, v.RefID)
|
|
||||||
if v.Sequence > lastSeq {
|
|
||||||
lastSeq = v.Sequence
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pageIDlist := strings.Join(pageIDs, ",")
|
|
||||||
if dpb, err2 := c.GetDocumentPagesBatch(testFile, pageIDlist); err != nil {
|
|
||||||
t.Error(err2)
|
|
||||||
} else {
|
|
||||||
for k, v := range dp {
|
|
||||||
if v.Body != dpb[k].Body {
|
|
||||||
t.Errorf("page %d from GetDocumentPages != from GetDocumentPagesBatch", k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const numPages = 100
|
|
||||||
pagesAdded := make([]*entity.Page, 0, numPages)
|
|
||||||
for i := 0; i < numPages; i++ {
|
|
||||||
lastSeq += 1000.0
|
|
||||||
pg := entity.Page{
|
|
||||||
DocumentID: testFile,
|
|
||||||
Level: 1,
|
|
||||||
Title: "AddPage() title " + fmt.Sprintf("%d", i),
|
|
||||||
Body: "AddPage() body " + fmt.Sprintf("%d", i),
|
|
||||||
ContentType: "wysiwyg",
|
|
||||||
Sequence: lastSeq,
|
|
||||||
}
|
|
||||||
if newPg, err2 := c.AddDocumentPage(testFile, &pg); err != nil {
|
|
||||||
t.Error(err2)
|
|
||||||
} else {
|
|
||||||
pagesAdded = append(pagesAdded, newPg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(pagesAdded) < 2 {
|
|
||||||
t.Error("Less than two pages added, cannot continue with pages test")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.ChangeDocumentPageLevel(testFile, &[]models.PageLevelRequestModel{
|
|
||||||
{PageID: pagesAdded[0].RefID, Level: int(pagesAdded[0].Level + 42)},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.ChangeDocumentPageSequence(testFile, &[]models.PageSequenceRequestModel{
|
|
||||||
{PageID: pagesAdded[0].RefID, Sequence: pagesAdded[0].Sequence - 1.0},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
pagesAdded[0].Body += " - Modified!"
|
|
||||||
err = c.UpdateDocumentPage(pagesAdded[0])
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = c.DeleteDocumentPage(testFile, pagesAdded[0].RefID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Deleted single doc page", pagesAdded[0].RefID)
|
|
||||||
}
|
|
||||||
|
|
||||||
delList := []string{pagesAdded[1].RefID}
|
|
||||||
for k, v := range pagesAdded {
|
|
||||||
if k > 1 {
|
|
||||||
delList = append(delList, v.RefID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = c.DeleteDocumentPages(testFile, delList)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: Deleted multiple doc pages:", len(delList))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,64 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testTemplates(t *testing.T, c *documize.Client, testFolder, testFile, testData string) {
|
|
||||||
|
|
||||||
temps, err := c.GetTemplates(true)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if len(temps) == 0 {
|
|
||||||
t.Log("INFO: no stock templates found in the database")
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: testing with stock template %#v", temps[0])
|
|
||||||
docID, errStart := c.StartDocumentFromTemplate(true, temps[0].ID, testFolder)
|
|
||||||
if errStart != nil {
|
|
||||||
t.Error(errStart)
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: created document", docID)
|
|
||||||
err = c.DeleteDocument(docID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
temps, err = c.GetTemplates(false)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
if len(temps) == 0 {
|
|
||||||
t.Log("INFO: no saved templates found in the database")
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: testing with saved template %#v", temps[0])
|
|
||||||
docID, err := c.StartDocumentFromTemplate(false, temps[0].ID, testFolder)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Log("INFO: created document", docID)
|
|
||||||
err = c.DeleteDocument(docID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,63 +0,0 @@
|
||||||
// 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 exttest
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
"github.com/documize/community/sdk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func testUsers(t *testing.T, c *documize.Client) {
|
|
||||||
|
|
||||||
usrs, err := c.GetUsers()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: users info %#v", usrs)
|
|
||||||
}
|
|
||||||
usr, err := c.GetUserInfo()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: this user info %#v", usr)
|
|
||||||
}
|
|
||||||
perms, err := c.GetUserFolderPermissions()
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: testing user folder perms %#v", len(*perms))
|
|
||||||
}
|
|
||||||
|
|
||||||
testUser := &entity.User{
|
|
||||||
Firstname: "TestFirstname",
|
|
||||||
Lastname: "TestLastname",
|
|
||||||
Email: "tt@a.b",
|
|
||||||
Admin: false,
|
|
||||||
}
|
|
||||||
err = c.AddUser(testUser)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
} else {
|
|
||||||
t.Logf("INFO: created test user %#v", *testUser)
|
|
||||||
err = c.UpdateUser(testUser)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
err = c.DeleteUser(testUser.BaseEntity.RefID)
|
|
||||||
if err != nil {
|
|
||||||
t.Error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
251
sdk/folders.go
251
sdk/folders.go
|
@ -1,251 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/endpoint/models"
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetFolders returns the folders that the current user can see.
|
|
||||||
func (c *Client) GetFolders() ([]entity.Label, error) {
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
var folders []entity.Label
|
|
||||||
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&folders)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return folders, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetNamedFolderIDs returns those folder IDs with the given name (folder names are not unique).
|
|
||||||
func (c *Client) GetNamedFolderIDs(name string) ([]string, error) {
|
|
||||||
ret := make([]string, 0, 2)
|
|
||||||
|
|
||||||
Folders, err := c.GetFolders()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, f := range Folders {
|
|
||||||
if name == f.Name {
|
|
||||||
ret = append(ret, f.RefID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFoldersVisibility returns the visibility of folders that the current user can see.
|
|
||||||
func (c *Client) GetFoldersVisibility() ([]entity.Label, error) {
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders?filter=viewers", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
folders := make([]entity.Label, 0, 10)
|
|
||||||
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&folders)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return folders, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFolder returns the documents in the given folder that the current user can see.
|
|
||||||
func (c *Client) GetFolder(folderID string) (*entity.Label, error) {
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders/"+folderID, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
var folder entity.Label
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, &folder)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &folder, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFolderPermissions returns the given user's permissions.
|
|
||||||
func (c *Client) GetFolderPermissions(folderID string) (*[]entity.LabelRole, error) {
|
|
||||||
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/folders/"+folderID+"/permissions", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
folderPerm := make([]entity.LabelRole, 0, 6)
|
|
||||||
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&folderPerm)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &folderPerm, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetFolderPermissions sets the given user's permissions.
|
|
||||||
func (c *Client) SetFolderPermissions(folderID, msg string, perms *[]entity.LabelRole) error {
|
|
||||||
frm := new(models.FolderRolesModel)
|
|
||||||
frm.Message = msg
|
|
||||||
frm.Roles = *perms
|
|
||||||
b, err := json.Marshal(frm)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/folders/"+folderID+"/permissions", bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err = ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddFolder adds the given folder for the current user.
|
|
||||||
// Fields added by the host are added to the folder structure referenced.
|
|
||||||
func (c *Client) AddFolder(fldr *entity.Label) error {
|
|
||||||
b, err := json.Marshal(fldr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/folders", bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err = ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, fldr)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateFolder changes the folder info the given folder for the current user, returning the changed version in the referenced folder structure.
|
|
||||||
func (c *Client) UpdateFolder(fldr *entity.Label) error {
|
|
||||||
b, err := json.Marshal(fldr)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/folders/"+fldr.RefID, bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err = ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, fldr)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// RemoveFolder removes the given folder and moves its contents to another.
|
|
||||||
func (c *Client) RemoveFolder(folderID, moveToID string) error {
|
|
||||||
req, err := http.NewRequest("DELETE", c.BaseURL+"/api/folders/"+folderID+"/move/"+moveToID, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,47 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
api "github.com/documize/community/core/convapi"
|
|
||||||
)
|
|
||||||
|
|
||||||
// LoadData uploads and converts the raw data comprising a Documize document into Documize, returning a fileID and error.
|
|
||||||
func (c *Client) LoadData(folderID, docName string, docData *api.DocumentConversionResponse) (*entity.Document, error) {
|
|
||||||
if len(docData.PagesHTML) == 0 && len(docData.Pages) == 0 {
|
|
||||||
return nil, errors.New("no data to load") // NOTE attachements must have a base document
|
|
||||||
}
|
|
||||||
for _, att := range docData.EmbeddedFiles {
|
|
||||||
if !strings.HasSuffix(strings.ToLower(att.Name), "."+strings.ToLower(att.Type)) {
|
|
||||||
return nil, errors.New("attachment " + att.Name + " does not have the extention " + att.Type)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buf, err := json.Marshal(*docData)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cv, err := c.upload(folderID, docName+".documizeapi", bytes.NewReader(buf))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
//cv, err := c.convert(folderID, job, nil)
|
|
||||||
//if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
//}
|
|
||||||
return cv, nil
|
|
||||||
}
|
|
131
sdk/loadfile.go
131
sdk/loadfile.go
|
@ -1,131 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"io/ioutil"
|
|
||||||
"mime/multipart"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (c *Client) upload(folderID, fileName string, fileReader io.Reader) (*entity.Document, error) {
|
|
||||||
|
|
||||||
bodyBuf := &bytes.Buffer{}
|
|
||||||
bodyWriter := multipart.NewWriter(bodyBuf)
|
|
||||||
|
|
||||||
_, fn := path.Split(fileName)
|
|
||||||
fileWriter, err := bodyWriter.CreateFormFile("attachment", fn)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
_, err = io.Copy(fileWriter, fileReader)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
contentType := bodyWriter.FormDataContentType()
|
|
||||||
err = bodyWriter.Close()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST",
|
|
||||||
c.BaseURL+"/api/import/folder/"+folderID,
|
|
||||||
bodyBuf)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
req.Header.Set("Content-Type", contentType)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
var du entity.Document
|
|
||||||
|
|
||||||
msg, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(msg, &du)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(msg)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return &du, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
func (c *Client) convert(folderID string, du *endpoint.DocumentUploadModel, cjr *api.ConversionJobRequest) (*endpoint.DocumentConversionModel, error) {
|
|
||||||
|
|
||||||
if cjr == nil {
|
|
||||||
cjr = &api.ConversionJobRequest{}
|
|
||||||
}
|
|
||||||
|
|
||||||
buf, err := json.Marshal(*cjr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST",
|
|
||||||
c.BaseURL+"/api/convert/folder/"+folderID+"/"+du.JobID,
|
|
||||||
bytes.NewReader(buf))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
|
|
||||||
var dc endpoint.DocumentConversionModel
|
|
||||||
|
|
||||||
msg, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(msg, &dc)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(msg)))
|
|
||||||
}
|
|
||||||
|
|
||||||
return &dc, nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// LoadFile uploads and converts a file into Documize, returning a fileID and error.
|
|
||||||
func (c *Client) LoadFile(folderID, targetFile string) (*entity.Document, error) {
|
|
||||||
file, err := os.Open(targetFile) // For read access.
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
cv, err := c.upload(folderID, targetFile, file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
//cv, err := c.convert(folderID, job, nil)
|
|
||||||
//if err != nil {
|
|
||||||
// return nil, err
|
|
||||||
//}
|
|
||||||
return cv, nil
|
|
||||||
}
|
|
75
sdk/meta.go
75
sdk/meta.go
|
@ -1,75 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetSiteMeta returns the site information based on the sub-domain of the URL called.
|
|
||||||
/* TODO - cant get sub-domain easily in test environment
|
|
||||||
func (c *Client) GetSiteMeta() (*entity.SiteMeta, error) {
|
|
||||||
tgt := c.BaseURL + "/api/public/meta"
|
|
||||||
req, err := http.NewRequest("GET", tgt, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
var sm entity.SiteMeta
|
|
||||||
err = json.Unmarshal(b, &sm)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return &sm, nil
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// GetSitemap returns the site map information based on the domain supplied in the URL called.
|
|
||||||
func (c *Client) GetSitemap() ([]byte, error) {
|
|
||||||
tgt := c.BaseURL + "/sitemap.xml"
|
|
||||||
return c.getFile(tgt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetRobots returns the site map information based on the domain supplied in the URL called.
|
|
||||||
func (c *Client) GetRobots() ([]byte, error) {
|
|
||||||
tgt := c.BaseURL + "/robots.txt"
|
|
||||||
return c.getFile(tgt)
|
|
||||||
}
|
|
||||||
|
|
||||||
// getFile is an internal function to avoid code duplication when fetching the plain output of the GET.
|
|
||||||
func (c *Client) getFile(tgt string) ([]byte, error) {
|
|
||||||
req, err := http.NewRequest("GET", tgt, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return b, nil
|
|
||||||
}
|
|
|
@ -1,81 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetOrganizations returns the user's organizations.
|
|
||||||
func (c *Client) GetOrganizations() ([]entity.Organization, error) {
|
|
||||||
ret := make([]entity.Organization, 0, len(c.Auth.User.Accounts))
|
|
||||||
for orgNum := range c.Auth.User.Accounts {
|
|
||||||
url := c.BaseURL + "/api/organizations/" + c.Auth.User.Accounts[orgNum].OrgID
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
var m entity.Organization
|
|
||||||
err = json.Unmarshal(b, &m)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ret = append(ret, m)
|
|
||||||
}
|
|
||||||
return ret, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateOrganization returns the user's organization information.
|
|
||||||
func (c *Client) UpdateOrganization(org *entity.Organization) error {
|
|
||||||
url := c.BaseURL + "/api/organizations/" + org.RefID
|
|
||||||
bod, err := json.Marshal(org)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("PUT", url, bytes.NewReader(bod))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
224
sdk/pages.go
224
sdk/pages.go
|
@ -1,224 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/endpoint/models"
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetDocumentPages returns all the pages in a document.
|
|
||||||
func (c *Client) GetDocumentPages(documentID string) ([]entity.Page, error) {
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/documents/"+documentID+"/pages", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
pages := make([]entity.Page, 0, 12)
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&pages)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return pages, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetDocumentPagesBatch returns those pages in a document whose RefIDs are in a comma-separated list.
|
|
||||||
func (c *Client) GetDocumentPagesBatch(documentID, pageIDlist string) ([]entity.Page, error) {
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/batch", strings.NewReader(pageIDlist))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
pages := make([]entity.Page, 0, 12)
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&pages)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return pages, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddDocumentPage adds the given page into the indicated document.
|
|
||||||
func (c *Client) AddDocumentPage(documentID string, pg *entity.Page) (*entity.Page, error) {
|
|
||||||
|
|
||||||
pageJSON, err := json.Marshal(pg)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages", bytes.NewReader(pageJSON))
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
var page entity.Page
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&page)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &page, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteDocumentPage deletes the given page from the indicated document.
|
|
||||||
func (c *Client) DeleteDocumentPage(documentID, pageID string) error {
|
|
||||||
req, err := http.NewRequest("DELETE", c.BaseURL+"/api/documents/"+documentID+"/pages/"+pageID, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
res, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(res)) {
|
|
||||||
return errors.New(trimErrors(string(res)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteDocumentPages deletes the given pageIDs in a slice from the indicated document.
|
|
||||||
func (c *Client) DeleteDocumentPages(documentID string, pageIDlist []string) error {
|
|
||||||
model := make([]models.PageLevelRequestModel, len(pageIDlist))
|
|
||||||
for k := range pageIDlist {
|
|
||||||
model[k].PageID = pageIDlist[k]
|
|
||||||
}
|
|
||||||
modelJSON, err := json.Marshal(&model)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/unused", bytes.NewReader(modelJSON))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
res, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(res)) {
|
|
||||||
return errors.New(trimErrors(string(res)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateDocumentPage updates the given page from the indicated document.
|
|
||||||
func (c *Client) UpdateDocumentPage(pg *entity.Page) error {
|
|
||||||
pgJSON, err := json.Marshal(pg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/documents/"+pg.DocumentID+"/pages/"+pg.RefID, bytes.NewReader(pgJSON))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
res, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(res)) {
|
|
||||||
return errors.New(trimErrors(string(res)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ChangeDocumentPageLevel sets the levels of the pages in the PageLevelRequestModel for the given document.
|
|
||||||
func (c *Client) ChangeDocumentPageLevel(documentID string, plrm *[]models.PageLevelRequestModel) error {
|
|
||||||
b, err := json.Marshal(plrm)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/level", bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
diff, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(diff)) {
|
|
||||||
return errors.New(trimErrors(string(diff)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// ChangeDocumentPageSequence sets the sequences of the pages in the PageSequenceRequestModel for the given document.
|
|
||||||
func (c *Client) ChangeDocumentPageSequence(documentID string, psrm *[]models.PageSequenceRequestModel) error {
|
|
||||||
b, err := json.Marshal(psrm)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/documents/"+documentID+"/pages/sequence", bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
diff, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(diff)) {
|
|
||||||
return errors.New(trimErrors(string(diff)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
|
@ -1,91 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetTemplates returns the available templates; from the stock templates if useStock is set.
|
|
||||||
func (c *Client) GetTemplates(useStock bool) (temps []entity.Template, err error) {
|
|
||||||
url := fmt.Sprintf("%s/api/templates",
|
|
||||||
c.BaseURL)
|
|
||||||
if useStock {
|
|
||||||
url += "/stock"
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, errDo := c.Client.Do(req)
|
|
||||||
if errDo != nil {
|
|
||||||
return nil, errDo
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, errRA := ioutil.ReadAll(resp.Body)
|
|
||||||
if errRA != nil {
|
|
||||||
return nil, errRA
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, &temps)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return temps, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// StartDocumentFromTemplate returns the documentID created in the given folderID from the given templateID,
|
|
||||||
// using either a stock template if isStock==true or a saved template.
|
|
||||||
func (c *Client) StartDocumentFromTemplate(isStock bool, templateID, folderID string) (DocumentID string, err error) {
|
|
||||||
url := fmt.Sprintf("%s/api/templates/%s/folder/%s?type=",
|
|
||||||
c.BaseURL, templateID, folderID)
|
|
||||||
if isStock {
|
|
||||||
url += "stock"
|
|
||||||
} else {
|
|
||||||
url += "saved"
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return "", errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
var model entity.Document
|
|
||||||
err = json.Unmarshal(b, &model)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
if model.RefID == "" {
|
|
||||||
return "", errors.New("empty DocumentID returned")
|
|
||||||
}
|
|
||||||
return model.RefID, nil
|
|
||||||
}
|
|
172
sdk/users.go
172
sdk/users.go
|
@ -1,172 +0,0 @@
|
||||||
// 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 documize
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
|
|
||||||
"github.com/documize/community/core/api/entity"
|
|
||||||
)
|
|
||||||
|
|
||||||
// GetUsers returns the users in the user's organization.
|
|
||||||
func (c *Client) GetUsers() ([]entity.User, error) {
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/users", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
users := make([]entity.User, 0, 5)
|
|
||||||
dec := json.NewDecoder(resp.Body)
|
|
||||||
err = dec.Decode(&users)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return users, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserInfo returns the user's information.
|
|
||||||
func (c *Client) GetUserInfo() (*entity.User, error) {
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/users/"+c.Auth.User.BaseEntity.RefID, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
user := new(entity.User)
|
|
||||||
err = json.Unmarshal(b, user)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return user, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddUser adds the given user to the system.
|
|
||||||
// The version of the user record written to the database
|
|
||||||
// is written into the referenced User record.
|
|
||||||
func (c *Client) AddUser(usrp *entity.User) error {
|
|
||||||
b, err := json.Marshal(usrp)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("POST", c.BaseURL+"/api/users", bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err = ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, usrp)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// UpdateUser updates the given user, writing the changed version back into the given User structure.
|
|
||||||
func (c *Client) UpdateUser(usrp *entity.User) error {
|
|
||||||
b, err := json.Marshal(usrp)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req, err := http.NewRequest("PUT", c.BaseURL+"/api/users/"+usrp.BaseEntity.RefID, bytes.NewReader(b))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err = ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(b, usrp)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// DeleteUser deletes the given user.
|
|
||||||
func (c *Client) DeleteUser(userID string) error {
|
|
||||||
req, err := http.NewRequest("DELETE", c.BaseURL+"/api/users/"+userID, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserFolderPermissions gets the folder permissions for the current user.
|
|
||||||
func (c *Client) GetUserFolderPermissions() (*[]entity.LabelRole, error) {
|
|
||||||
req, err := http.NewRequest("GET", c.BaseURL+"/api/users/"+c.Auth.User.RefID+"/permissions", nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Add(HeaderAuthTokenName, c.Auth.Token)
|
|
||||||
resp, err := c.Client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close() // ignore error
|
|
||||||
b, err := ioutil.ReadAll(resp.Body)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if isError(string(b)) {
|
|
||||||
return nil, errors.New(trimErrors(string(b)))
|
|
||||||
}
|
|
||||||
perm := make([]entity.LabelRole, 0, 2)
|
|
||||||
err = json.Unmarshal(b, &perm)
|
|
||||||
return &perm, err
|
|
||||||
}
|
|
Loading…
Add table
Add a link
Reference in a new issue