aboutsummaryrefslogtreecommitdiff
path: root/crane.go
blob: d8d0fd2aae92e43769955c1770fed15b38723a54 (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
package main

import (
	"bufio"
	"context"
	"encoding/xml"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"mime"
	"net"
	"net/http"
	"net/http/cookiejar"
	"net/url"
	"os"
	"path/filepath"
	"strings"
	"sync"
	"time"

	"golang.org/x/net/publicsuffix"
)

const (
	TIMEOUT  time.Duration = 25       // seconds to wait for an outbound HTTP request to complete
	MAX_SIZE int64         = 50000000 // max incoming HTTP request body size (50MB)
)

var (
	client      *http.Client
	scihubURL   *url.URL
	host        string
	port        uint64
	user        string
	pass        string
	buildPrefix string
)

type Contributor struct {
	FirstName string `xml:"given_name"`
	LastName  string `xml:"surname"`
	Role      string `xml:"contributor_role,attr"`
	Sequence  string `xml:"sequence,attr"`
}

type Meta struct {
	XMLName      xml.Name      `xml:"doi_records"`
	Journal      string        `xml:"doi_record>crossref>journal>journal_metadata>full_title"`
	ISSN         string        `xml:"doi_record>crossref>journal>journal_metadata>issn"`
	Title        string        `xml:"doi_record>crossref>journal>journal_article>titles>title"`
	Contributors []Contributor `xml:"doi_record>crossref>journal>journal_article>contributors>person_name"`
	PubYear      string        `xml:"doi_record>crossref>journal>journal_article>publication_date>year"`
	PubMonth     string        `xml:"doi_record>crossref>journal>journal_article>publication_date>month"`
	FirstPage    string        `xml:"doi_record>crossref>journal>journal_article>pages>first_page"`
	LastPage     string        `xml:"doi_record>crossref>journal>journal_article>pages>last_page"`
	DOI          string        `xml:"doi_record>crossref>journal>journal_article>doi_data>doi"`
	ArxivID      string        `xml:"doi_record>crossref>journal>journal_article>arxiv_data>arxiv_id"`
	Resource     string        `xml:"doi_record>crossref>journal>journal_article>doi_data>resource"`
}

type Paper struct {
	Meta      Meta
	MetaPath  string
	PaperName string
	PaperPath string
}

type Papers struct {
	sync.RWMutex
	List map[string]map[string]*Paper
	Path string
}

type Resp struct {
	Papers           Papers
	Status           string
	LastPaperDL      string
	LastUsedCategory string
}

// getPaperFileNameFromMeta returns the built filename (absent an extension)
// from metadata, consisting of the lowercase last name of the first author
// followed by the year of publication (e.g. doe2020)
func getPaperFileNameFromMeta(p *Meta) string {
	var mainAuthor string
	for _, contributor := range p.Contributors {
		if contributor.Sequence == "first" {
			mainAuthor = strings.Replace(contributor.LastName, "..", "", -1)
			mainAuthor = strings.Replace(contributor.LastName, "/", "", -1)
			break
		}
	}
	if mainAuthor == "" || p.PubYear == "" {
		return ""
	}
	pubYear := strings.Replace(p.PubYear, "..", "", -1)
	pubYear = strings.Replace(p.PubYear, "/", "", -1)
	return fmt.Sprint(strings.ToLower(mainAuthor), pubYear)
}

// getPaperFileNameFromResp returns the name of the file present at resp taken
// first from content-disposition (if exists) then its destination URL
// following redirects; e.g. doe2020
func getPaperFileNameFromResp(resp *http.Response) string {
	var filename string
	if disp, ok := resp.Header["Content-Disposition"]; ok {
		_, params, _ := mime.ParseMediaType(disp[0])
		if f, ok := params["filename"]; ok && f != "" {
			filename = f
		}
	}
	if filename == "" {
		u, _ := url.Parse(resp.Request.URL.String())
		filename = strings.TrimSuffix(filepath.Base(u.Path), "/")
	}
	filename = strings.TrimSuffix(filename, ".pdf")
	filename = strings.Replace(filename, "..", "", -1)
	filename = strings.Replace(filename, "/", "", -1)
	return filename
}

// getUniqueName ensures a paper name is unique, appending "-$ext" until
// a unique name is found and returned
func (papers *Papers) getUniqueName(category string, name string) string {
	newName := name
	ext := 2
	papers.RLock()
	for {
		key := filepath.Join(category, newName+".pdf")
		if _, exists := papers.List[category][key]; exists != true {
			break
		} else {
			newName = fmt.Sprint(name, "-", ext)
			ext++
		}
	}
	papers.RUnlock()
	return newName
}

// findPapersWalk is a WalkFunc passed to filepath.Walk() to process papers
// stored on the filesystem
func (papers *Papers) findPapersWalk(path string, info os.FileInfo,
	err error) error {
	// skip the papers.Path root directory
	if p, _ := filepath.Abs(path); p == papers.Path ||
		strings.HasPrefix(filepath.Base(path), ".") {

		return nil
	}

	papers.Lock()
	defer papers.Unlock()

	// derive category name (e.g. Mathematics) from directory name; used as key
	var category string
	if i, _ := os.Stat(path); i.IsDir() {
		category = strings.TrimPrefix(path, papers.Path+"/")
	} else {
		category = strings.TrimPrefix(filepath.Dir(path), papers.Path+"/")
	}
	if _, exists := papers.List[category]; exists == false {
		papers.List[category] = make(map[string]*Paper)
	}

	// now that category was added, ensure file is actually a PDF
	if filepath.Ext(path) != ".pdf" {
		return nil
	}

	var paper Paper
	paper.PaperName = strings.TrimSuffix(filepath.Base(path),
		filepath.Ext(path))
	paper.PaperPath = filepath.Join(papers.Path, filepath.Join(category,
		paper.PaperName+".pdf"))

	// XML metadata is not required but highly recommended; PDFs aren't parsed
	// so its our source only source of metadata at the moment
	//
	// PDF parsing looks (and probably is) fairly annoying to support and might
	// be better handled by an external script
	metaPath := filepath.Join(papers.Path, filepath.Join(category,
		paper.PaperName+".meta.xml"))
	if _, err := os.Stat(metaPath); err == nil {
		paper.MetaPath = metaPath

		f, err := os.Open(paper.MetaPath)
		if err != nil {
			return err
		}

		// memory-efficient relative to ioutil.ReadAll()
		r := bufio.NewReader(f)
		d := xml.NewDecoder(r)

		// populate p struct with values derived from doi.org metadata
		if err := d.Decode(&paper.Meta); err != nil {
			return err
		}
		if err := f.Close(); err != nil {
			return err
		}
	}

	// finally add paper to papers.List set; the subkey is the paper path
	// relative to papers.Path, e.g. Mathematics/example2020.pdf
	relPath := filepath.Join(category, paper.PaperName+".pdf")
	papers.List[category][relPath] = &paper
	return nil
}

// PopulatePapers wraps filepath.Walk() and populates the papers set with
// discovered papers
func (papers *Papers) PopulatePapers() error {
	if err := filepath.Walk(papers.Path, papers.findPapersWalk); err != nil {
		return err
	}
	return nil
}

// NewPaperFromDOI contains routines used to retrieve papers from remote
// endpoints provided a DOI
func (papers *Papers) NewPaperFromDOI(doi []byte, category string) (*Paper,
	error) {
	var paper Paper

	meta, err := getMetaFromDOI(client, doi)
	if err != nil {
		return nil, err
	}

	// create a temporary file to store XML stream
	tmpXML, err := ioutil.TempFile("", "tmp-*.meta.xml")
	if err != nil {
		return nil, err
	}
	defer os.Remove(tmpXML.Name())

	e := xml.NewEncoder(tmpXML)
	err = e.Encode(meta)
	if err != nil {
		return nil, err
	}
	tmpXML.Close()

	name := getPaperFileNameFromMeta(meta) // doe2020

	// last-resort if metadata lacking author or publication year
	if name == "" {
		name = strings.Replace(string(doi), "..", "", -1)
		name = strings.Replace(string(doi), "/", "", -1)
	}

	// doe2020-(2, 3, 4...) if n already exists in set
	uniqueName := papers.getUniqueName(category, name)

	// check if DOIs match (genuine duplicate)
	if name != uniqueName {
		key := filepath.Join(category, name+".pdf")
		papers.RLock()
		if meta.DOI == papers.List[category][key].Meta.DOI {
			return nil, fmt.Errorf("paper %q with DOI %q already downloaded",
				name, string(doi))
		}
		papers.RUnlock()
	}

	paper.PaperName = uniqueName
	paper.PaperPath = filepath.Join(filepath.Join(papers.Path, category),
		paper.PaperName+".pdf")
	paper.MetaPath = filepath.Join(filepath.Join(papers.Path, category),
		paper.PaperName+".meta.xml")

	// make outbound request to sci-hub, save paper to temporary location
	tmpPDF, err := getPaper(client, scihubURL, string(doi))
	defer os.Remove(tmpPDF)
	if err != nil {
		// try passing resource URL (from doi.org metadata) to sci-hub instead
		// (force cache)
		if meta.Resource != "" {
			if tmpPDF, err = getPaper(client, scihubURL, meta.Resource); err != nil {
				return nil, err
			}
		} else {
			return nil, err
		}
	}

	if err := renameFile(tmpPDF, paper.PaperPath); err != nil {
		return nil, err
	}
	if err := renameFile(tmpXML.Name(), paper.MetaPath); err != nil {
		return nil, err
	}
	paper.Meta = *meta

	papers.Lock()
	papers.List[category][filepath.Join(category,
		paper.PaperName+".pdf")] = &paper
	papers.Unlock()
	return &paper, nil
}

// NewPaperFromDirectLink contains routines used to retrieve papers from remote
// endpoints provided a direct link's http.Response and/or optional metadata
func (papers *Papers) NewPaperFromDirectLink(resp *http.Response, meta *Meta,
	category string) (*Paper, error) {
	tmpPDF, err := ioutil.TempFile("", "tmp-*.pdf")
	if err != nil {
		return &Paper{}, err
	}
	err = saveRespBody(resp, tmpPDF.Name())
	if err != nil {
		return &Paper{}, err
	}
	if err := tmpPDF.Close(); err != nil {
		return &Paper{}, err
	}
	defer os.Remove(tmpPDF.Name())

	var paper Paper
	paper.PaperName = papers.getUniqueName(category,
		getPaperFileNameFromMeta(meta))
	if paper.PaperName == "" {
		paper.PaperName = papers.getUniqueName(category,
			getPaperFileNameFromResp(resp))
	}
	paper.PaperPath = filepath.Join(papers.Path,
		filepath.Join(category, paper.PaperName+".pdf"))

	if err := renameFile(tmpPDF.Name(), paper.PaperPath); err != nil {
		return nil, err
	}
	papers.Lock()
	papers.List[category][filepath.Join(category,
		paper.PaperName+".pdf")] = &paper
	papers.Unlock()
	return &paper, nil
}

// DeletePaper deletes a paper and its metadata from the filesystem and the
// papers.List set
func (papers *Papers) DeletePaper(paper string) error {
	// check if the category in which the paper is said to belong
	// exists
	papers.RLock()
	category := filepath.Dir(paper)
	if _, exists := papers.List[category]; exists != true {
		return fmt.Errorf("category %q does not exist\n",
			papers.List[filepath.Dir(paper)])
	}

	// check if paper already exists in the provided category
	if _, exists := papers.List[category][paper]; exists != true {
		return fmt.Errorf("paper %q does not exist in category %q\n", paper,
			category)
	}
	papers.RUnlock()

	// paper and category exists and the paper belongs to the provided
	// category; remove it and its XML metadata
	papers.Lock()
	if err := os.Remove(papers.List[category][paper].PaperPath); err != nil {
		return err
	}

	// XML metadata optional; delete it if it exists
	metaPath := papers.List[category][paper].MetaPath
	if metaPath != "" {
		if _, err := os.Stat(metaPath); err == nil {
			if err := os.Remove(metaPath); err != nil {
				return err
			}
		}
	}
	delete(papers.List[category], paper)
	papers.Unlock()
	return nil
}

// DeleteCategory deletes a category and its contents from the filesystem and
// the papers.List set
func (papers *Papers) DeleteCategory(category string) error {
	papers.RLock()
	if _, exists := papers.List[category]; exists != true {
		return fmt.Errorf("category %q does not exist in the set\n", category)
	}
	papers.RUnlock()

	papers.Lock()
	if err := os.RemoveAll(filepath.Join(papers.Path, category)); err != nil {
		return err
	}

	// remove subcategories (nested directories) which exist under the primary
	for key, _ := range papers.List {
		if strings.HasPrefix(key, category+"/") {
			delete(papers.List, key)
		}
	}

	delete(papers.List, category)
	papers.Unlock()
	return nil
}

// MovePaper moves a paper to the destination category on the filesystem and
// the papers.List set
func (papers *Papers) MovePaper(paper string, category string) error {
	papers.RLock()
	prevCategory := filepath.Dir(paper)
	if _, exists := papers.List[prevCategory]; exists != true {
		return fmt.Errorf("category %q does not exist\n", prevCategory)
	}
	if _, exists := papers.List[category]; exists != true {
		return fmt.Errorf("category %q does not exist\n", category)
	}
	if _, exists := papers.List[prevCategory][paper]; exists != true {
		return fmt.Errorf("paper %q does not exist in category %q\n", paper,
			prevCategory)
	}
	if _, exists := papers.List[category][paper]; exists == true {
		return fmt.Errorf("paper %q exists in destination category %q\n",
			paper, category)
	}
	papers.RUnlock()

	papers.Lock()
	paperDest := filepath.Join(filepath.Join(papers.Path, category),
		papers.List[prevCategory][paper].PaperName+".pdf")
	if err := os.Rename(papers.List[prevCategory][paper].PaperPath, paperDest); err != nil {
		return err
	}

	papers.List[category][filepath.Join(category,
		filepath.Base(paper))] = papers.List[prevCategory][paper]

	papers.List[category][filepath.Join(category,
		filepath.Base(paper))].PaperPath = paperDest

	// XML metadata optional; move if any exists
	metaPath := papers.List[prevCategory][paper].MetaPath
	if metaPath != "" {
		if _, err := os.Stat(metaPath); err == nil {
			metaName := papers.List[prevCategory][paper].PaperName +
				".meta.xml"
			metaDest := filepath.Join(filepath.Join(papers.Path, category),
				metaName)
			if err := os.Rename(metaPath, metaDest); err != nil {
				return err
			}
			papers.List[category][filepath.Join(category,
				filepath.Base(paper))].MetaPath = metaDest
		}
	}
	delete(papers.List[prevCategory], paper)

	papers.Unlock()
	return nil
}

// RenameCategory renames a category on the filesystem and the paper.List set
func (papers *Papers) RenameCategory(oldCategory string,
	newCategory string) error {
	papers.RLock()
	if _, exists := papers.List[oldCategory]; exists != true {
		return fmt.Errorf("category %q does not exist in the set\n", oldCategory)
	}
	if _, exists := papers.List[newCategory]; exists == true {
		return fmt.Errorf("category %q already exists in the set\n", newCategory)
	}
	if err := os.Rename(filepath.Join(papers.Path, oldCategory),
		filepath.Join(papers.Path, newCategory)); err != nil {
		return err
	}
	papers.RUnlock()

	papers.Lock()
	papers.List[newCategory] = make(map[string]*Paper)
	for k, v := range papers.List[oldCategory] {
		pPaperPath := filepath.Join(papers.Path, filepath.Join(newCategory,
			v.PaperName+".pdf"))
		pK := filepath.Join(newCategory, filepath.Base(k))
		papers.List[newCategory][pK] = papers.List[oldCategory][k]
		papers.List[newCategory][pK].PaperPath = pPaperPath

		if v.MetaPath != "" {
			pMetaPath := filepath.Join(papers.Path, filepath.Join(newCategory,
				v.PaperName+".meta.xml"))
			papers.List[newCategory][pK].MetaPath = pMetaPath
		}
	}
	delete(papers.List, oldCategory)

	papers.Unlock()
	return nil
}

// ProcessAddPaperInput processes takes user input and attempts to retrieve
// a DOI and initiate paper download
func (papers *Papers) ProcessAddPaperInput(category string,
	input string) (*Paper, error) {
	if strings.HasPrefix(input, "http") {
		resp, err := makeRequest(client, input)
		if err != nil {
			return &Paper{}, err
		}
		if resp.Header.Get("Content-Type") == "application/pdf" {
			paper, err := papers.NewPaperFromDirectLink(resp, &Meta{}, category)
			if err != nil {
				return &Paper{}, err
			}
			return paper, nil
		}

		meta, err := getMetaFromCitation(resp)
		if err != nil {
			return nil, err
		}
		if meta.Resource != "" {
			resp, err := makeRequest(client, meta.Resource)
			if err == nil && strings.HasPrefix(resp.Header.Get("Content-Type"), "application/pdf") {
				paper, err := papers.NewPaperFromDirectLink(resp, meta, category)
				if err != nil {
					return nil, err
				} else {
					tmpXML, err := ioutil.TempFile("", "tmp-*.meta.xml")
					if err != nil {
						return nil, err
					}
					defer os.Remove(tmpXML.Name())

					e := xml.NewEncoder(tmpXML)
					err = e.Encode(meta)
					if err != nil {
						return nil, err
					}
					tmpXML.Close()

					paper.MetaPath = filepath.Join(filepath.Join(papers.Path,
						category), paper.PaperName+".meta.xml")
					if err := renameFile(tmpXML.Name(), paper.MetaPath); err != nil {
						return nil, err
					}

					paper.Meta = *meta
					return paper, nil
				}
			}
		}
		if meta.DOI != "" {
			paper, err := papers.NewPaperFromDOI([]byte(meta.DOI), category)
			if err != nil {
				return nil, err
			}
			return paper, nil
		} else {
			return &Paper{}, fmt.Errorf("%q: DOI could not be discovered", input)
		}
	} else {
		doi := getDOIFromBytes([]byte(input))
		if doi == nil {
			return &Paper{}, fmt.Errorf("%q is not a valid DOI or URL\n", input)
		}
		if paper, err := papers.NewPaperFromDOI(doi, category); err != nil {
			return nil, fmt.Errorf("%q: %v", input, err)
		} else {
			return paper, nil
		}
	}
}

func main() {
	// some publishers have cookie + HTTP 302 checks (e.g. sagepub), let's look
	// like a real browser
	options := cookiejar.Options{
		PublicSuffixList: publicsuffix.List,
	}
	cookies, err := cookiejar.New(&options)
	if err != nil {
		panic(err)
	}

	// custom DialContext which blocks outbound requests to local addresses and
	// interfaces (security)
	http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context,
		network, addr string) (net.Conn, error) {
		// we could run our check after a dial, but we'd have to discard
		// connect errors to prevent exposure of local services; a preemptive
		// lookup is the lesser of two evils, I think
		hosts, _ := net.LookupHost(addr[:strings.LastIndex(addr, ":")])
		for _, host := range hosts {
			if isPrivateIP(net.ParseIP(host)) {
				return nil, errors.New("requests to private IPs are blocked")
			}
		}
		conn, err := net.Dial(network, addr)
		if err != nil {
			return nil, err
		}
		return conn, err
	}
	client = &http.Client{
		Jar:     cookies,
		Timeout: TIMEOUT * time.Second,
	}

	var papers Papers
	papers.List = make(map[string]map[string]*Paper)

	var scihub string

	flag.StringVar(&scihub, "sci-hub", "https://sci-hub.hkvisa.net/", "Sci-Hub URL")
	flag.StringVar(&papers.Path, "path", "./papers",
		"Absolute or relative path to papers folder")
	flag.StringVar(&host, "host", "127.0.0.1", "IP address to listen on")
	flag.Uint64Var(&port, "port", 9090, "Port to listen on")
	flag.StringVar(&user, "user", "", "Username for /admin/ endpoints (optional)")
	flag.StringVar(&pass, "pass", "", "Password for /admin/ endpoints (optional)")
	flag.Parse()

	papers.Path, _ = filepath.Abs(papers.Path)

	scihubURL, err = url.Parse(scihub)
	if err != nil {
		panic(err)
	}
	if _, err := os.Stat(papers.Path); os.IsNotExist(err) {
		os.Mkdir(papers.Path, os.ModePerm)
	}
	if err := papers.PopulatePapers(); err != nil {
		panic(err)
	}
	if net.ParseIP(host) == nil {
		panic(errors.New("Host flag could not be parsed; is it an IP address?"))
	}

	http.HandleFunc("/", papers.IndexHandler)
	http.HandleFunc("/admin/", papers.AdminHandler)
	http.HandleFunc("/admin/edit/", papers.EditHandler)
	http.HandleFunc("/admin/add/", papers.AddHandler)
	http.HandleFunc("/download/", papers.DownloadHandler)
	fmt.Printf("Listening on %v port %v (http://%v:%v/)\n", host, port, host,
		port)
	log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), nil))
}