mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 14:53:44 +08:00
1 parent
dc59aefeb7
commit
e8c3f355d7
3 files changed
+44
-6
No files matched your search
@@ -8,7 +8,7 @@ require (
|
||||
github.com/go-text/typesetting v0.3.5
|
||||
github.com/tdewolff/canvas v0.0.0-20260923214215-09804640d00c
|
||||
github.com/tdewolff/font v0.0.0-20261010204837-88a7f6b56918
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261017102430-5d7ee9a3b5aa
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261017114758-498e989ad05f
|
||||
github.com/xiaoqidun/j2kgo v1.0.2
|
||||
github.com/xiaoqidun/jbig2 v1.1.8
|
||||
github.com/xiaoqidun/pdfgo v0.0.0-20261016003351-18348c283ac1
|
||||
|
||||
@@ -58,8 +58,8 @@ github.com/tdewolff/parse/v2 v2.8.16 h1:bLk5svUOQRkW/Y2SJ+DeENSIkZBcTIkq+Atyv5D8
|
||||
github.com/tdewolff/parse/v2 v2.8.16/go.mod h1:XdsoSFThlVIRIajAuqz1evNY7bagZS8LBOPA3aVopwQ=
|
||||
github.com/tdewolff/test v1.0.12 h1:7F21DqIajswxuche0geHdrUZRCWE4oko4b7bcmkkrxk=
|
||||
github.com/tdewolff/test v1.0.12/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261017102430-5d7ee9a3b5aa h1:Tc0xttOsC6R9mAxbvdcHoJ4hDbnuaiPBvPQVGTIyoS0=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261017102430-5d7ee9a3b5aa/go.mod h1:QAHK2Sa/KvY6dO+6dtvVUeOXVQO4Zh8R5oSSU+av7kE=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261017114758-498e989ad05f h1:LReZHupfHnvaOvjGMdPShKDArLMOEF5Fwm8HjfOR9n4=
|
||||
github.com/xiaoqidun/guigo v0.0.0-20261017114758-498e989ad05f/go.mod h1:QAHK2Sa/KvY6dO+6dtvVUeOXVQO4Zh8R5oSSU+av7kE=
|
||||
github.com/xiaoqidun/j2kgo v1.0.2 h1:OAjjEOfaBaQzBNcj9wx+eM68UI5pewR6SzNZa1R1DIw=
|
||||
github.com/xiaoqidun/j2kgo v1.0.2/go.mod h1:DCwAfuDzCDYqUiAlZ7BDwigTjaXw7o9L1FtEma/D1aA=
|
||||
github.com/xiaoqidun/jbig2 v1.1.8 h1:dERPW7F8gs18a/DOD9gXHR3fP1Vl6KhCNsLnq5l3BIo=
|
||||
|
||||
@@ -22,6 +22,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"math/big"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -62,13 +63,50 @@ type fileWriter struct {
|
||||
|
||||
// newFileStore 创建独立文件授权表
|
||||
func newFileStore() (*fileStore, error) {
|
||||
dir, err := os.MkdirTemp("", "ofdgo-webview-")
|
||||
dir, err := temporaryDirectory()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &fileStore{entries: make(map[string]*fileEntry), writers: make(map[string]*fileWriter), temporary: dir}, nil
|
||||
}
|
||||
|
||||
// temporaryDirectory 创建独立临时目录
|
||||
// 返回: string 目录路径, error 创建错误
|
||||
func temporaryDirectory() (string, error) {
|
||||
for i := 0; i < 100; i++ {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(10000000))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := filepath.Join(os.TempDir(), fmt.Sprintf("ofdgo-%07d", n.Int64()))
|
||||
err = os.Mkdir(path, 0700)
|
||||
if os.IsExist(err) {
|
||||
continue
|
||||
}
|
||||
return path, err
|
||||
}
|
||||
return "", os.ErrExist
|
||||
}
|
||||
|
||||
// temporaryFile 创建临时文件
|
||||
// 入参: dir 所在目录, prefix 文件名前缀
|
||||
// 返回: *os.File 临时文件, error 创建错误
|
||||
func temporaryFile(dir, prefix string) (*os.File, error) {
|
||||
for i := 0; i < 100; i++ {
|
||||
n, err := rand.Int(rand.Reader, big.NewInt(10000000))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
path := filepath.Join(dir, fmt.Sprintf("%s%07d.tmp", prefix, n.Int64()))
|
||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
|
||||
if os.IsExist(err) {
|
||||
continue
|
||||
}
|
||||
return file, err
|
||||
}
|
||||
return nil, os.ErrExist
|
||||
}
|
||||
|
||||
// add 登记已有授权文件
|
||||
func (s *fileStore) add(name string, directory bool) (map[string]any, error) {
|
||||
info, err := os.Stat(name)
|
||||
@@ -289,7 +327,7 @@ func (s *fileStore) begin(id string) (*fileWriter, error) {
|
||||
if entry == nil || entry.directory || entry.path == "" {
|
||||
return nil, fmt.Errorf("输出授权无效")
|
||||
}
|
||||
file, err := os.CreateTemp(filepath.Dir(entry.path), ".ofdgo-*")
|
||||
file, err := temporaryFile(filepath.Dir(entry.path), filepath.Base(entry.path)+".")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -411,7 +449,7 @@ func (w *fileWriter) release() {
|
||||
func (s *fileStore) serve(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
if r.URL.Path == "/__native/input" && r.Method == http.MethodPost {
|
||||
file, err := os.CreateTemp(s.temporary, "input-")
|
||||
file, err := temporaryFile(s.temporary, "input-")
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user