背景

起因是需要让产品在Axure生成的产品文档方便在内部共享,之前有很多的思路,最后还是走了最简单的一条路。在Linux上搭建一个Samba服务,让产品方便上传文档,开发查看通过自己写的Flask服务。

安装Samba

1、安装

1
2
sudo apt-get update
sudo apt-get install samba

2、创建共享目录

1
2
sudo mkdir /var/share
sudo chmod 777 /var/share

3、修改Samba配置

1
sudo vim /etc/samba/smb.conf

在配置文件最后加上

1
2
3
4
5
6
[share]
path = /var/share
browseable = yes
writable = yes
public = no
writable = yes

小贴士:public的设置是确认用户是否需要输入密码

设定public=no时候,设定访问用户:

1
2
sudo adduser test
sudo smbpasswd -a test

4、启动Samba

1
2
3
4
5
6
# 启动
sudo /etc/init.d/smbd start
# 重启
sudo /etc/init.d/smbd restart
# 关闭
sudo /etc/init.d/smbd shutdown

5、访问

Win环境访问:在我的电脑中选择映射网络浏览器,然后输入:

1
\\192.168.1.100\share

Mac环境访问:在Finder中选择菜单上的前往,然后选择连接服务器,然后输入,

1
smb://192.168.1.100/share

开发查看产品文档

我是通过Flask写了一个服务,然后将地址映射到共享的目录,注意到Axure生成的html文件夹都存在一个index.html,然后点击index.html就可以直接查看整个文档。
具体代码实现为:

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
#coding=utf-8
from __future__ import division
from flask import request, session, g, send_file, url_for, abort, render_template, flash, jsonify, make_response, Response, redirect
from werkzeug.utils import secure_filename
from werkzeug.exceptions import NotFound
from flask import Flask, request, make_response, send_file, safe_join, redirect, Response
import sys, os, cgi, urllib, posixpath, argparse
from StringIO import StringIO
from pypinyin import lazy_pinyin
import commands
import json
def init_router(app):
def show_directory(path):
list = os.listdir(path)
list.sort(key=lambda a: a.lower())
list = eval(json.dumps(list, ensure_ascii=False))
f = StringIO()
print path
path = str(path).strip(".")
displaypath = cgi.escape(path)
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write("<html>\n<title>产品文档</title>\n")
encoding = sys.getfilesystemencoding()
f.write('<meta http-equiv="Content-Type" content="text/html; charset=%s">' % encoding)
f.write("<body>\n<h2>最新产品文档列表</h2>\n")
f.write("<hr>\n<ul>\n")
for name in list:
fullname = path + name
displayname = linkname = name
if os.path.splitext(name)[1] in app.config["IGNORED"]:
continue
if name in app.config["IGNORED"]:
continue
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
if os.path.isdir(path + "/"+name):
if "index.html" in os.listdir(path + "/"+name):
linkname = name + "/index.html"
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(linkname), cgi.escape(displayname)))
else:
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(name), cgi.escape(displayname)))
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
resp = make_response(f.read())
return resp
@app.route('/<path:filename>')
@app.route('/', methods=['POST', 'GET'])
def show_file(filename=''):
global ROOT_DIR
ROOT_DIR = os.path.abspath('../../../share/')
print ROOT_DIR
if request.method == 'GET':
filename = safe_join(ROOT_DIR, "out_put/"+filename)
print filename
if os.path.isdir(filename):
return show_directory(filename)
if not os.path.isfile(filename):
raise NotFound()
return send_file(filename, as_attachment=False)
@app.before_request
def before_request():
create_path()
@app.after_request
def after_request(response):
return response
@app.errorhandler(404)
def url_not_found(error):
"""重新定义404报错"""
return make_response(jsonify({'error': 'URL occurs errors'}), 404)
@app.errorhandler(400)
def params_not_found(error):
"""重新定义400报错"""
return make_response(jsonify({'error': 'Params occurs errors'}), 400)
def get_locale():
return request.accept_languages.best_match(app.config['LANGUAGES'].keys())
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
def create_path():
upload_path = app.config["UPLOAD_FOLDER"]
if not os.path.exists(upload_path):
os.makedirs(upload_path)

开发查看

Flask的服务部署到和共享文档同在的Linux机器上。

小贴士:注意文件路径。

通过Supervisord部署好之后,开发查看产品文档就非常方便,只需要知道Flask服务的ipport即可,实现了局域网内共享。类如我们本地配置好的,:http://192.168.1.100:8001

总结

1、配置Samba共享服务
2、通过Flask写的服务查看