File size: 5,714 Bytes
efe92b5
e4956d2
919170c
bbf8af3
4d8577b
459e040
 
 
 
4d8577b
e085d41
 
 
4d8577b
 
919170c
e085d41
 
 
 
 
0c799ac
f3b0cae
04e49a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4956d2
f3b0cae
 
 
 
 
 
 
 
5ef1604
 
 
 
4d8577b
e085d41
04e49a6
5bfbaae
5ef1604
 
 
 
 
 
 
 
 
 
4d8577b
5bfbaae
 
 
 
 
 
 
 
5ef1604
5bfbaae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ef1604
 
 
 
 
 
 
4d8577b
5ef1604
5bfbaae
f3b0cae
0c799ac
ae0ce11
459e040
f3b0cae
459e040
e085d41
 
 
50ec61c
e085d41
459e040
 
f3b0cae
 
 
 
 
4d8577b
b39ad01
0c799ac
459e040
f3b0cae
 
e8539ed
4d8577b
e8539ed
e085d41
 
50ec61c
e085d41
50ec61c
e085d41
50ec61c
ae0ce11
e085d41
 
 
b9de94c
e085d41
 
 
 
 
 
 
 
04e49a6
e085d41
5ef1604
4d8577b
e085d41
b9de94c
 
5ef1604
 
e085d41
 
5ef1604
fc0cce5
 
0f0189d
e8539ed
 
f3b0cae
e8539ed
 
 
 
 
f3b0cae
e085d41
 
459e040
19c209b
e4cf850
f3b0cae
4d8577b
 
 
f3b0cae
e4cf850
 
f3b0cae
e085d41
e8539ed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
48
49
50
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
130
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
FROM ymlisoft/kkfileview

USER root

# 1. 安装必要依赖
RUN apt-get update && apt-get install -y \
    nginx \
    apache2-utils \
    xvfb \
    curl \
    libfreetype6 \
    fontconfig \
    fonts-wqy-zenhei \
    && rm -rf /var/lib/apt/lists/* \
    && fc-cache -fv

# 2. 环境变量配置
ENV SERVER_PORT=8012
ENV KKFILEVIEW_SECURITY_TRUST_HOST=default
ENV AUTH_USERNAME=admin
ENV AUTH_PASSWORD=yourpassword

# 3. JVM 配置
# - 堆内存: 4GB
# - G1GC: 低延迟垃圾回收
# - 元空间: 256MB
ENV JAVA_OPTS="-server \
-Xms4g \
-Xmx4g \
-XX:+UseG1GC \
-XX:MaxGCPauseMillis=200 \
-XX:MetaspaceSize=256m \
-XX:MaxMetaspaceSize=512m \
-Djava.awt.headless=true \
-Dsun.java2d.fontpath=/usr/share/fonts \
-Dfile.encoding=UTF-8 \
-Dserver.tomcat.threads.max=16 \
-Dserver.tomcat.threads.min-spare=4 \
-Dserver.tomcat.accept-count=50 \
-Dserver.tomcat.max-connections=200"

# 4. LibreOffice 配置
ENV office.plugin.server.ports=2001,2002,2003,2004,2005
ENV office.plugin.task.timeout=5m
ENV office.preview.type=pdf
ENV office.timeout=300000
ENV file.dir=/opt/kkFileView/file/

# 5. Nginx 配置
RUN rm -rf /etc/nginx/sites-enabled/* /etc/nginx/conf.d/* && \
    printf 'server {\n\
    listen 7860 default_server;\n\
    server_name _;\n\
    \n\
    client_max_body_size 500M;\n\
    \n\
    # 静态文件目录\n\
    location ~ ^/(demo|file|static)/ {\n\
        auth_basic off;\n\
        proxy_pass http://127.0.0.1:8012;\n\
        proxy_set_header Host $http_host;\n\
        proxy_set_header X-Real-IP $remote_addr;\n\
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;\n\
        proxy_buffering off;\n\
        proxy_read_timeout 300s;\n\
    }\n\
    \n\
    # 健康检查端点\n\
    location /health {\n\
        auth_basic off;\n\
        return 200 "OK";\n\
        add_header Content-Type text/plain;\n\
    }\n\
    \n\
    # 主要预览路径 - 基于 Referer 白名单\n\
    location / {\n\
        set $auth_required "Restricted Access";\n\
        \n\
        # 替换为域名\n\
        if ($http_referer ~* "^https?://.*\\.cloudpaste\\.qzz\\.io") {\n\
            set $auth_required off;\n\
        }\n\
        \n\
        # 部署在 HuggingFace Spaces\n\
        if ($http_referer ~* "^https?://.*\\.hf\\.space") {\n\
            set $auth_required off;\n\
        }\n\
        \n\
        # 开发环境支持\n\
        if ($http_referer ~* "^https?://localhost") {\n\
            set $auth_required off;\n\
        }\n\
        if ($http_referer ~* "^https?://127\\.0\\.0\\.1") {\n\
            set $auth_required off;\n\
        }\n\
        \n\
        auth_basic $auth_required;\n\
        auth_basic_user_file /etc/nginx/.htpasswd;\n\
        \n\
        proxy_pass http://127.0.0.1:8012;\n\
        proxy_set_header Host $http_host;\n\
        proxy_set_header X-Real-IP $remote_addr;\n\
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n\
        proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;\n\
        proxy_buffering off;\n\
        proxy_read_timeout 300s;\n\
    }\n\
}\n' > /etc/nginx/conf.d/kkfileview.conf
  
# 6. 启动脚本
RUN printf '#!/bin/bash\n\
set -e\n\
\n\
echo "=== Starting KKFileView (EPS + CAD Fix) for HuggingFace Spaces ==="\n\
\n\
# 查找 KKFileView 启动脚本\n\
STARTUP_SCRIPT=$(find /opt -name "startup.sh" -o -name "kkFileView" | grep "bin/" | head -n 1)\n\
KK_ROOT_DIR=$(dirname $(dirname "$STARTUP_SCRIPT"))\n\
\n\
# 生成 htpasswd 文件\n\
htpasswd -cb /etc/nginx/.htpasswd "$AUTH_USERNAME" "$AUTH_PASSWORD"\n\
\n\
# 创建文件存储目录\n\
mkdir -p /opt/kkFileView/file\n\
chmod 755 /opt/kkFileView/file\n\
\n\
# 启动 Xvfb\n\
echo "Starting Xvfb..."\n\
Xvfb :99 -screen 0 1024x768x24 >/dev/null 2>&1 &\n\
export DISPLAY=:99\n\
sleep 2\n\
echo "✓ Xvfb started on DISPLAY=:99"\n\
\n\
# 启动 KKFileView)\n\
echo "Starting KKFileView on port 8012..."\n\
echo "LibreOffice will be managed by KKFileView automatically"\n\
export JAVA_OPTS="$JAVA_OPTS"\n\
\n\
if [[ "$STARTUP_SCRIPT" == *".sh" ]]; then\n\
    bash "$STARTUP_SCRIPT" 2>&1 | tee /var/log/kkfileview.log &\n\
else\n\
    "$STARTUP_SCRIPT" --server.port=8012 2>&1 | tee /var/log/kkfileview.log &\n\
fi\n\
\n\
# 等待日志文件生成\n\
echo "Waiting for log file..."\n\
LOG_FILE="$KK_ROOT_DIR/log/kkFileView.log"\n\
for i in {1..30}; do\n\
    if [ -f "$LOG_FILE" ]; then\n\
        echo "✓ Found log file: $LOG_FILE"\n\
        tail -f "$LOG_FILE" &\n\
        break\n\
    fi\n\
    sleep 1\n\
done\n\
\n\
# 等待 KKFileView 启动\n\
echo "Waiting for KKFileView to start..."\n\
for i in {1..60}; do\n\
    if curl -s http://127.0.0.1:8012 >/dev/null 2>&1; then\n\
        echo "✓ KKFileView is ready on port 8012!"\n\
        break\n\
    fi\n\
    if [ $i -eq 60 ]; then\n\
        echo "ERROR: KKFileView failed to start within 120 seconds"\n\
        exit 1\n\
    fi\n\
    echo "Waiting... ($i/60)"\n\
    sleep 2\n\
done\n\
\n\
# 验证 LibreOffice\n\
echo "Checking LibreOffice status..."\n\
LIBREOFFICE_COUNT=$(ps aux | grep soffice | grep -v grep | wc -l)\n\
if [ $LIBREOFFICE_COUNT -gt 0 ]; then\n\
    echo "✓ LibreOffice processes running: $LIBREOFFICE_COUNT"\n\
else\n\
    echo "⚠ LibreOffice not detected (will be started on first use)"\n\
fi\n\
\n\
# 启动 Nginx\n\
echo "Starting Nginx on port 7860..."\n\
nginx -g "daemon off;"\n' > /start.sh && \
    chmod +x /start.sh

# 7. 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:7860/health || exit 1

# 8. 暴露端口
EXPOSE 7860

# 9. 启动命令
ENTRYPOINT []
CMD ["/start.sh"]