Pastebin

Check-in [d52baeac41]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Turn Db struct fields private
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d52baeac41a136b5bf05fc7c1bb0de97a142cc649baf5823b66f1fedf2e1f2d3
User & Date: jef 2019-11-05 08:36:52
Context
2019-11-05
12:30
Add -ldflags to reduce binary size check-in: b702abe83b user: jef tags: trunk
08:36
Turn Db struct fields private check-in: d52baeac41 user: jef tags: trunk
06:59
Fix typo in man page check-in: 49f2abf624 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to db.go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
..
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
..
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
...
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
package main

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"
	"net/url"
	"os"
	"path/filepath"
	"strings"
)

type PasteData struct {
	MimeType string
................................................................................
	InsertTime string `json:"inserttime"`
	MimeType   string `json:"mimetype"`
}

// PasteBinDb

type PasteBinDb struct {
	Cfg *PasteBinCfg
	Db  *sql.DB
}

func (p *PasteBinDb) init() error {
	dbDir := filepath.Dir(p.Cfg.DbPath)
	err := os.MkdirAll(dbDir, 0700)
	if err != nil {
		return err
	}

	db, err := sql.Open("sqlite3", p.Cfg.DbPath)
	if err != nil {
		return err
	}
	defer db.Close()

	_, err = db.Exec(`CREATE TABLE pastes (
	pasteid TEXT UNIQUE NOT NULL,
................................................................................
);
CREATE INDEX idx_pasteid on pastes (pasteid);`)

	return err
}

func (p *PasteBinDb) Open() (err error) {
	if _, err = os.Stat(p.Cfg.DbPath); os.IsNotExist(err) {
		if err = p.init(); err != nil {
			return
		}
	}

	p.Db, err = sql.Open("sqlite3", p.Cfg.DbPath)
	if err != nil {
		return
	}

	_, err = p.Db.Query("PRAGMA busy_timeout = 5000;")
	return
}

func (p *PasteBinDb) Close() {
	p.Db.Close()
}

func (p *PasteBinDb) List(baseURL *url.URL) (pastes []*Paste, err error) {
	rows, err := p.Db.Query(`SELECT pasteid, mimetype, inserttime FROM pastes ORDER BY inserttime`)
	if err != nil {
		return
	}
	defer rows.Close()

	for rows.Next() {
		paste := Paste{}
		err := rows.Scan(&paste.PasteId, &paste.MimeType, &paste.InsertTime)
		if err != nil {
			return nil, err
		}
		paste.URL = fmt.Sprintf("%s/%s", strings.TrimRight(baseURL.String(), "/"), paste.PasteId)
		pastes = append(pastes, &paste)
	}

	return
}

func (p *PasteBinDb) Add(data string) (url string, mimetype string, err error) {
	stmt, err := p.Db.Prepare(`INSERT INTO pastes (pasteid, mimetype, data) VALUES (?, ?, ?)`)
	if err != nil {
		return
	}
	defer stmt.Close()

	mimetype = MimeString(data)
	for _ = range []int{1, 2, 3} {
		pasteid := PasteIdRandom()
		_, err = stmt.Exec(pasteid, mimetype, data)
		if err == nil {
			url = fmt.Sprintf("%s/%s", strings.TrimRight(p.Cfg.PasteURL.String(), "/"), pasteid)
			return
		}
	}

	return
}

func (p *PasteBinDb) Del(pasteId string) error {
	if !PasteIdIsValid(pasteId) {
		return fmt.Errorf("invalid pasteid")
	}

	stmt, err := p.Db.Prepare(`DELETE FROM pastes WHERE pasteid = ?`)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(pasteId)
	return err
................................................................................

func (p *PasteBinDb) Purge(ndays int) error {
	if ndays < 0 {
		return nil
	}

	delay := fmt.Sprintf("-%d days", ndays)
	stmt, err := p.Db.Prepare(`DELETE FROM pastes WHERE inserttime < datetime('now', ?, 'localtime');`)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(delay)
	return err
}

func (p *PasteBinDb) Get(pasteid string) (pdata *PasteData, err error) {
	stmt, err := p.Db.Prepare(`SELECT data, mimetype FROM pastes WHERE pasteid = ?`)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query(pasteid)
	if err != nil {






<







 







|
|



|





|







 







|





|




|




|


|
|











|







|










|












|







 







|










|







1
2
3
4
5
6

7
8
9
10
11
12
13
..
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
..
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
...
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
package main

import (
	"database/sql"
	"fmt"
	_ "github.com/mattn/go-sqlite3"

	"os"
	"path/filepath"
	"strings"
)

type PasteData struct {
	MimeType string
................................................................................
	InsertTime string `json:"inserttime"`
	MimeType   string `json:"mimetype"`
}

// PasteBinDb

type PasteBinDb struct {
	cfg *PasteBinCfg
	db  *sql.DB
}

func (p *PasteBinDb) init() error {
	dbDir := filepath.Dir(p.cfg.DbPath)
	err := os.MkdirAll(dbDir, 0700)
	if err != nil {
		return err
	}

	db, err := sql.Open("sqlite3", p.cfg.DbPath)
	if err != nil {
		return err
	}
	defer db.Close()

	_, err = db.Exec(`CREATE TABLE pastes (
	pasteid TEXT UNIQUE NOT NULL,
................................................................................
);
CREATE INDEX idx_pasteid on pastes (pasteid);`)

	return err
}

func (p *PasteBinDb) Open() (err error) {
	if _, err = os.Stat(p.cfg.DbPath); os.IsNotExist(err) {
		if err = p.init(); err != nil {
			return
		}
	}

	p.db, err = sql.Open("sqlite3", p.cfg.DbPath)
	if err != nil {
		return
	}

	_, err = p.db.Query("PRAGMA busy_timeout = 5000;")
	return
}

func (p *PasteBinDb) Close() {
	p.db.Close()
}

func (p *PasteBinDb) List() (pastes []*Paste, err error) {
	rows, err := p.db.Query(`SELECT pasteid, mimetype, inserttime FROM pastes ORDER BY inserttime`)
	if err != nil {
		return
	}
	defer rows.Close()

	for rows.Next() {
		paste := Paste{}
		err := rows.Scan(&paste.PasteId, &paste.MimeType, &paste.InsertTime)
		if err != nil {
			return nil, err
		}
		paste.URL = fmt.Sprintf("%s/%s", strings.TrimRight(p.cfg.PasteURL.String(), "/"), paste.PasteId)
		pastes = append(pastes, &paste)
	}

	return
}

func (p *PasteBinDb) Add(data string) (url string, mimetype string, err error) {
	stmt, err := p.db.Prepare(`INSERT INTO pastes (pasteid, mimetype, data) VALUES (?, ?, ?)`)
	if err != nil {
		return
	}
	defer stmt.Close()

	mimetype = MimeString(data)
	for _ = range []int{1, 2, 3} {
		pasteid := PasteIdRandom()
		_, err = stmt.Exec(pasteid, mimetype, data)
		if err == nil {
			url = fmt.Sprintf("%s/%s", strings.TrimRight(p.cfg.PasteURL.String(), "/"), pasteid)
			return
		}
	}

	return
}

func (p *PasteBinDb) Del(pasteId string) error {
	if !PasteIdIsValid(pasteId) {
		return fmt.Errorf("invalid pasteid")
	}

	stmt, err := p.db.Prepare(`DELETE FROM pastes WHERE pasteid = ?`)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(pasteId)
	return err
................................................................................

func (p *PasteBinDb) Purge(ndays int) error {
	if ndays < 0 {
		return nil
	}

	delay := fmt.Sprintf("-%d days", ndays)
	stmt, err := p.db.Prepare(`DELETE FROM pastes WHERE inserttime < datetime('now', ?, 'localtime');`)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(delay)
	return err
}

func (p *PasteBinDb) Get(pasteid string) (pdata *PasteData, err error) {
	stmt, err := p.db.Prepare(`SELECT data, mimetype FROM pastes WHERE pasteid = ?`)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query(pasteid)
	if err != nil {

Changes to http.go.

192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
		}

		handler(w, r)
	}
}

func (h *httpHandler) pageApiList(w http.ResponseWriter, r *http.Request) {
	pastes, err := h.db.List(h.cfg.PasteURL)
	if err != nil {
		http.Error(w, fmt.Sprintf("error: %s"), http.StatusForbidden)
		return
	}

	jsonBytes, err := json.MarshalIndent(pastes, "", "    ")
	if err != nil {
................................................................................
		return
	}
}

func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var err error

	h.db = &PasteBinDb{Cfg: h.cfg}
	err = h.db.Open()
	if err != nil {
		http.Error(w, fmt.Sprintf("error: %s", err), http.StatusForbidden)
		return
	}
	defer h.db.Close()








|







 







|







192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
		}

		handler(w, r)
	}
}

func (h *httpHandler) pageApiList(w http.ResponseWriter, r *http.Request) {
	pastes, err := h.db.List()
	if err != nil {
		http.Error(w, fmt.Sprintf("error: %s"), http.StatusForbidden)
		return
	}

	jsonBytes, err := json.MarshalIndent(pastes, "", "    ")
	if err != nil {
................................................................................
		return
	}
}

func (h *httpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	var err error

	h.db = &PasteBinDb{cfg: h.cfg}
	err = h.db.Open()
	if err != nil {
		http.Error(w, fmt.Sprintf("error: %s", err), http.StatusForbidden)
		return
	}
	defer h.db.Close()