|
1 | | -import os |
2 | | - |
| 1 | +from flask import current_app |
3 | 2 | from werkzeug.utils import secure_filename |
4 | 3 |
|
5 | 4 | from lin.core import File |
|
9 | 8 | class LocalUploader(Uploader): |
10 | 9 |
|
11 | 10 | def upload(self, **kwargs): |
12 | | - ret = dict(file_storage=self._file_storage, file=[]) |
| 11 | + ret = [] |
| 12 | + site_domain = current_app.config.get('SITE_DOMAIN')\ |
| 13 | + if current_app.config.get('SITE_DOMAIN') else '127.0.0.1:5000' |
13 | 14 | for single in self._file_storage: |
14 | | - secure_filename(single.filename) |
15 | | - name = self._generate_uuid() + self._get_ext(single) |
16 | | - full_path = os.path.join(self._store_dir, name) |
17 | | - single.save(full_path) |
18 | | - file = File.create_file( |
19 | | - name=name, |
20 | | - path=full_path, |
21 | | - _type=1, |
22 | | - extension=self._get_ext(single), |
23 | | - size=self._get_size(single), |
24 | | - commit=True |
25 | | - ) |
26 | | - ret['file'].append(file) |
| 15 | + file_md5 = self._generate_md5(single.read()) |
| 16 | + single.seek(0) |
| 17 | + exists = File.query.filter_by(md5=file_md5).first() |
| 18 | + if exists: |
| 19 | + ret.append({ |
| 20 | + "key": single.filename, |
| 21 | + "id": exists.id, |
| 22 | + "url": site_domain + '/assets/' + exists.path |
| 23 | + }) |
| 24 | + else: |
| 25 | + absolute_path, relative_path, real_name = self._get_store_path(single.filename) |
| 26 | + secure_filename(single.filename) |
| 27 | + single.save(absolute_path) |
| 28 | + file = File.create_file( |
| 29 | + name=real_name, |
| 30 | + path=relative_path, |
| 31 | + extension=self._get_ext(single.filename), |
| 32 | + size=self._get_size(single), |
| 33 | + md5=file_md5, |
| 34 | + commit=True |
| 35 | + ) |
| 36 | + ret.append({ |
| 37 | + "key": single.filename, |
| 38 | + "id": file.id, |
| 39 | + "url": site_domain + '/assets/' + file.path |
| 40 | + }) |
27 | 41 | return ret |
0 commit comments