From d00cf408d3c4fb46603764908deb4ccdbaeb2590 Mon Sep 17 00:00:00 2001 From: 兔子 Date: Tue, 4 Apr 2023 17:13:29 +0800 Subject: [PATCH] modify new file --- httpserver/mime.go | 215 +++++++++++++++++++++++++++++++++++++++++++ httpserver/server.go | 168 +++++++++++++++++++++++++++++++-- 2 files changed, 375 insertions(+), 8 deletions(-) diff --git a/httpserver/mime.go b/httpserver/mime.go index cda0226..4a7d6c0 100644 --- a/httpserver/mime.go +++ b/httpserver/mime.go @@ -2,6 +2,221 @@ package httpserver import "path/filepath" +func (h *HttpServer) FileType(name string) string { + ext := filepath.Ext(name) + if len(ext) == 0 || ext == "." { + return "文件" + } + ext = ext[1:] + mimeMap := map[string]string{ + "3g2": "视频文件", + "3gp": "视频文件", + "7z": "压缩文件", + "aac": "音频文件", + "aif": "音频文件", + "aiff": "音频文件", + "amr": "音频文件", + "apk": "Android 应用程序", + "appx": "Windows 应用程序", + "asf": "视频文件", + "avi": "视频文件", + "bak": "备份文件", + "bat": "批处理文件", + "bin": "二进制文件", + "bmp": "图像文件", + "c": "源代码文件", + "cab": "Windows 安装文件", + "cfg": "配置文件", + "class": "Java 类文件", + "com": "可执行文件", + "conf": "配置文件", + "cpp": "C++ 源代码文件", + "crx": "Google Chrome 扩展", + "cs": "C# 源代码文件", + "csv": "电子表格文件", + "cue": "光盘映像文件", + "dat": "数据文件", + "deb": "Debian 安装包", + "dll": "动态链接库文件", + "dmg": "MacOS 安装文件", + "doc": "Microsoft Word 文档", + "docx": "Microsoft Word 文档", + "dtd": "文档类型定义文件", + "dwg": "CAD 文件", + "dxf": "CAD 文件", + "eml": "电子邮件文件", + "eps": "矢量图形文件", + "exe": "可执行文件", + "flac": "音频文件", + "flv": "视频文件", + "fnt": "字体文件", + "fon": "字体文件", + "gif": "图像文件", + "gz": "压缩文件", + "htm": "网页文件", + "html": "网页文件", + "ibooks": "iBooks 电子书", + "ics": "日历文件", + "iso": "光盘映像文件", + "jar": "Java 应用程序", + "java": "Java 源代码文件", + "jpe": "图像文件", + "jpeg": "图像文件", + "jpg": "图像文件", + "js": "JavaScript 文件", + "json": "JSON 文件", + "key": "Keynote 演示文稿", + "log": "日志文件", + "m4a": "音频文件", + "m4v": "视频文件", + "mdb": "Microsoft Access 数据库文件", + "mht": "网页存档文件", + "mid": "图像文件", + "midi": "音频文件", + "mkv": "视频文件", + "mov": "视频文件", + "mp3": "音频文件", + "mp4": "视频文件", + "mpeg": "视频文件", + "mpg": "视频文件", + "msg": "电子邮件文件", + "nes": "NES 游戏 ROM 文件", + "obj": "3D 模型文件", + "odp": "OpenOffice Impress 演示文稿", + "ods": "OpenOffice Calc 电子表格文件", + "odt": "OpenOffice Writer 文档", + "ogg": "音频文件", + "otf": "字体文件", + "pages": "Pages 文档", + "pct": "图像文件", + "pdb": "Palm 数据库文件", + "pdf": "Adobe PDF 文档", + "php": "PHP 文件", + "go": "Go 源代码文件", + "pkg": "MacOS 安装包", + "pl": "Perl 文件", + "png": "图像文件", + "ppt": "PowerPoint 演示文稿", + "pptx": "PowerPoint 演示文稿", + "ps": "PostScript 文件", + "psd": "Photoshop 文件", + "py": "Python 文件", + "rar": "压缩文件", + "reg": "Windows 注册表文件", + "rm": "视频文件", + "rmvb": "视频文件", + "rom": "游戏 ROM 文件", + "rpm": "Red Hat Package Manager 安装包", + "rtf": "Rich Text Format 文档", + "sav": "存档文件", + "sh": "Shell 脚本文件", + "srt": "字幕文件", + "sql": "SQL 数据库文件", + "svg": "矢量图形文件", + "swf": "Flash 文件", + "tar": "压缩文件", + "tbz": "压缩文件", + "tbz2": "压缩文件", + "tgz": "压缩文件", + "tex": "TeX 文档", + "tga": "图像文件", + "tif": "图像文件", + "tiff": "图像文件", + "torrent": "BitTorrent 文件", + "ttf": "字体文件", + "txt": "纯文本文件", + "uue": "UUEncode 编码文件", + "vbs": "VBScript 文件", + "vcf": "联系人文件", + "wav": "音频文件", + "webm": "视频文件", + "wma": "音频文件", + "wmv": "视频文件", + "woff": "字体文件", + "wpd": "WordPerfect 文档", + "wps": "Microsoft Works 文档", + "xhtml": "网页文件", + "xls": "Excel 电子表格文件", + "xlsx": "Excel 电子表格文件", + "xml": "XML 文件", + "xpi": "Mozilla Firefox 扩展", + "xps": "Windows XPS 文档", + "yaml": "YAML 配置文件", + "yml": "YAML 配置文件", + "zip": "压缩文件", + "cmd": "命令行脚本文件", + "gadget": "Windows 桌面小工具", + "hlp": "帮助文件", + "ini": "配置文件", + "ins": "Internet 设置文件", + "isp": "Internet 设置文件", + "job": "计划任务文件", + "jse": "加密的 JavaScript 文件", + "lnk": "Windows 快捷方式", + "msc": "Microsoft 控制台文件", + "msp": "Windows Installer 补丁文件", + "mst": "Windows Installer 转换文件", + "nls": "国家化字符串文件", + "ocx": "ActiveX 控件文件", + "ops": "Office Profile Settings 文件", + "pcd": "Photo CD 图片文件", + "pif": "MS-DOS 程序快捷方式", + "prf": "Outlook 配置文件", + "pst": "Outlook 数据文件", + "scf": "Windows Explorer 命令文件", + "scr": "屏幕保护程序文件", + "sct": "Windows Script Component 文件", + "shb": "Windows 快捷方式对象文件", + "shs": "Shell Scrap Object 文件", + "sys": "系统文件", + "theme": "Windows 主题文件", + "tmp": "临时文件", + "url": "Internet 快捷方式", + "vb": "VBScript 文件", + "vbe": "VBScript 文件", + "vxd": "虚拟设备驱动程序文件", + "wsc": "Windows 脚本组件文件", + "wsf": "Windows 脚本文件", + "wsh": "Windows Script Host 设置文件", + "hta": "HTML 应用程序文件", + "cpl": "控制面板文件", + "msi": "Windows Installer 安装程序", + "nfo": "信息文本文件", + "odc": "Office 数据连接文件", + "p7b": "数字证书文件", + "spc": "数字证书文件", + "pem": "X.509 证书文件", + "cer": "X.509 证书文件", + "der": "X.509 证书文件", + "pfx": "个人信息交换格式文件", + "p12": "个人信息交换格式文件", + "pot": "PowerPoint 模板文件", + "pps": "PowerPoint 幻灯片演示文件", + "ps1": "Windows PowerShell 脚本文件", + "pub": "Publisher 文件", + "qbb": "QuickBooks 备份文件", + "qbw": "QuickBooks 公司文件", + "qxd": "QuarkXPress 文件", + "qxp": "QuarkXPress 文件", + "ra": "音频文件", + "ram": "音频文件", + "rb": "Ruby 文件", + "rdf": "RDF 文件", + "res": "资源文件", + "sdf": "Microsoft SQL Server CE 数据库文件", + "sln": "Visual Studio 解决方案文件", + "stl": "3D 模型文件", + "sub": "字幕文件", + "ssa": "字幕文件", + "ass": "字幕文件", + } + mime, ok := mimeMap[ext] + if !ok { + return "其他文件" + } + return mime +} + func (h *HttpServer) MIME(fullpath string) string { ext := filepath.Ext(filepath.Base(fullpath)) if len(ext) == 0 || ext == "." { diff --git a/httpserver/server.go b/httpserver/server.go index 9410193..afcbc31 100644 --- a/httpserver/server.go +++ b/httpserver/server.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/ioutil" + "math" "net" "net/http" "os" @@ -19,7 +20,7 @@ import ( "time" ) -var version = "2.0.0" +var version = "2.0.1" type HttpServerCfgs func(cfg *HttpServerCfg) @@ -42,6 +43,135 @@ type HttpServer struct { HttpServerCfg } +var htmlTitle string = ` + + + + B612 Http Server %s + + + +
+

B612 Http Server - %s

+

%s

%s + + + + + + + + + + ` + +var htmlTail = ` +
NameModifiedSizeType
+
+ + + + +` + func WithTLSCert(cert, key string) HttpServerCfgs { return func(cfg *HttpServerCfg) { cfg.key = key @@ -310,13 +440,18 @@ func (h *HttpServer) getFolder(log *starlog.StarLogger, w http.ResponseWriter, r if r.Method != "GET" { return nil } - w.Write([]byte("\n\n

B612 Http Server - " + version + "

")) + var upload string if h.uploadFolder != "" { - w.Write([]byte("Upload Web Page Is Openned!

")) + upload = `Upload Web Page Is Openned!` } - w.Write([]byte("
\n"))
+	w.Write([]byte(fmt.Sprintf(htmlTitle, r.URL.Path, version, r.URL.Path, upload)))
 	if r.URL.Path != "/" {
-		w.Write([]byte(fmt.Sprintf("

%s %s

\n", r.URL.Path+"/..", "..", "上层文件夹"))) + p := r.URL.Path + if p[len(p)-1:] != "/" { + p += "/" + } + w.Write([]byte(fmt.Sprintf(`%s%s%s%s`, + p+"..", "..", "-", "-", "上层文件夹"))) } if r.URL.Path == "/" { r.URL.Path = "" @@ -327,13 +462,15 @@ func (h *HttpServer) getFolder(log *starlog.StarLogger, w http.ResponseWriter, r for _, v := range dir { if v.Name() != "." || v.Name() != ".." { if !v.IsDir() { - w.Write([]byte(fmt.Sprintf("

%s %d %s

\n", r.URL.Path+"/"+v.Name(), v.Name(), int(v.Size()), v.ModTime().Format("2006-01-02 15:04:05")))) + w.Write([]byte(fmt.Sprintf(`%s%s%s%s`, + r.URL.Path+"/"+v.Name(), v.Name(), v.ModTime().Format("2006-01-02 15:04:05"), fmt.Sprintf("%d (%s)", v.Size(), h.trimSize(v.Size())), h.FileType(v.Name())))) } else { - w.Write([]byte(fmt.Sprintf("

%s %s %s

\n", r.URL.Path+"/"+v.Name(), v.Name(), "文件夹", v.ModTime().Format("2006-01-02 15:04:05")))) + w.Write([]byte(fmt.Sprintf(`%s%s%s%s`, + r.URL.Path+"/"+v.Name(), v.Name(), v.ModTime().Format("2006-01-02 15:04:05"), "-", "文件夹"))) } } } - w.Write([]byte("
\n")) + w.Write([]byte(htmlTail)) return nil } @@ -434,6 +571,21 @@ func (h *HttpServer) getFile(log *starlog.StarLogger, w http.ResponseWriter, r * return nil } +func (h *HttpServer) trimSize(size int64) string { + var tani string + tani = fmt.Sprintf("%v Byte", size) + if f64 := float64(size) / 1024; f64 > 1 { + tani = fmt.Sprintf("%.3f KB", math.Trunc(f64*1e3+0.5)*1e-3) + if f64 = float64(f64) / 1024; f64 > 1 { + tani = fmt.Sprintf("%.3f MB", math.Trunc(f64*1e3+0.5)*1e-3) + if f64 = float64(f64) / 1024; f64 > 1 { + tani = fmt.Sprintf("%.3f GB", math.Trunc(f64*1e3+0.5)*1e-3) + } + } + } + return tani +} + func (h *HttpServer) uploadFile(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { w.WriteHeader(200)