@@ -12,6 +12,7 @@ import (
1212 "regexp"
1313 "strconv"
1414 "strings"
15+ "sync"
1516 "text/template"
1617)
1718
@@ -79,6 +80,7 @@ type Session struct {
7980 path string
8081 pageCount int
8182 tmpDir string
83+ mu sync.Mutex
8284 annotated map [int ]struct {}
8385}
8486
@@ -109,7 +111,12 @@ func New(path string) (*Session, error) {
109111 return nil , err
110112 }
111113
112- return & Session {copyPath , p , tmpDir , map [int ]struct {}{}}, nil
114+ return & Session {
115+ path : copyPath ,
116+ pageCount : p ,
117+ tmpDir : tmpDir ,
118+ annotated : map [int ]struct {}{},
119+ }, nil
113120}
114121
115122func (s * Session ) PageCount () int {
@@ -214,7 +221,9 @@ func (s *Session) Annotate(page int) (bool, error) {
214221 modified := afterEditStat .ModTime () != beforeEditStat .ModTime ()
215222 if modified {
216223 _ = os .Remove (s .thumbPath (page ))
224+ s .mu .Lock ()
217225 s .annotated [page ] = struct {}{}
226+ s .mu .Unlock ()
218227 }
219228 return modified , nil
220229}
@@ -235,11 +244,15 @@ func (s *Session) IsAnnotated(page int) bool {
235244 if page < 0 || page >= s .pageCount {
236245 panic ("invalid page number" )
237246 }
247+ s .mu .Lock ()
238248 _ , ok := s .annotated [page ]
249+ s .mu .Unlock ()
239250 return ok
240251}
241252
242253func (s * Session ) HasAnnotations () bool {
254+ s .mu .Lock ()
255+ defer s .mu .Unlock ()
243256 return len (s .annotated ) > 0
244257}
245258
@@ -249,16 +262,21 @@ func (s *Session) Clear(page int) {
249262 }
250263 _ = os .Remove (s .annotPath (page ))
251264 _ = os .Remove (s .thumbPath (page ))
265+ s .mu .Lock ()
252266 delete (s .annotated , page )
267+ s .mu .Unlock ()
253268}
254269
255270func (s * Session ) Save (path string ) error {
256271
257272 // Shortcut for when no page is annotated
258273
274+ s .mu .Lock ()
259275 if len (s .annotated ) == 0 {
276+ s .mu .Unlock ()
260277 return fileCopy (s .path , path )
261278 }
279+ s .mu .Unlock ()
262280
263281 // Covert all annotated pages to PDF
264282
@@ -330,7 +348,9 @@ func (s *Session) Close() {
330348 _ = os .Remove (filepath .Join (s .tmpDir , f .Name ()))
331349 }
332350 _ = os .Remove (s .tmpDir )
351+ s .mu .Lock ()
333352 s .annotated = nil
353+ s .mu .Unlock ()
334354 s .tmpDir = ""
335355 s .pageCount = - 1
336356 s .path = ""
0 commit comments