Skip to content

Commit 10c57a0

Browse files
Merge pull request #249 from documize/section-pdf-files
PDF section type + section level attachments
2 parents c0ed3c3 + 7287891 commit 10c57a0

File tree

444 files changed

+46291
-840
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

444 files changed

+46291
-840
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ All you need to provide is PostgreSQL, Microsoft SQL Server or any MySQL variant
1313

1414
## Latest Release
1515

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

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

2020
> *We provide frequent product updates for both cloud and self-hosted customers.*
2121
>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Community Edition */
2+
3+
-- Support per section attachments
4+
ALTER TABLE dmz_doc_attachment ADD COLUMN `c_sectionid` VARCHAR(20) NOT NULL DEFAULT '' COLLATE utf8_bin AFTER `c_docid`;

core/database/scripts/postgresql/db_00003.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CREATE TABLE dmz_space_label (
1515
CREATE INDEX idx_space_label_1 ON dmz_space_label (id);
1616
CREATE INDEX idx_space_label_2 ON dmz_space_label (c_orgid);
1717

18-
-- Space table upgrade to support labelling, icon and summary stats
18+
-- Space table upgrade to support label, icon and summary stats
1919
ALTER TABLE dmz_space ADD COLUMN c_desc VARCHAR(200) NOT NULL DEFAULT '';
2020
ALTER TABLE dmz_space ADD COLUMN c_labelid VARCHAR(20) NOT NULL DEFAULT '' COLLATE ucs_basic;
2121
ALTER TABLE dmz_space ADD COLUMN c_icon VARCHAR(20) NOT NULL DEFAULT '';
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Community Edition */
2+
3+
-- Support per section attachments
4+
ALTER TABLE dmz_doc_attachment ADD COLUMN c_sectionid VARCHAR(20) NOT NULL DEFAULT '' COLLATE ucs_basic;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/* Community Edition */
2+
3+
-- Support per section attachments
4+
ALTER TABLE dmz_doc_attachment ADD c_sectionid NVARCHAR(20) COLLATE Latin1_General_CS_AS NOT NULL DEFAULT '';

domain/attachment/endpoint.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,6 @@ func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
217217
response.WriteServerError(w, method, err)
218218
return
219219
}
220-
221220
if len(a) == 0 {
222221
a = []attachment.Attachment{}
223222
}
@@ -299,6 +298,9 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
299298
return
300299
}
301300

301+
// File can be associated with a section as well.
302+
sectionID := request.Query(r, "page")
303+
302304
if !permission.CanChangeDocument(ctx, *h.Store, documentID) {
303305
response.WriteForbiddenError(w)
304306
return
@@ -336,6 +338,7 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
336338
a.FileID = random[0:9]
337339
a.Filename = filename.Filename
338340
a.Data = b.Bytes()
341+
a.SectionID = sectionID
339342

340343
ctx.Transaction, err = h.Runtime.Db.Beginx()
341344
if err != nil {

domain/attachment/store.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package attachment
1313

1414
import (
1515
"database/sql"
16+
"fmt"
1617
"strings"
1718
"time"
1819

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

39-
_, 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
40-
a.RefID, a.OrgID, a.DocumentID, a.Job, a.FileID, a.Filename, a.Data, a.Extension, a.Created, a.Revised)
40+
_, 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 (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"),
41+
a.RefID, a.OrgID, a.DocumentID, a.SectionID, a.Job, a.FileID, a.Filename, a.Data, a.Extension, a.Created, a.Revised)
4142

4243
if err != nil {
4344
err = errors.Wrap(err, "execute insert attachment")
@@ -50,7 +51,7 @@ func (s Store) Add(ctx domain.RequestContext, a attachment.Attachment) (err erro
5051
func (s Store) GetAttachment(ctx domain.RequestContext, orgID, attachmentID string) (a attachment.Attachment, err error) {
5152
err = s.Runtime.Db.Get(&a, s.Bind(`
5253
SELECT id, c_refid AS refid,
53-
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
54+
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
5455
c_filename AS filename, c_data AS data, c_extension AS extension,
5556
c_created AS created, c_revised AS revised
5657
FROM dmz_doc_attachment
@@ -68,7 +69,7 @@ func (s Store) GetAttachment(ctx domain.RequestContext, orgID, attachmentID stri
6869
func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
6970
err = s.Runtime.Db.Select(&a, s.Bind(`
7071
SELECT id, c_refid AS refid,
71-
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
72+
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
7273
c_filename AS filename, c_extension AS extension,
7374
c_created AS created, c_revised AS revised
7475
FROM dmz_doc_attachment
@@ -88,11 +89,36 @@ func (s Store) GetAttachments(ctx domain.RequestContext, docID string) (a []atta
8889
return
8990
}
9091

92+
// GetSectionAttachments returns a slice containing the attachment records
93+
// with file data for specified document section.
94+
func (s Store) GetSectionAttachments(ctx domain.RequestContext, sectionID string) (a []attachment.Attachment, err error) {
95+
err = s.Runtime.Db.Select(&a, s.Bind(`
96+
SELECT id, c_refid AS refid,
97+
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
98+
c_filename AS filename, c_data AS data, c_extension AS extension,
99+
c_created AS created, c_revised AS revised
100+
FROM dmz_doc_attachment
101+
WHERE c_orgid=? AND c_sectionid=?
102+
ORDER BY c_filename`),
103+
ctx.OrgID, sectionID)
104+
105+
if err == sql.ErrNoRows {
106+
err = nil
107+
a = []attachment.Attachment{}
108+
}
109+
if err != nil {
110+
err = errors.Wrap(err, "execute select section attachments")
111+
return
112+
}
113+
114+
return
115+
}
116+
91117
// GetAttachmentsWithData returns a slice containing the attachment records (including their data) for document docID, ordered by filename.
92118
func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (a []attachment.Attachment, err error) {
93119
err = s.Runtime.Db.Select(&a, s.Bind(`
94120
SELECT id, c_refid AS refid,
95-
c_orgid AS orgid, c_docid AS documentid, c_job AS job, c_fileid AS fileid,
121+
c_orgid AS orgid, c_docid AS documentid, c_sectionid AS sectionid, c_job AS job, c_fileid AS fileid,
96122
c_filename AS filename, c_data AS data, c_extension AS extension,
97123
c_created AS created, c_revised AS revised
98124
FROM dmz_doc_attachment
@@ -116,3 +142,11 @@ func (s Store) GetAttachmentsWithData(ctx domain.RequestContext, docID string) (
116142
func (s Store) Delete(ctx domain.RequestContext, id string) (rows int64, err error) {
117143
return s.DeleteConstrained(ctx.Transaction, "dmz_doc_attachment", ctx.OrgID, id)
118144
}
145+
146+
// DeleteSection removes all attachments agasinst a section.
147+
func (s Store) DeleteSection(ctx domain.RequestContext, sectionID string) (rows int64, err error) {
148+
rows, err = s.DeleteWhere(ctx.Transaction, fmt.Sprintf("DELETE FROM dmz_doc_attachment WHERE c_orgid='%s' AND c_sectionid='%s'",
149+
ctx.OrgID, sectionID))
150+
151+
return
152+
}

domain/document/endpoint.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
indexer "github.com/documize/community/domain/search"
3131
"github.com/documize/community/domain/store"
3232
"github.com/documize/community/model/activity"
33+
"github.com/documize/community/model/attachment"
3334
"github.com/documize/community/model/audit"
3435
"github.com/documize/community/model/doc"
3536
"github.com/documize/community/model/link"
@@ -661,6 +662,17 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
661662
}
662663
}
663664

665+
// Attachments.
666+
a, err := h.Store.Attachment.GetAttachments(ctx, id)
667+
if err != nil && err != sql.ErrNoRows {
668+
h.Runtime.Log.Error("get attachment", err)
669+
response.WriteServerError(w, method, err)
670+
return
671+
}
672+
if len(a) == 0 {
673+
a = []attachment.Attachment{}
674+
}
675+
664676
// Prepare response.
665677
data := BulkDocumentData{}
666678
data.Document = document
@@ -669,6 +681,7 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
669681
data.Links = l
670682
data.Spaces = sp
671683
data.Versions = v
684+
data.Attachments = a
672685

673686
ctx.Transaction, err = h.Runtime.Db.Beginx()
674687
if err != nil {
@@ -700,12 +713,13 @@ func (h *Handler) FetchDocumentData(w http.ResponseWriter, r *http.Request) {
700713
// BulkDocumentData represents all data associated for a single document.
701714
// Used by FetchDocumentData() bulk data load call.
702715
type BulkDocumentData struct {
703-
Document doc.Document `json:"document"`
704-
Permissions pm.Record `json:"permissions"`
705-
Roles pm.DocumentRecord `json:"roles"`
706-
Spaces []space.Space `json:"folders"`
707-
Links []link.Link `json:"links"`
708-
Versions []doc.Version `json:"versions"`
716+
Document doc.Document `json:"document"`
717+
Permissions pm.Record `json:"permissions"`
718+
Roles pm.DocumentRecord `json:"roles"`
719+
Spaces []space.Space `json:"folders"`
720+
Links []link.Link `json:"links"`
721+
Versions []doc.Version `json:"versions"`
722+
Attachments []attachment.Attachment `json:"attachments"`
709723
}
710724

711725
// Export returns content as self-enclosed HTML file.

domain/page/endpoint.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/documize/community/core/env"
2323
"github.com/documize/community/core/request"
2424
"github.com/documize/community/core/response"
25+
"github.com/documize/community/core/secrets"
2526
"github.com/documize/community/core/streamutil"
2627
"github.com/documize/community/core/uniqueid"
2728
"github.com/documize/community/domain"
@@ -599,6 +600,8 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
599600

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

603+
h.Store.Attachment.DeleteSection(ctx, pageID)
604+
602605
// Update doc revised.
603606
h.Store.Document.UpdateRevised(ctx, doc.RefID)
604607

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

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

706+
h.Store.Attachment.DeleteSection(ctx, page.SectionID)
707+
703708
// Draft actions are not logged
704709
if doc.Lifecycle == workflow.LifecycleLive {
705710
h.Store.Activity.RecordUserActivity(ctx, activity.UserActivity{
@@ -977,7 +982,27 @@ func (h *Handler) Copy(w http.ResponseWriter, r *http.Request) {
977982
h.Store.Block.IncrementUsage(ctx, model.Page.TemplateID)
978983
}
979984

980-
// Log t actions are not logged
985+
// Copy section attachments.
986+
at, err := h.Store.Attachment.GetSectionAttachments(ctx, pageID)
987+
if err != nil {
988+
h.Runtime.Log.Error(method, err)
989+
}
990+
for i := range at {
991+
at[i].DocumentID = targetID
992+
at[i].SectionID = newPageID
993+
at[i].RefID = uniqueid.Generate()
994+
random := secrets.GenerateSalt()
995+
at[i].FileID = random[0:9]
996+
997+
err1 := h.Store.Attachment.Add(ctx, at[i])
998+
if err1 != nil {
999+
ctx.Transaction.Rollback()
1000+
response.WriteServerError(w, method, err1)
1001+
h.Runtime.Log.Error(method, err1)
1002+
return
1003+
}
1004+
}
1005+
9811006
if doc.Lifecycle == workflow.LifecycleLive {
9821007
h.Store.Activity.RecordUserActivity(ctx, activity.UserActivity{
9831008
SpaceID: doc.SpaceID,

domain/section/pdfjs/pdfjs.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2016 Documize Inc. <[email protected]>. All rights reserved.
2+
//
3+
// This software (Documize Community Edition) is licensed under
4+
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
5+
//
6+
// You can operate outside the AGPL restrictions by purchasing
7+
// Documize Enterprise Edition and obtaining a commercial license
8+
// by contacting <[email protected]>.
9+
//
10+
// https://documize.com
11+
12+
package pdfjs
13+
14+
import (
15+
"net/http"
16+
17+
"github.com/documize/community/core/env"
18+
"github.com/documize/community/domain/section/provider"
19+
"github.com/documize/community/domain/store"
20+
)
21+
22+
// Provider represents PDF viewer
23+
type Provider struct {
24+
Runtime *env.Runtime
25+
Store *store.Store
26+
}
27+
28+
// Meta describes us.
29+
func (*Provider) Meta() provider.TypeMeta {
30+
section := provider.TypeMeta{}
31+
32+
section.ID = "d54b0b89-6a91-460b-885d-95518b3dfdca"
33+
section.Title = "PDF Viewer"
34+
section.Description = "View PDF file"
35+
section.ContentType = "pdf"
36+
section.PageType = "section"
37+
section.Order = 9995
38+
39+
return section
40+
}
41+
42+
// Command stub.
43+
func (*Provider) Command(ctx *provider.Context, w http.ResponseWriter, r *http.Request) {
44+
provider.WriteEmpty(w)
45+
}
46+
47+
// Render just sends back HMTL as-is.
48+
func (*Provider) Render(ctx *provider.Context, config, data string) string {
49+
return config
50+
}
51+
52+
// Refresh just sends back data as-is.
53+
func (*Provider) Refresh(ctx *provider.Context, config, data string) string {
54+
return config
55+
}

0 commit comments

Comments
 (0)