This commit is contained in:
守矢諏訪子 2023-06-23 18:09:23 +09:00
parent ff81340d99
commit b8a1c8f1ec
6 changed files with 131 additions and 55 deletions

View File

@ -8,11 +8,13 @@ import (
func getlist (lang string) []byte {
var jloc = []byte(`{
"top": "トップ",
"errfuseiurl": "URLは「http://」又は「https://」で始めます。"
"errfuseiurl": "URLは「http://」又は「https://」で始めます。",
"errfusei": "不正なエラー。"
}`)
var eloc = []byte(`{
"top": "Top",
"errfuseiurl": "The URL should start with \"http://\" or \"https://\"."
"errfuseiurl": "The URL should start with \"http://\" or \"https://\".",
"errfusei": "Unknown error."
}`)
if lang == "en" { return eloc }

15
mime.go Normal file
View File

@ -0,0 +1,15 @@
package main
func getmime () map[string]string {
return map[string]string {
"text/css": ".css",
"text/javascript": ".js",
"image/png": ".png",
"image/jpeg": ".jpg",
"image/webp": ".webp",
"image/gif": ".gif",
"font/ttf": ".ttf",
"font/woff2": ".woff2",
"image/vnd.microsoft.icon": ".ico",
}
}

View File

@ -25,7 +25,7 @@ func scanpage (path string, domain string, thisdomain string) error {
var video = regexp.MustCompile(`(<video.*</video>)`).ReplaceAllString(string(audio), "")
var iframe = regexp.MustCompile(`(<iframe.*</iframe>)`).ReplaceAllString(string(video), "")
/* 追加ダウンロード+ローカル化 */
var ass = regexp.MustCompile(`(<img.*src=['"]|<meta.*content=['"]|<link.*href=['"])(.*\.)(png|webm|jpg|jpeg|gif|css|js|ico|svg|tiff|woff2)(\?[^'"]*)?`)
var ass = regexp.MustCompile(`(<img.*src=['"]|<meta.*content=['"]|<link.*href=['"])(.*\.)(png|webp|jpg|jpeg|gif|css|js|ico|svg|ttf|woff2)(\?[^'"]*)?`)
spath := "static/"
if !strings.HasSuffix(path, "/") {
@ -43,6 +43,7 @@ func scanpage (path string, domain string, thisdomain string) error {
s := regexp.MustCompile(`(.*src=['"]|.*content=['"]|.*href=['"])`).Split(cssx, -1)
ss := regexp.MustCompile(`(['"].*)`).Split(s[1], -1)
ogurl := ss[0]
if strings.HasPrefix(ss[0], "//") {
ss[0] = "https:" + ss[0]
}
@ -71,21 +72,27 @@ func scanpage (path string, domain string, thisdomain string) error {
return err
}
} else {
af := domain
if strings.HasPrefix(ss[0], "/") {
af = af + ss[0]
} else {
af = af + "/" + ss[0]
u, err := url.Parse(domain)
if err != nil {
return err
}
rel, err := url.Parse(ss[0])
if err != nil {
return err
}
af := u.ResolveReference(rel).String()
err = dlres(af, filepath.Join(asspath, filename))
if err != nil {
return err
}
}
repmap[ss[0]] = filepath.Join("/static", assdom, filename)
repmap[ogurl] = filepath.Join("/static", assdom, filename)
if assdom == "" {
repmap[ss[0]] = filepath.Join("/static", filename)
repmap[ogurl] = filepath.Join("/static", filename)
}
if err != nil {
@ -114,13 +121,14 @@ func stripver (durl string) string {
fmt.Println("エラー:", err)
return ""
}
u.RawQuery = ""
return u.Path
}
func dlres (durl string, dest string) error {
// ダウンロード
res, err := http.Get(stripver(durl))
res, err := http.Get(durl)
if err != nil {
return err
}
@ -128,6 +136,15 @@ func dlres (durl string, dest string) error {
dest = stripver(dest)
// MIMEタイプを確認
ct := res.Header.Get("Content-Type")
for mime, ext := range getmime() {
if strings.Contains(ct, mime) && !strings.HasSuffix(dest, ext) {
dest += ext
break
}
}
// ファイルを作成
f, err := os.Create(dest)
if err != nil {
@ -136,21 +153,9 @@ func dlres (durl string, dest string) error {
defer f.Close()
// ファイルを書き込む
if strings.HasSuffix(dest, "css") || strings.HasSuffix(dest, "js") {
body, err := io.ReadAll(res.Body)
if err != nil {
return err
}
_, err2 := f.WriteString(string(body))
if err2 != nil {
return err
}
} else {
_, err = io.Copy(f, res.Body)
if err != nil {
return err
}
_, err = io.Copy(f, res.Body)
if err != nil {
return err
}
return nil

86
srv.go
View File

@ -5,34 +5,43 @@ import (
"fmt"
"net/http"
"strings"
"strconv"
"time"
"os"
"encoding/json"
)
type (
Page struct {
Tit string
Err string
Lan string
Ver string
Ves string
Ext []string // 既に存在する場合
Url string // 確認ページ用
Body string // 保存したページ用
Tit, Err, Lan, Ver, Ves, Url, Body string
Ext []Exist // 既に存在する場合
}
Stat struct {
Url string
Ver string
Url, Ver string
}
Exist struct {
Date, Url string
}
)
func initloc (r *http.Request) string {
cookie, err := r.Cookie("lang")
if err != nil {
return "ja"
} else {
return cookie.Value
if err == nil && cookie.Value == "en" {
return "en"
}
return "ja"
}
func tspath (p string) string {
pc := strings.Split(p, "/")
for i := len(pc) - 1; i >= 0; i-- {
if _, err := strconv.Atoi(pc[i]); err == nil {
return pc[i]
}
}
return ""
}
func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
@ -49,7 +58,11 @@ func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
data.Tit = getloc("top", lang)
if r.Method == "POST" {
err := r.ParseForm()
if err != nil { fmt.Println(err) }
if err != nil {
fmt.Println(err)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
// クッキー
if r.PostForm.Get("langchange") != "" {
@ -63,16 +76,45 @@ func siteHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
return
}
var exist []string
if r.PostForm.Get("hozonsite") != "" {
fmt.Println("sasa")
url := r.PostForm.Get("hozonsite")
fmt.Println(url)
// HTTPかHTTPSじゃない場合
if !checkprefix(url) {
data.Err = getloc("errfuseiurl", lang)
ftmpl[0] = cnf.webpath + "/view/404.html"
} else {
//if r.PostForm.Get("sosin") != "" {}
eurl := stripurl(url)
exist = checkexist(eurl, cnf.datapath)
if len(exist) == 0 || r.PostForm.Get("agree") == "1" {
path := mkdirs(eurl, cnf.datapath)
getpage(url, path)
scanpage(path, eurl, cnf.datapath)
http.Redirect(w, r, cnf.domain + strings.Replace(path, cnf.datapath, "", 1), http.StatusSeeOther)
} else if len(exist) > 0 {
ftmpl[0] = cnf.webpath + "/view/check.html"
data.Url = url
var existing []Exist
e := Exist{}
for _, ex := range exist {
ti, err := strconv.ParseInt(tspath(ex), 10, 64)
if err != nil {
fmt.Println(err)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
t := time.Unix(ti, 0)
e.Date = t.Format("2006年01月02日 15:04:05")
e.Url = strings.Replace(ex, cnf.datapath, cnf.domain, 1)
existing = append(existing, e)
}
data.Ext = existing
} else {
data.Err = getloc("errfusei", lang)
ftmpl[0] = cnf.webpath + "/view/404.html"
}
}
}
}
@ -118,7 +160,13 @@ func archiveHandler (cnf Config) func (http.ResponseWriter, *http.Request) {
pth += "index.html"
}
bdy, err := os.ReadFile(cnf.datapath + pth)
file := cnf.datapath + pth
if _, err := os.Stat(file); os.IsNotExist(err) {
http.Redirect(w, r, "/404", http.StatusSeeOther)
return
}
bdy, err := os.ReadFile(file)
if err != nil {
http.Redirect(w, r, "/404", http.StatusSeeOther)
return

View File

@ -13,9 +13,10 @@
position: fixed !important;
left: 0 !important;
top: 0 !important;
font-size: 14px !important;
font-size: 18px !important;
font-family: unset !important;
width: 100% !important;
text-align: left !important;
}
.archbody {
padding-top: 74px !important;

View File

@ -1,23 +1,28 @@
{{template "header" .}}
{{if eq .Lan "ja"}}既に保存されたページ:{{else}}Pages that already got archived:{{end}}
{{range .Ext}}
<!--a href="/archive/'.$e.'/'.$url.'">
date('Y年m月d日 H:i:s', $e)
<h3>{{.Url}}</h3>
{{if eq .Lan "en"}}
Pages that already got archived:
{{else}}
既に保存されたページ:
{{end}}<br />
{{range $i, $e := .Ext}}
<a href="{{$e.Url}}">
{{$e.Date}}
</a>
<br /-->
<br />
{{end}}
<p>
{{if eq .Lan "ja"}}
このページが既に保存されているみたいです。<br />本当に手続きましょうか?
{{else}}
{{if eq .Lan "en"}}
This page seems to have been already archived.<br />Do you really want to proceed?
{{else}}
このページが既に保存されているみたいです。<br />本当に手続きましょうか?
{{end}}
</p>
<form action="/" method="post">
<input type="hidden" name="hozonsite" value="{{.Url}}" />
<input type="hidden" name="agree" value="1" />
<div class="submit">
<input type="submit" name="submit" value="{{if eq .Lan "ja"}}はい保存して下さい{{else}}Yes, please archive!!{{end}}" />
<input type="submit" name="submit" value="{{if eq .Lan "en"}}Yes, please archive!!{{else}}はい保存して下さい{{end}}" />
</div>
</form>
{{template "footer" .}}