Skip to content

Commit 508c04b

Browse files
committed
查看MS Office附件的时候,调用soffice生成预览的html
1 parent 6a945d0 commit 508c04b

File tree

5 files changed

+137
-62
lines changed

5 files changed

+137
-62
lines changed

src/server/RFC2047/RFC2047.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func decodeBuffer(s string) (string, []byte, error) {
5757
if charset != "iso-8859-1" &&
5858
charset != "utf-8" &&
5959
charset != "iso-2022-jp" &&
60+
charset != "big5" &&
6061
charset != "gb18030" &&
6162
charset != "gb2312" &&
6263
charset != "gbk" {
@@ -94,7 +95,7 @@ func decodeRFC2047Word(dec []byte, charset string) (string, error) {
9495
b.WriteRune(rune(c))
9596
}
9697
return b.String(), nil
97-
case "gb18030", "iso-2022-jp":
98+
case "gb18030", "big5", "iso-2022-jp":
9899
cd, err := iconv.Open("utf-8", charset)
99100
if err != nil {
100101
return "", err

src/server/handlers/doc_viewer.go

Lines changed: 51 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
package handlers
22

33
import (
4+
"io/ioutil"
45
"net/http"
56
"os"
7+
"os/exec"
68
"path"
79
"strings"
8-
// "net/url"
910

1011
"../web"
1112
)
1213

14+
const kCmd = "/Volumes/HDD/Users/leeight/Applications/LibreOffice.app/Contents/MacOS/soffice"
15+
1316
type DocViewerHandler struct {
1417
Context web.Context
1518
}
1619

20+
// 只有 if (/\.(doc|xls|ppt)x?$/i.test(item.name)) { 会进入这个Handler来处理
1721
// docid=base64(${uidl}/att/xxx.docx)
1822
// base64解码之后就可以知道需要回写的url路径
1923
// 后续浏览附件的时候,如果有xxx.pdf,那么优先浏览这个,否则给出提示即可
@@ -33,62 +37,54 @@ func (h DocViewerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3337
}
3438

3539
parts := strings.Split(name, "/")
36-
pdfname := parts[0] + "/doc/" + strings.Replace(parts[2], path.Ext(parts[2]), ".pdf", 1)
37-
pdfabs := path.Join(config.DownloadDir(), pdfname)
38-
log.Info("pdfabs = [%s]", pdfabs)
39-
if _, err := os.Stat(pdfabs); err == nil {
40-
http.Redirect(w, r, "/downloads/"+pdfname, http.StatusMovedPermanently)
40+
htmlname := parts[0] + "/doc/" + strings.Replace(parts[2], path.Ext(parts[2]), ".html", 1)
41+
htmlabs := path.Join(config.DownloadDir(), htmlname)
42+
if _, err := os.Stat(htmlabs); err == nil {
43+
log.Info("htmlabs = [%s]", htmlabs)
44+
http.Redirect(w, r, "/downloads/"+htmlname, http.StatusMovedPermanently)
45+
return
46+
}
47+
48+
os.MkdirAll(path.Dir(htmlabs), 0755)
49+
p := exec.Command(kCmd,
50+
"--headless",
51+
"--convert-to", "html",
52+
"--outdir", path.Dir(htmlabs),
53+
abs)
54+
55+
err := p.Start()
56+
if err != nil {
57+
http.Error(w, err.Error(), http.StatusServiceUnavailable)
58+
return
59+
}
60+
61+
err = p.Wait()
62+
if err != nil {
63+
http.Error(w, err.Error(), http.StatusServiceUnavailable)
4164
return
4265
}
4366

44-
w.Write([]byte(`<!doctype html>
45-
<html>
46-
<head>
47-
<meta charset="utf-8" />
48-
<title>预览『` + path.Base(name) + `』</title>
49-
<style type="text/css">
50-
html, body {
51-
height: 100%;
52-
}
53-
body {
54-
font-family: Arial, Helvetica, STHeiti, SimSun, sans-serif;
55-
font-size: 12px;
56-
color: #000;
57-
}
58-
p {
59-
position: relative;
60-
top: 50%;
61-
height: 100px;
62-
line-height: 100px;
63-
font-size: 18px;
64-
text-align: center;
65-
margin: 0;
66-
margin-top: -50px;
67-
}
68-
</style>
69-
</head>
70-
<body>
71-
<p>你可以<a id="submit-req" href="javascript:void(0)">提交文档转码请求</a>或者<a download="` + path.Base(name) +
72-
`" href="/downloads/` + name + `">直接下载</a>文档查看</p>
73-
</body>
74-
<script type="text/javascript">
75-
document.getElementById("submit-req").onclick = function() {
76-
var xhr = new XMLHttpRequest();
77-
xhr.onreadystatechange = function() {
78-
if (xhr.readyState === 4) {
79-
if (xhr.status === 200) {
80-
alert('提交成功,请稍候重试')
81-
} else {
82-
alert(xhr.responseText);
83-
}
67+
if _, err := os.Stat(htmlabs); err == nil {
68+
log.Info("htmlabs = [%s]", htmlabs)
69+
// 添加自定义的样式,美化一下效果
70+
raw, _ := ioutil.ReadFile(htmlabs)
71+
style := `
72+
<style type="text/css">
73+
html{ background: #ebebeb}
74+
body{
75+
width:980px;
76+
margin: 50px auto;
77+
background: #fff;
78+
padding: 10px;
79+
box-shadow: 0 0 0 1px #d1d1d1, 0 0 4px 1px #ccc;
8480
}
85-
}
86-
var url = '/api/doc/transfer?docid=' + encodeURIComponent('` + name + `')
87-
xhr.open('POST', url, true);
88-
xhr.send(null);
89-
return false;
90-
};
91-
</script>
92-
</html>
93-
`))
81+
</style>
82+
</head>`
83+
raw = []byte(strings.Replace(string(raw), "</head>", style, -1))
84+
85+
ioutil.WriteFile(htmlabs, raw, 0644)
86+
87+
http.Redirect(w, r, "/downloads/"+htmlname, http.StatusMovedPermanently)
88+
return
89+
}
9490
}

src/server/sqlite3-to-mysql.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#! /usr/bin/env python
2+
3+
import sys
4+
5+
def main():
6+
print "SET sql_mode='NO_BACKSLASH_ESCAPES';"
7+
lines = sys.stdin.read().splitlines()
8+
for line in lines:
9+
processLine(line)
10+
11+
def processLine(line):
12+
if (
13+
line.startswith("PRAGMA") or
14+
line.startswith("BEGIN TRANSACTION;") or
15+
line.startswith("COMMIT;") or
16+
line.startswith("DELETE FROM sqlite_sequence;") or
17+
line.startswith("INSERT INTO \"sqlite_sequence\"")
18+
):
19+
return
20+
line = line.replace("AUTOINCREMENT", "AUTO_INCREMENT")
21+
line = line.replace("DEFAULT 't'", "DEFAULT '1'")
22+
line = line.replace("DEFAULT 'f'", "DEFAULT '0'")
23+
line = line.replace(",'t'", ",'1'")
24+
line = line.replace(",'f'", ",'0'")
25+
in_string = False
26+
newLine = ''
27+
for c in line:
28+
if not in_string:
29+
if c == "'":
30+
in_string = True
31+
elif c == '"':
32+
newLine = newLine + '`'
33+
continue
34+
elif c == "'":
35+
in_string = False
36+
newLine = newLine + c
37+
print newLine
38+
39+
if __name__ == "__main__":
40+
main()

src/server/tools/doc_converter.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"os/exec"
9+
)
10+
11+
const kCmd = "/Volumes/HDD/Users/leeight/Applications/LibreOffice.app/Contents/MacOS/soffice"
12+
13+
func main() {
14+
var fileptr = flag.String("file", "", "The file will be convert to preview html")
15+
flag.Parse()
16+
17+
if *fileptr == "" {
18+
return
19+
}
20+
21+
// /Volumes/HDD/Users/leeight/Applications/LibreOffice.app/Contents/MacOS/soffice \
22+
// --headless \
23+
// --convert-to html \
24+
// ../../data/baidu.com/liyubei/downloads/723474/att/使用无障碍平台认证评测记录表-xx平台.xlsx \
25+
// --help
26+
var out bytes.Buffer
27+
28+
p := exec.Command(kCmd, "--headless", "--convert-to", "html", "--outdir", "tmp", *fileptr)
29+
p.Stdout = &out
30+
err := p.Start()
31+
if err != nil {
32+
log.Fatal(err)
33+
}
34+
35+
err = p.Wait()
36+
if err != nil {
37+
log.Fatal(err)
38+
}
39+
40+
fmt.Printf("%q\n", out.String())
41+
}

src/server/tools/fix_threads.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"../base"
1313
"../thread"
14+
"../web"
1415
)
1516

1617
func readAllMessages(db *sql.DB) []*thread.Message {
@@ -130,12 +131,8 @@ func addThread(db *sql.DB, subject string, mids []string) (int64, error) {
130131

131132
func main() {
132133
config, err := base.GetConfig("config.yml")
133-
134-
db, err := sql.Open("sqlite3", config.DbPath())
135-
if err != nil {
136-
log.Panic(err)
137-
return
138-
}
134+
ctx := web.NewContext(config)
135+
db := ctx.GetDb()
139136
defer db.Close()
140137

141138
messages := readAllMessages(db)

0 commit comments

Comments
 (0)