1
0
Fork 0
mirror of https://github.com/documize/community.git synced 2025-07-25 08:09:43 +02:00

foundational layer for inserting content and attachment links into content

This commit is contained in:
Harvey Kandola 2016-10-23 18:33:07 -07:00
parent 5ca53ecb04
commit 7db618dea0
20 changed files with 1397 additions and 721 deletions

View file

@ -40,7 +40,7 @@ func AttachmentDownload(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
attachment, err := p.GetAttachmentByJobAndFileID(params["orgID"], params["job"], params["fileID"])
attachment, err := p.GetAttachment(params["orgID"], params["attachmentID"])
if err == sql.ErrNoRows {
writeNotFoundError(w, method, params["fileID"])

View file

@ -0,0 +1,135 @@
// 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 endpoint
import (
"database/sql"
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/documize/community/core/api/entity"
"github.com/documize/community/core/api/request"
"github.com/documize/community/core/api/util"
)
// GetLinkCandidates returns references to documents/sections/attachments.
func GetLinkCandidates(w http.ResponseWriter, r *http.Request) {
method := "GetLinkCandidates"
p := request.GetPersister(r)
params := mux.Vars(r)
documentID := params["documentID"]
pageID := params["pageID"]
// parameter check
if len(documentID) == 0 {
util.WriteMissingDataError(w, method, "documentID")
return
}
if len(pageID) == 0 {
util.WriteMissingDataError(w, method, "pageID")
return
}
// permission check
if !p.CanViewDocument(documentID) {
util.WriteForbiddenError(w)
return
}
// We can link to a section within the same document so
// let's get all pages for the document and remove "us".
pages, err := p.GetPagesWithoutContent(documentID)
if err != nil && err != sql.ErrNoRows {
util.WriteServerError(w, method, err)
return
}
if len(pages) == 0 {
pages = []entity.Page{}
}
pc := []entity.LinkCandidate{}
for _, p := range pages {
if p.RefID != pageID {
c := entity.LinkCandidate{
RefID: util.UniqueID(),
DocumentID: documentID,
PageID: p.RefID,
LinkType: "section",
Title: p.Title,
}
pc = append(pc, c)
}
}
// We can link to attachment within the same document so
// let's get all attachments for the document.
files, err := p.GetAttachments(documentID)
if err != nil && err != sql.ErrNoRows {
util.WriteServerError(w, method, err)
return
}
if len(files) == 0 {
files = []entity.Attachment{}
}
fc := []entity.LinkCandidate{}
for _, f := range files {
c := entity.LinkCandidate{
RefID: util.UniqueID(),
DocumentID: documentID,
AttachmentID: f.RefID,
LinkType: "file",
Title: f.Filename,
}
fc = append(fc, c)
}
// send back the payload
var payload struct {
Pages []entity.LinkCandidate `json:"pages"`
Attachments []entity.LinkCandidate `json:"attachments"`
Matches []entity.LinkCandidate `json:"matches"`
}
payload.Pages = pc
payload.Attachments = fc
json, err := json.Marshal(payload)
if err != nil {
util.WriteMarshalError(w, err)
return
}
util.WriteSuccessBytes(w, json)
}
/*
DocumentID string `json:"documentId"`
PageID string `json:"pageId"`
FileID string `json:"fileId"`
LinkType string `json:"linkType"`
Title string `json:"caption"` // what we label the link
Context string `json:"context"` // additional context (e.g. excerpt, parent)
*/

View file

@ -136,7 +136,7 @@ func init() {
log.IfErr(Add(RoutePrefixPublic, "forgot", []string{"POST", "OPTIONS"}, nil, ForgotUserPassword))
log.IfErr(Add(RoutePrefixPublic, "reset/{token}", []string{"POST", "OPTIONS"}, nil, ResetUserPassword))
log.IfErr(Add(RoutePrefixPublic, "share/{folderID}", []string{"POST", "OPTIONS"}, nil, AcceptSharedFolder))
log.IfErr(Add(RoutePrefixPublic, "attachments/{orgID}/{job}/{fileID}", []string{"GET", "OPTIONS"}, nil, AttachmentDownload))
log.IfErr(Add(RoutePrefixPublic, "attachments/{orgID}/{attachmentID}", []string{"GET", "OPTIONS"}, nil, AttachmentDownload))
log.IfErr(Add(RoutePrefixPublic, "version", []string{"GET", "OPTIONS"}, nil, version))
// **** add secure routes
@ -212,12 +212,14 @@ func init() {
log.IfErr(Add(RoutePrefixPrivate, "sections", []string{"POST", "OPTIONS"}, nil, RunSectionCommand))
log.IfErr(Add(RoutePrefixPrivate, "sections/refresh", []string{"GET", "OPTIONS"}, nil, RefreshSections))
// Links
log.IfErr(Add(RoutePrefixPrivate, "links/{documentID}/{pageID}", []string{"GET", "OPTIONS"}, nil, GetLinkCandidates))
// Global installation-wide config
log.IfErr(Add(RoutePrefixPrivate, "global", []string{"GET", "OPTIONS"}, nil, GetGlobalConfig))
log.IfErr(Add(RoutePrefixPrivate, "global", []string{"PUT", "OPTIONS"}, nil, SaveGlobalConfig))
// **** configure single page app handler.
// Single page app handler
log.IfErr(Add(RoutePrefixRoot, "robots.txt", []string{"GET", "OPTIONS"}, nil, GetRobots))
log.IfErr(Add(RoutePrefixRoot, "sitemap.xml", []string{"GET", "OPTIONS"}, nil, GetSitemap))
log.IfErr(Add(RoutePrefixRoot, "{rest:.*}", nil, nil, web.EmberHandler))

View file

@ -342,3 +342,27 @@ type SitemapDocument struct {
Folder string
Revised time.Time
}
// Link defines a reference between a section and another document/section/attachment.
type Link struct {
BaseEntity
OrgID string `json:"orgId"`
UserID string `json:"userId"`
SourceID string `json:"sourceId"`
DocumentID string `json:"documentId"`
PageID string `json:"pageId"`
LinkType string `json:"linkType"`
Orphan bool `json:"orphan"`
}
// LinkCandidate defines a potential link to a document/section/attachment.
type LinkCandidate struct {
RefID string `json:"id"`
OrgID string `json:"orgId"`
DocumentID string `json:"documentId"`
PageID string `json:"pageId"`
AttachmentID string `json:"attachmentId"`
LinkType string `json:"linkType"`
Title string `json:"title"` // what we label the link
Context string `json:"context"` // additional context (e.g. excerpt, parent)
}

View file

@ -47,23 +47,23 @@ func (p *Persister) AddAttachment(a entity.Attachment) (err error) {
return
}
// GetAttachmentByJobAndFileID returns the database attachment record specified by the parameters.
func (p *Persister) GetAttachmentByJobAndFileID(orgID, job, fileID string) (attachment entity.Attachment, err error) {
// GetAttachment returns the database attachment record specified by the parameters.
func (p *Persister) GetAttachment(orgID, attachmentID string) (attachment entity.Attachment, err error) {
err = nil
stmt, err := Db.Preparex("SELECT id, refid, orgid, documentid, job, fileid, filename, data, extension, created, revised FROM attachment WHERE orgid=? and job=? and fileid=?")
stmt, err := Db.Preparex("SELECT id, refid, orgid, documentid, job, fileid, filename, data, extension, created, revised FROM attachment WHERE orgid=? and refid=?")
defer utility.Close(stmt)
if err != nil {
log.Error(fmt.Sprintf("Unable to prepare select for attachment %s/%s", job, fileID), err)
log.Error(fmt.Sprintf("Unable to prepare select for attachment %s", attachmentID), err)
return
}
err = stmt.Get(&attachment, orgID, job, fileID)
err = stmt.Get(&attachment, orgID, attachmentID)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute select for attachment %s/%s", job, fileID), err)
log.Error(fmt.Sprintf("Unable to execute select for attachment %s", attachmentID), err)
return
}

138
core/api/request/link.go Normal file
View file

@ -0,0 +1,138 @@
// 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 request
import (
"fmt"
"time"
"github.com/documize/community/core/api/entity"
"github.com/documize/community/core/log"
"github.com/documize/community/core/utility"
)
// AddLink inserts wiki-link into the store.
// These links exist when content references another document or content.
func (p *Persister) AddLink(l entity.Link) (err error) {
l.UserID = p.Context.UserID
l.Created = time.Now().UTC()
l.Revised = time.Now().UTC()
stmt, err := p.Context.Transaction.Preparex("INSERT INTO link (refid, orgid, userid, sourceid, documentid, pageid, linktype, created, revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")
defer utility.Close(stmt)
if err != nil {
log.Error("Unable to prepare insert for link", err)
return
}
_, err = stmt.Exec(l.RefID, l.OrgID, l.UserID, l.SourceID, l.DocumentID, l.PageID, l.LinkType, l.Created, l.Revised)
if err != nil {
log.Error("Unable to execute insert for link", err)
return
}
return
}
// GetReferencedLinks returns all links that the specified section is referencing.
func (p *Persister) GetReferencedLinks(sectionID string) (links []entity.Link, err error) {
err = nil
sql := "SELECT id,refid,orgid,userid,sourceid,documentid,sectionid,linktype,orphan,created,revised from link WHERE orgid=? AND sourceid=?"
err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
return
}
return
}
// GetLinksToSection returns all links that are linking to the specified section.
func (p *Persister) GetLinksToSection(sectionID string) (links []entity.Link, err error) {
err = nil
sql := "SELECT id,refid,orgid,userid,sourceid,documentid,sectionid,linktype,orphan,created,revised from link WHERE orgid=? AND sectionid=?"
err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
return
}
return
}
// GetLinksToDocument returns all links that are linking to the specified document.
func (p *Persister) GetLinksToDocument(documentID string) (links []entity.Link, err error) {
err = nil
sql := "SELECT id,refid,orgid,userid,sourceid,documentid,sectionid,linktype,orphan,created,revised from link WHERE orgid=? AND documentid=?"
err = Db.Select(&links, sql, p.Context.OrgID, documentID)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
return
}
return
}
// MarkLinkAsOrphan marks the link record as being invalid.
func (p *Persister) MarkLinkAsOrphan(l entity.Link) (err error) {
l.Orphan = true
l.Revised = time.Now().UTC()
stmt, err := p.Context.Transaction.PrepareNamed("UPDATE link SET orphan=1 revised=:revised WHERE orgid=:orgid AND refid=:refid")
defer utility.Close(stmt)
if err != nil {
log.Error(fmt.Sprintf("Unable to prepare update for link %s", l.RefID), err)
return
}
_, err = stmt.Exec(&l)
if err != nil {
log.Error(fmt.Sprintf("Unable to execute update for link %s", l.RefID), err)
return
}
return
}
// DeleteLink removes saved link from the store.
func (p *Persister) DeleteLink(id string) (rows int64, err error) {
return p.Base.DeleteConstrained(p.Context.Transaction, "link", p.Context.OrgID, id)
}
// GetLinkCandidates returns matching results based upon specified parameters.
// func (p *Persister) GetLinkCandidates(keywords string) (c []entity.LinkCandidate, err error) {
// err = nil
//
// sql := "SELECT id,refid,orgid,userid,sourceid,documentid,sectionid,linktype,orphan,created,revised from link WHERE orgid=? AND sectionid=?"
//
// err = Db.Select(&links, sql, p.Context.OrgID, sectionID)
//
// if err != nil {
// log.Error(fmt.Sprintf("Unable to execute select links for org %s", p.Context.OrgID), err)
// return
// }
//
// return
// }