aboutsummaryrefslogtreecommitdiffstats
path: root/nlp/scripts/upload.py
blob: febf880bd6bd51aa6e1f93cec151693302abca0b (plain)
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
#!/usr/bin/env python
# coding: utf-8

# auther = 'zhaoyehua'
# date = 20210918


from flask import Flask, request
import zipfile
import shutil
import os

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'upload/'


@app.route("/uploader", methods=["POST"])
def uploader():
    print("aa")
    if request.method == 'POST':
        f = request.files['file']
        print(f.filename)
        f.save(os.path.join(app.config['UPLOAD_FOLDER'], f.filename))
        return "ok"


@app.route("/unzipFile/<fileName>", methods=["GET"])
def unzipFile(fileName):
    zip_file = zipfile.ZipFile(os.path.join(app.config['UPLOAD_FOLDER'], fileName))
    parentDir = os.path.join(app.config['UPLOAD_FOLDER'], fileName.split(".zip")[0])
    if os.path.isdir(parentDir):
        pass
    else:
        os.mkdir(parentDir)
    for names in zip_file.namelist():
        zip_file.extract(names, parentDir)
    zip_file.close()
    return parentDir


@app.route("/deleteFile/<fileName>", methods=["GET"])
def deleteFile(fileName):
    path = os.path.join(app.config['UPLOAD_FOLDER'], fileName)
    if os.path.exists(path):
        os.remove(path)
        dirPath = path.split(".zip")[0]
        if os.path.isdir(dirPath):
            shutil.rmtree(dirPath)
            return "ok"
        return "ok"
    return "error: no such file"


if __name__ == '__main__':
    app.run(host="0.0.0.0", port=33013)