oficina / sheets Public

Clone
git clone https://amadan.net/oficina/sheets

Plain git — no account needed to clone.

Download

Download this file

1package sheets
2
3import (
4 "encoding/json"
5 "errors"
6
7 "gorm.io/gorm"
8
9 "amadan.net/oficina/sheets/internal/sheets/store"
10 "amadan.net/oficina/sheets/internal/sheets/xlsx"
11)
12
13// sealPurposeImportReport is the seal purpose an import report is
14// stored under. Its own purpose, so a report can never be opened as a
15// cell, a title or an event, nor any of those as a report.
16const sealPurposeImportReport = "import.report"
17
18// errDiscardedBeforeReveal is an import whose sheet was discarded (by a
19// stale-import sweep) before it could be revealed.
20var errDiscardedBeforeReveal = errors.New("sheets: that sheet was discarded before its import finished")
21
22// revealImport stores an import's report and reveals its sheet, in one
23// transaction.
24//
25// One transaction is the point. A visible imported sheet always has
26// its report, and a report never exists for a sheet that was not
27// revealed: if the reveal finds the sheet already discarded, the
28// report rolls back with it. The deleted_at condition is markFilled's,
29// for markFilled's reason.
30func (a *App) revealImport(id uint64, rep xlsx.Report) error {
31 if a.db == nil {
32 return errNoDB
33 }
34 body, err := json.Marshal(rep)
35 if err != nil {
36 return err
37 }
38 sealed, err := a.sealer.Seal(sealPurposeImportReport, body)
39 if err != nil {
40 return err
41 }
42 return a.db.Transaction(func(tx *gorm.DB) error {
43 if err := tx.Where("sheet_id = ?", id).Delete(&store.ImportReport{}).Error; err != nil {
44 return err
45 }
46 if err := tx.Create(&store.ImportReport{SheetID: id, ReportSealed: sealed}).Error; err != nil {
47 return err
48 }
49 res := tx.Model(&store.Sheet{}).Where("id = ? AND deleted_at IS NULL", id).
50 Update("filling_since", nil)
51 if res.Error != nil {
52 return res.Error
53 }
54 if res.RowsAffected == 0 {
55 return errDiscardedBeforeReveal
56 }
57 return nil
58 })
59}
60
61// importReport opens a sheet's import report. ok is false when the
62// sheet has none: it was not imported, or was imported before reports
63// existed.
64//
65// The caller has already decided the viewer may read the sheet; a
66// report is never served on its own authority.
67func (a *App) importReport(sheetID uint64) (rep xlsx.Report, ok bool, err error) {
68 if a.db == nil {
69 return xlsx.Report{}, false, errNoDB
70 }
71 var row store.ImportReport
72 err = a.db.Where("sheet_id = ?", sheetID).Limit(1).Find(&row).Error
73 if err != nil || row.SheetID == 0 {
74 return xlsx.Report{}, false, err
75 }
76 body, err := a.sealer.Open(sealPurposeImportReport, row.ReportSealed)
77 if err != nil {
78 return xlsx.Report{}, false, err
79 }
80 if err := json.Unmarshal(body, &rep); err != nil {
81 return xlsx.Report{}, false, err
82 }
83 return rep, true, nil
84}
85
86// hasImportReport is importReport without opening the seal: whether
87// the sheet's File menu offers the report at all.
88func (a *App) hasImportReport(sheetID uint64) bool {
89 if a.db == nil {
90 return false
91 }
92 var n int64
93 a.db.Model(&store.ImportReport{}).Where("sheet_id = ?", sheetID).Count(&n)
94 return n > 0
95}
96