Skip to content

Commit

Permalink
Merge pull request #249 from documize/section-pdf-files
Browse files Browse the repository at this point in the history
PDF section type + section level attachments
  • Loading branch information
HarveyKandola authored Apr 19, 2019
2 parents c0ed3c3 + 7287891 commit 10c57a0
Show file tree
Hide file tree
Showing 444 changed files with 46,291 additions and 840 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ All you need to provide is PostgreSQL, Microsoft SQL Server or any MySQL variant

## Latest Release

[Community Edition: v2.3.2](https://github.com/documize/community/releases)
[Community Edition: v2.4.0](https://github.com/documize/community/releases)

[Enterprise Edition: v2.3.2](https://www.documize.com/downloads)
[Enterprise Edition: v2.4.0](https://www.documize.com/downloads)

> *We provide frequent product updates for both cloud and self-hosted customers.*
>
Expand Down
4 changes: 4 additions & 0 deletions core/database/scripts/mysql/db_00029.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Community Edition */

-- Support per section attachments
ALTER TABLE dmz_doc_attachment ADD COLUMN `c_sectionid` VARCHAR(20) NOT NULL DEFAULT '' COLLATE utf8_bin AFTER `c_docid`;
2 changes: 1 addition & 1 deletion core/database/scripts/postgresql/db_00003.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TABLE dmz_space_label (
CREATE INDEX idx_space_label_1 ON dmz_space_label (id);
CREATE INDEX idx_space_label_2 ON dmz_space_label (c_orgid);

-- Space table upgrade to support labelling, icon and summary stats
-- Space table upgrade to support label, icon and summary stats
ALTER TABLE dmz_space ADD COLUMN c_desc VARCHAR(200) NOT NULL DEFAULT '';
ALTER TABLE dmz_space ADD COLUMN c_labelid VARCHAR(20) NOT NULL DEFAULT '' COLLATE ucs_basic;
ALTER TABLE dmz_space ADD COLUMN c_icon VARCHAR(20) NOT NULL DEFAULT '';
Expand Down
4 changes: 4 additions & 0 deletions core/database/scripts/postgresql/db_00005.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Community Edition */

-- Support per section attachments
ALTER TABLE dmz_doc_attachment ADD COLUMN c_sectionid VARCHAR(20) NOT NULL DEFAULT '' COLLATE ucs_basic;
4 changes: 4 additions & 0 deletions core/database/scripts/sqlserver/db_00002.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/* Community Edition */

-- Support per section attachments
ALTER TABLE dmz_doc_attachment ADD c_sectionid NVARCHAR(20) COLLATE Latin1_General_CS_AS NOT NULL DEFAULT '';
5 changes: 4 additions & 1 deletion domain/attachment/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
response.WriteServerError(w, method, err)
return
}

if len(a) == 0 {
a = []attachment.Attachment{}
}
Expand Down Expand Up @@ -299,6 +298,9 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
return
}

// File can be associated with a section as well.
sectionID := request.Query(r, "page")

if !permission.CanChangeDocument(ctx, *h.Store, documentID) {
response.WriteForbiddenError(w)
return
Expand Down Expand Up @@ -336,6 +338,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
a.FileID = random[0:9]
a.Filename = filename.Filename
a.Data = b.Bytes()
a.SectionID = sectionID

ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
Expand Down
44 changes: 39 additions & 5 deletions domain/attachment/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package attachment

import (
"database/sql"
"fmt"
"strings"
"time"

Expand All @@ -36,8 +37,8 @@ func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err erro
bits := strings.Split(a.Filename, ".")
a.Extension = bits[len(bits)-1]

_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_doc_attachment (c_refid, c_orgid, c_docid, c_job, c_fileid, c_filename, c_data, c_extension, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
a.RefID, a.OrgID, a.DocumentID, a.Job, a.FileID, a.Filename, a.Data, a.Extension, a.Created, a.Revised)
_, err = ctx.Transaction.Exec(s.Bind("INSERT INTO dmz_doc_attachment (c_refid, c_orgid, c_docid, c_sectionid, c_job, c_fileid, c_filename, c_data, c_extension, c_created, c_revised) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
a.RefID, a.OrgID, a.DocumentID, a.SectionID, a.Job, a.FileID, a.Filename, a.Data, a.Extension, a.Created, a.Revised)

if err != nil {
err = errors.Wrap(err, "execute insert attachment")
Expand All @@ -50,7 +51,7 @@ func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err erro
func (s Store) GetAttachment(ctx domain.RequestContext, orgID, attachmentID string) (a attachment.Attachment, err error) {
err = s.Runtime.Db.Get(&a, s.Bind(`
SELECT id, c_refid AS refid,
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
c_filename AS filename, c_data AS data, c_extension AS extension,
c_created AS created, c_revised AS revised
FROM dmz_doc_attachment
Expand All @@ -68,7 +69,7 @@ func (s Store) GetAttachment(ctx domain.RequestContext, orgID, attachmentID stri
func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
err = s.Runtime.Db.Select(&a, s.Bind(`
SELECT id, c_refid AS refid,
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
c_filename AS filename, c_extension AS extension,
c_created AS created, c_revised AS revised
FROM dmz_doc_attachment
Expand All @@ -88,11 +89,36 @@ func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []atta
return
}

// GetSectionAttachments returns a slice containing the attachment records
// with file data for specified document section.
func (s Store) GetSectionAttachments(ctx domain.RequestContext, sectionID string) (a []attachment.Attachment, err error) {
err = s.Runtime.Db.Select(&a, s.Bind(`
SELECT id, c_refid AS refid,
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
c_filename AS filename, c_data AS data, c_extension AS extension,
c_created AS created, c_revised AS revised
FROM dmz_doc_attachment
WHERE c_orgid=? AND c_sectionid=?
ORDER BY c_filename`),
ctx.OrgID, sectionID)

if err == sql.ErrNoRows {
err = nil
a = []attachment.Attachment{}
}
if err != nil {
err = errors.Wrap(err, "execute select section attachments")
return
}

return
}

// GetAttachmentsWithData returns a slice containing the attachment records (including their data) for document docID, ordered by filename.
func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
err = s.Runtime.Db.Select(&a, s.Bind(`
SELECT id, c_refid AS refid,
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
c_filename AS filename, c_data AS data, c_extension AS extension,
c_created AS created, c_revised AS revised
FROM dmz_doc_attachment
Expand All @@ -116,3 +142,11 @@ func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
return s.DeleteConstrained(ctx.Transaction, "dmz_doc_attachment", ctx.OrgID, id)
}

// DeleteSection removes all attachments agasinst a section.
func (s Store) DeleteSection(ctx domain.RequestContext, sectionID string) (rows int64, err error) {
rows, err = s.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_doc_attachment WHERE c_orgid='%s' AND c_sectionid='%s'",
ctx.OrgID, sectionID))

return
}
26 changes: 20 additions & 6 deletions domain/document/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
indexer "github.com/documize/community/domain/search"
"github.com/documize/community/domain/store"
"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/link"
Expand Down Expand Up @@ -661,6 +662,17 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
}
}

// Attachments.
a, err := h.Store.Attachment.GetAttachments(ctx, id)
if err != nil && err != sql.ErrNoRows {
h.Runtime.Log.Error("get attachment", err)
response.WriteServerError(w, method, err)
return
}
if len(a) == 0 {
a = []attachment.Attachment{}
}

// Prepare response.
data := BulkDocumentData{}
data.Document = document
Expand All @@ -669,6 +681,7 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
data.Links = l
data.Spaces = sp
data.Versions = v
data.Attachments = a

ctx.Transaction, err = h.Runtime.Db.Beginx()
if err != nil {
Expand Down Expand Up @@ -700,12 +713,13 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
// BulkDocumentData represents all data associated for a single document.
// Used by FetchDocumentData() bulk data load call.
type BulkDocumentData struct {
Document doc.Document `json:"document"`
Permissions pm.Record `json:"permissions"`
Roles pm.DocumentRecord `json:"roles"`
Spaces []space.Space `json:"folders"`
Links []link.Link `json:"links"`
Versions []doc.Version `json:"versions"`
Document doc.Document `json:"document"`
Permissions pm.Record `json:"permissions"`
Roles pm.DocumentRecord `json:"roles"`
Spaces []space.Space `json:"folders"`
Links []link.Link `json:"links"`
Versions []doc.Version `json:"versions"`
Attachments []attachment.Attachment `json:"attachments"`
}

// Export returns content as self-enclosed HTML file.
Expand Down
27 changes: 26 additions & 1 deletion domain/page/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/documize/community/core/env"
"github.com/documize/community/core/request"
"github.com/documize/community/core/response"
"github.com/documize/community/core/secrets"
"github.com/documize/community/core/streamutil"
"github.com/documize/community/core/uniqueid"
"github.com/documize/community/domain"
Expand Down Expand Up @@ -599,6 +600,8 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {

h.Store.Page.DeletePageRevisions(ctx, pageID)

h.Store.Attachment.DeleteSection(ctx, pageID)

// Update doc revised.
h.Store.Document.UpdateRevised(ctx, doc.RefID)

Expand Down Expand Up @@ -700,6 +703,8 @@ func (h *Handler) DeletePages(w http.ResponseWriter, r *http.Request) {

h.Store.Page.DeletePageRevisions(ctx, page.SectionID)

h.Store.Attachment.DeleteSection(ctx, page.SectionID)

// Draft actions are not logged
if doc.Lifecycle == workflow.LifecycleLive {
h.Store.Activity.RecordUserActivity(ctx, activity.UserActivity{
Expand Down Expand Up @@ -977,7 +982,27 @@ func (h *Handler) Copy(w http.ResponseWriter, r *http.Request) {
h.Store.Block.IncrementUsage(ctx, model.Page.TemplateID)
}

// Log t actions are not logged
// Copy section attachments.
at, err := h.Store.Attachment.GetSectionAttachments(ctx, pageID)
if err != nil {
h.Runtime.Log.Error(method, err)
}
for i := range at {
at[i].DocumentID = targetID
at[i].SectionID = newPageID
at[i].RefID = uniqueid.Generate()
random := secrets.GenerateSalt()
at[i].FileID = random[0:9]

err1 := h.Store.Attachment.Add(ctx, at[i])
if err1 != nil {
ctx.Transaction.Rollback()
response.WriteServerError(w, method, err1)
h.Runtime.Log.Error(method, err1)
return
}
}

if doc.Lifecycle == workflow.LifecycleLive {
h.Store.Activity.RecordUserActivity(ctx, activity.UserActivity{
SpaceID: doc.SpaceID,
Expand Down
55 changes: 55 additions & 0 deletions domain/section/pdfjs/pdfjs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2016 Documize Inc. <[email protected]>. 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 <[email protected]>.
//
// https://documize.com

package pdfjs

import (
"net/http"

"github.com/documize/community/core/env"
"github.com/documize/community/domain/section/provider"
"github.com/documize/community/domain/store"
)

// Provider represents PDF viewer
type Provider struct {
Runtime *env.Runtime
Store *store.Store
}

// Meta describes us.
func (*Provider) Meta() provider.TypeMeta {
section := provider.TypeMeta{}

section.ID = "d54b0b89-6a91-460b-885d-95518b3dfdca"
section.Title = "PDF Viewer"
section.Description = "View PDF file"
section.ContentType = "pdf"
section.PageType = "section"
section.Order = 9995

return section
}

// Command stub.
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
provider.WriteEmpty(w)
}

// Render just sends back HMTL as-is.
func (*Provider) Render(ctx *provider.Context, config, data string) string {
return config
}

// Refresh just sends back data as-is.
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
return config
}
2 changes: 2 additions & 0 deletions domain/section/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/documize/community/domain/section/jira"
"github.com/documize/community/domain/section/markdown"
"github.com/documize/community/domain/section/papertrail"
"github.com/documize/community/domain/section/pdfjs"
"github.com/documize/community/domain/section/plantuml"
"github.com/documize/community/domain/section/provider"
"github.com/documize/community/domain/section/table"
Expand All @@ -46,6 +47,7 @@ func Register(rt *env.Runtime, s *store.Store) {
provider.Register("airtable", &airtable.Provider{Runtime: rt, Store: s})
provider.Register("plantuml", &plantuml.Provider{Runtime: rt, Store: s})
provider.Register("flowchart", &flowchart.Provider{Runtime: rt, Store: s})
provider.Register("pdf", &pdfjs.Provider{Runtime: rt, Store: s})

p := provider.List()
rt.Log.Info(fmt.Sprintf("Extensions: registered %d section types", len(p)))
Expand Down
2 changes: 2 additions & 0 deletions domain/store/storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,10 @@ type AttachmentStorer interface {
Add(ctx domain.RequestContext, a attachment.Attachment) (err error)
GetAttachment(ctx domain.RequestContext, orgID, attachmentID string) (a attachment.Attachment, err error)
GetAttachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error)
GetSectionAttachments(ctx domain.RequestContext, sectionID string) (a []attachment.Attachment, err error)
GetAttachmentsWithData(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error)
Delete(ctx domain.RequestContext, id string) (rows int64, err error)
DeleteSection(ctx domain.RequestContext, id string) (rows int64, err error)
}

// LinkStorer defines required methods for persisting content links
Expand Down
6 changes: 3 additions & 3 deletions edition/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ func main() {
// product details
rt.Product = domain.Product{}
rt.Product.Major = "2"
rt.Product.Minor = "3"
rt.Product.Patch = "2"
rt.Product.Revision = "190416125622"
rt.Product.Minor = "4"
rt.Product.Patch = "0"
rt.Product.Revision = "190419131243"
rt.Product.Version = fmt.Sprintf("%s.%s.%s", rt.Product.Major, rt.Product.Minor, rt.Product.Patch)
rt.Product.Edition = domain.CommunityEdition
rt.Product.Title = fmt.Sprintf("%s Edition", rt.Product.Edition)
Expand Down
Loading

0 comments on commit 10c57a0

Please sign in to comment.