2017-08-02 12:39:12 +01:00
|
|
|
// 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 conversion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
api "github.com/documize/community/core/convapi"
|
2018-04-20 14:38:35 +01:00
|
|
|
"github.com/documize/community/core/env"
|
2017-08-02 12:39:12 +01:00
|
|
|
"github.com/documize/community/core/request"
|
|
|
|
"github.com/documize/community/core/response"
|
|
|
|
"github.com/documize/community/core/stringutil"
|
|
|
|
"github.com/documize/community/core/uniqueid"
|
|
|
|
"github.com/documize/community/domain"
|
2018-09-27 15:14:48 +01:00
|
|
|
ls "github.com/documize/community/domain/conversion/store"
|
2017-09-18 17:53:42 +01:00
|
|
|
"github.com/documize/community/domain/permission"
|
2018-03-28 14:25:37 +01:00
|
|
|
indexer "github.com/documize/community/domain/search"
|
2018-09-27 15:14:48 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2017-08-02 12:39:12 +01:00
|
|
|
"github.com/documize/community/model/activity"
|
|
|
|
"github.com/documize/community/model/attachment"
|
|
|
|
"github.com/documize/community/model/audit"
|
|
|
|
"github.com/documize/community/model/doc"
|
|
|
|
"github.com/documize/community/model/page"
|
2018-04-20 14:38:35 +01:00
|
|
|
"github.com/documize/community/model/space"
|
2018-05-10 15:10:15 +01:00
|
|
|
"github.com/documize/community/model/workflow"
|
2017-08-02 12:39:12 +01:00
|
|
|
uuid "github.com/nu7hatch/gouuid"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
var storageProvider StorageProvider
|
2017-08-02 12:39:12 +01:00
|
|
|
|
|
|
|
func init() {
|
2018-09-27 15:14:48 +01:00
|
|
|
storageProvider = new(ls.LocalStorageProvider)
|
2017-08-02 12:39:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) upload(w http.ResponseWriter, r *http.Request) (string, string, string) {
|
|
|
|
method := "conversion.upload"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
spaceID := request.Param(r, "spaceID")
|
2017-08-02 12:39:12 +01:00
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
if !permission.CanUploadDocument(ctx, *h.Store, spaceID) {
|
2017-08-02 12:39:12 +01:00
|
|
|
response.WriteForbiddenError(w)
|
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// grab file
|
|
|
|
filedata, filename, err := r.FormFile("attachment")
|
|
|
|
if err != nil {
|
|
|
|
response.WriteMissingDataError(w, method, "attachment")
|
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
_, err = io.Copy(b, filedata)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate job id
|
|
|
|
newUUID, err := uuid.NewV4()
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
job := newUUID.String()
|
|
|
|
|
|
|
|
err = storageProvider.Upload(job, filename.Filename, b.Bytes())
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return "", "", ""
|
|
|
|
}
|
|
|
|
|
|
|
|
h.Runtime.Log.Info(fmt.Sprintf("Org %s (%s) [Uploaded] %s", ctx.OrgName, ctx.OrgID, filename.Filename))
|
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
return job, spaceID, ctx.OrgID
|
2017-08-02 12:39:12 +01:00
|
|
|
}
|
|
|
|
|
2018-10-12 17:54:15 +01:00
|
|
|
func (h *Handler) convert(w http.ResponseWriter, r *http.Request, job, spaceID string, conversion api.ConversionJobRequest) {
|
2017-08-02 12:39:12 +01:00
|
|
|
method := "conversion.upload"
|
|
|
|
ctx := domain.GetRequestContext(r)
|
|
|
|
|
2017-08-27 16:39:09 +01:00
|
|
|
licenseKey, _ := h.Store.Setting.Get("EDITION-LICENSE", "key")
|
|
|
|
licenseSignature, _ := h.Store.Setting.Get("EDITION-LICENSE", "signature")
|
2017-08-02 12:39:12 +01:00
|
|
|
k, _ := hex.DecodeString(licenseKey)
|
|
|
|
s, _ := hex.DecodeString(licenseSignature)
|
|
|
|
|
|
|
|
conversion.LicenseKey = k
|
|
|
|
conversion.LicenseSignature = s
|
|
|
|
|
|
|
|
org, err := h.Store.Organization.GetOrganization(ctx, ctx.OrgID)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
conversion.ServiceEndpoint = org.ConversionEndpoint
|
|
|
|
|
|
|
|
var fileResult *api.DocumentConversionResponse
|
|
|
|
var filename string
|
|
|
|
filename, fileResult, err = storageProvider.Convert(conversion)
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if fileResult.Err != "" {
|
|
|
|
response.WriteServerError(w, method, errors.New(fileResult.Err))
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: empty .docx documents trigger this error
|
|
|
|
if len(fileResult.Pages) == 0 {
|
|
|
|
response.WriteMissingDataError(w, method, "no pages in document")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
|
|
|
if err != nil {
|
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-20 14:38:35 +01:00
|
|
|
// Fetch space where document resides.
|
2018-10-12 17:54:15 +01:00
|
|
|
sp, err := h.Store.Space.Get(ctx, spaceID)
|
2018-04-20 14:38:35 +01:00
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
response.WriteServerError(w, method, err)
|
|
|
|
h.Runtime.Log.Error(method, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
nd, err := processDocument(ctx, h.Runtime, h.Store, h.Indexer, filename, job, sp, fileResult)
|
2017-08-02 12:39:12 +01:00
|
|
|
if err != nil {
|
2018-02-04 15:43:57 +00:00
|
|
|
ctx.Transaction.Rollback()
|
2017-08-02 12:39:12 +01:00
|
|
|
response.WriteServerError(w, method, err)
|
2017-08-03 10:00:24 +01:00
|
|
|
h.Runtime.Log.Error(method, err)
|
2017-08-02 12:39:12 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response.WriteJSON(w, nd)
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:14:48 +01:00
|
|
|
func processDocument(ctx domain.RequestContext, r *env.Runtime, store *store.Store, indexer indexer.Indexer, filename, job string, sp space.Space, fileResult *api.DocumentConversionResponse) (newDocument doc.Document, err error) {
|
2017-08-02 12:39:12 +01:00
|
|
|
// Convert into database objects
|
|
|
|
document := convertFileResult(filename, fileResult)
|
|
|
|
document.Job = job
|
|
|
|
document.OrgID = ctx.OrgID
|
2018-09-19 16:03:29 +01:00
|
|
|
document.SpaceID = sp.RefID
|
2017-08-02 12:39:12 +01:00
|
|
|
document.UserID = ctx.UserID
|
|
|
|
documentID := uniqueid.Generate()
|
|
|
|
document.RefID = documentID
|
|
|
|
|
2018-11-05 19:48:50 +00:00
|
|
|
if r.Product.Edition == domain.CommunityEdition {
|
2018-05-10 15:10:15 +01:00
|
|
|
document.Lifecycle = workflow.LifecycleLive
|
|
|
|
} else {
|
|
|
|
document.Lifecycle = sp.Lifecycle
|
|
|
|
}
|
2017-08-02 12:39:12 +01:00
|
|
|
err = store.Document.Add(ctx, document)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
err = errors.Wrap(err, "cannot insert new document")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range fileResult.Pages {
|
|
|
|
var p page.Page
|
|
|
|
p.OrgID = ctx.OrgID
|
|
|
|
p.DocumentID = documentID
|
|
|
|
p.Level = v.Level
|
2018-09-19 16:03:29 +01:00
|
|
|
p.Name = v.Title
|
2017-08-02 12:39:12 +01:00
|
|
|
p.Body = string(v.Body)
|
|
|
|
p.Sequence = float64(k+1) * 1024.0 // need to start above 0 to allow insertion before the first item
|
|
|
|
pageID := uniqueid.Generate()
|
|
|
|
p.RefID = pageID
|
|
|
|
p.ContentType = "wysiwyg"
|
2018-09-19 16:03:29 +01:00
|
|
|
p.Type = "section"
|
2017-08-02 12:39:12 +01:00
|
|
|
|
|
|
|
meta := page.Meta{}
|
2018-09-19 16:03:29 +01:00
|
|
|
meta.SectionID = pageID
|
2017-08-02 12:39:12 +01:00
|
|
|
meta.RawBody = p.Body
|
|
|
|
meta.Config = "{}"
|
|
|
|
|
|
|
|
model := page.NewPage{}
|
|
|
|
model.Page = p
|
|
|
|
model.Meta = meta
|
|
|
|
|
|
|
|
err = store.Page.Add(ctx, model)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
err = errors.Wrap(err, "cannot insert new page for new document")
|
|
|
|
return
|
|
|
|
}
|
2018-03-28 14:25:37 +01:00
|
|
|
|
|
|
|
// pp, _ := store.Page.Get(ctx, pageID)
|
|
|
|
go indexer.IndexContent(ctx, p)
|
2017-08-02 12:39:12 +01:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:25:37 +01:00
|
|
|
da := []attachment.Attachment{}
|
|
|
|
|
2017-08-02 12:39:12 +01:00
|
|
|
for _, e := range fileResult.EmbeddedFiles {
|
|
|
|
//fmt.Println("DEBUG embedded file info", document.OrgId, document.Job, e.Name, len(e.Data), e.ID)
|
|
|
|
var a attachment.Attachment
|
|
|
|
a.DocumentID = documentID
|
|
|
|
a.Job = document.Job
|
|
|
|
a.FileID = e.ID
|
|
|
|
a.Filename = strings.Replace(e.Name, "embeddings/", "", 1)
|
|
|
|
a.Data = e.Data
|
|
|
|
refID := uniqueid.Generate()
|
|
|
|
a.RefID = refID
|
|
|
|
|
|
|
|
err = store.Attachment.Add(ctx, a)
|
|
|
|
if err != nil {
|
|
|
|
ctx.Transaction.Rollback()
|
|
|
|
err = errors.Wrap(err, "cannot insert attachment for new document")
|
|
|
|
return
|
|
|
|
}
|
2018-03-28 14:25:37 +01:00
|
|
|
|
|
|
|
da = append(da, a)
|
2017-08-02 12:39:12 +01:00
|
|
|
}
|
|
|
|
|
2017-08-21 07:43:18 +01:00
|
|
|
store.Activity.RecordUserActivity(ctx, activity.UserActivity{
|
2018-09-19 16:03:29 +01:00
|
|
|
SpaceID: newDocument.SpaceID,
|
2018-01-19 11:36:38 +00:00
|
|
|
DocumentID: newDocument.RefID,
|
2017-08-21 07:43:18 +01:00
|
|
|
SourceType: activity.SourceTypeDocument,
|
|
|
|
ActivityType: activity.TypeCreated})
|
|
|
|
|
|
|
|
err = ctx.Transaction.Commit()
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot commit new document import")
|
|
|
|
return
|
|
|
|
}
|
2017-08-18 14:31:36 +01:00
|
|
|
|
2017-08-02 12:39:12 +01:00
|
|
|
newDocument, err = store.Document.Get(ctx, documentID)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot fetch new document")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:13:09 +01:00
|
|
|
go indexer.IndexDocument(ctx, newDocument, da)
|
2018-03-28 14:25:37 +01:00
|
|
|
|
2019-01-04 16:33:30 +00:00
|
|
|
err = store.Space.IncrementContentCount(ctx, newDocument.SpaceID)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot increment space content count")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-02 12:39:12 +01:00
|
|
|
store.Audit.Record(ctx, audit.EventTypeDocumentUpload)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// convertFileResult takes the results of a document upload and convert,
|
|
|
|
// and creates the outline of a database record suitable for inserting into the document
|
|
|
|
// table.
|
|
|
|
func convertFileResult(filename string, fileResult *api.DocumentConversionResponse) (document doc.Document) {
|
|
|
|
document = doc.Document{}
|
|
|
|
document.RefID = ""
|
|
|
|
document.OrgID = ""
|
2018-09-19 16:03:29 +01:00
|
|
|
document.SpaceID = ""
|
2017-08-02 12:39:12 +01:00
|
|
|
document.Job = ""
|
|
|
|
document.Location = filename
|
|
|
|
|
|
|
|
if fileResult != nil {
|
|
|
|
if len(fileResult.Pages) > 0 {
|
2018-09-19 16:03:29 +01:00
|
|
|
document.Name = fileResult.Pages[0].Title
|
2017-08-02 12:39:12 +01:00
|
|
|
document.Slug = stringutil.MakeSlug(fileResult.Pages[0].Title)
|
|
|
|
}
|
|
|
|
document.Excerpt = fileResult.Excerpt
|
|
|
|
}
|
|
|
|
|
|
|
|
document.Tags = "" // now a # separated list of tag-words, rather than JSON
|
|
|
|
|
|
|
|
return document
|
|
|
|
}
|