This repository was archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
183 lines (155 loc) · 3.87 KB
/
doc.go
File metadata and controls
183 lines (155 loc) · 3.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package ot
import (
"context"
"io"
"os"
"strconv"
"time"
"github.com/itcomusic/ot/pkg/oscript"
)
const docmanService = "DocumentManagement"
// FileAttr information about files.
type FileAttr struct {
NodeID int64 `oscript:"-"`
Created time.Time `oscript:"CreatedDate"`
Modified time.Time `oscript:"ModifiedDate"`
Name string `oscript:"Name"`
Size int64 `oscript:"DataForkSize"`
}
// MarshalOscriptBuf marshals to specific format, the fields for process unmarshal/marshal are identified differently.
func (f *FileAttr) MarshalOscriptBuf(buf oscript.Buffer) error {
buf.WriteString("A<1,?,'CreatedDate'=")
buf.WriteEncode(f.Created)
buf.WriteString(",'FileName'=")
buf.WriteStringValue(f.Name)
buf.WriteString(",'FileSize'=" + strconv.FormatInt(f.Size, 10))
buf.WriteString(",'ModifiedDate'=")
buf.WriteEncode(f.Modified)
buf.WriteString(",'_SDOName'='Core.FileAtts'")
buf.WriteByte('>')
return nil
}
// OpenFile opens the named file for reading.
func OpenFile(name string) (*os.File, *FileAttr, error) {
f, err := os.Open(name)
if err != nil {
return nil, nil, err
}
s, err := f.Stat()
if err != nil {
return nil, nil, err
}
return f, &FileAttr{
Name: s.Name(),
Size: s.Size(),
Created: time.Now(),
Modified: s.ModTime(),
}, nil
}
// CreateFile creates a document.
func (s *Session) CreateFile(ctx context.Context, parent int64, name string, file *FileAttr, r io.Reader) error {
c, err := s.connect(ctx)
if err != nil {
return err
}
defer c.Close()
if err := c.Write(docmanService, "CreateSimpleDocument", s.auth,
oscript.M{
"parentID": parent,
"name": name,
"fileAtts": file,
}); err != nil {
return err
}
if err := c.WriteFrom(r); err != nil {
return err
}
if err := errIn(c.Read(&file.NodeID)); err != nil {
return err
}
return nil
}
// AddVersionFile adds new version of the file.
func (s *Session) AddVersionFile(ctx context.Context, file *FileAttr, r io.Reader) error {
c, err := s.connect(ctx)
if err != nil {
return err
}
defer c.Close()
if err := c.Write(docmanService, "AddVersion", s.auth,
oscript.M{
"ID": file.NodeID,
"Metadata": nil,
"fileAtts": file,
}); err != nil {
return err
}
if err := c.WriteFrom(r); err != nil {
return err
}
if err := errIn(c.Read(nil)); err != nil {
return err
}
return nil
}
// ReadFile reads content and returns information about the file.
func (s *Session) ReadFile(ctx context.Context, id, version int64, w io.Writer) (*FileAttr, error) {
c, err := s.connect(ctx)
if err != nil {
return nil, err
}
defer c.Close()
if err := c.Write(docmanService, "GetVersionContents", s.auth,
oscript.M{
"ID": id,
"versionNum": version,
}); err != nil {
return nil, err
}
fa := &FileAttr{}
if err := errIn(c.ReadFile(fa)); err != nil {
return nil, err
}
if err := c.ReadTo(w); err != nil {
return nil, err
}
fa.NodeID = id
return fa, nil
}
type Document struct {
Comment string
Name string
Parent int64
Metadata Metadata
VersionControl bool
File *FileAttr
Reader io.Reader
}
// CreateDocument creates document.
func (s *Session) CreateDocument(ctx context.Context, doc Document) error {
c, err := s.connect(ctx)
if err != nil {
return err
}
defer c.Close()
if err := c.Write(docmanService, "CreateDocument", s.auth,
oscript.M{
"parentID": doc.Parent,
"name": doc.Name,
"comment": doc.Comment,
"advancedVersionControl": doc.VersionControl,
"metadata": doc.Metadata, // inherit metadata from the parent object if metadata not set
"fileAtts": doc.File,
}); err != nil {
return err
}
if err := c.WriteFrom(doc.Reader); err != nil {
return err
}
var node Node
if err := errIn(c.Read(&node)); err != nil {
return err
}
doc.File.NodeID = node.ID
return nil
}