Show More
@@ -1,57 +1,57 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2016-2020 RhodeCode GmbH |
|
3 | # Copyright (C) 2016-2020 RhodeCode GmbH | |
4 | # |
|
4 | # | |
5 | # This program is free software: you can redistribute it and/or modify |
|
5 | # This program is free software: you can redistribute it and/or modify | |
6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
7 | # (only), as published by the Free Software Foundation. |
|
7 | # (only), as published by the Free Software Foundation. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU Affero General Public License |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
16 | # |
|
16 | # | |
17 | # This program is dual-licensed. If you wish to learn more about the |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
21 | import io |
|
21 | import io | |
22 | import uuid |
|
22 | import uuid | |
23 |
import pathlib |
|
23 | import pathlib | |
24 |
|
24 | |||
25 |
|
25 | |||
26 | def get_file_storage(settings): |
|
26 | def get_file_storage(settings): | |
27 | from rhodecode.apps.file_store.backends.local_store import LocalFileStorage |
|
27 | from rhodecode.apps.file_store.backends.local_store import LocalFileStorage | |
28 | from rhodecode.apps.file_store import config_keys |
|
28 | from rhodecode.apps.file_store import config_keys | |
29 | store_path = settings.get(config_keys.store_path) |
|
29 | store_path = settings.get(config_keys.store_path) | |
30 | return LocalFileStorage(base_path=store_path) |
|
30 | return LocalFileStorage(base_path=store_path) | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | def splitext(filename): |
|
33 | def splitext(filename): | |
34 |
ext = ''.join(pathlib |
|
34 | ext = ''.join(pathlib.Path(filename).suffixes) | |
35 | return filename, ext |
|
35 | return filename, ext | |
36 |
|
36 | |||
37 |
|
37 | |||
38 | def uid_filename(filename, randomized=True): |
|
38 | def uid_filename(filename, randomized=True): | |
39 | """ |
|
39 | """ | |
40 | Generates a randomized or stable (uuid) filename, |
|
40 | Generates a randomized or stable (uuid) filename, | |
41 | preserving the original extension. |
|
41 | preserving the original extension. | |
42 |
|
42 | |||
43 | :param filename: the original filename |
|
43 | :param filename: the original filename | |
44 | :param randomized: define if filename should be stable (sha1 based) or randomized |
|
44 | :param randomized: define if filename should be stable (sha1 based) or randomized | |
45 | """ |
|
45 | """ | |
46 |
|
46 | |||
47 | _, ext = splitext(filename) |
|
47 | _, ext = splitext(filename) | |
48 | if randomized: |
|
48 | if randomized: | |
49 | uid = uuid.uuid4() |
|
49 | uid = uuid.uuid4() | |
50 | else: |
|
50 | else: | |
51 | hash_key = '{}.{}'.format(filename, 'store') |
|
51 | hash_key = '{}.{}'.format(filename, 'store') | |
52 | uid = uuid.uuid5(uuid.NAMESPACE_URL, hash_key) |
|
52 | uid = uuid.uuid5(uuid.NAMESPACE_URL, hash_key) | |
53 | return str(uid) + ext.lower() |
|
53 | return str(uid) + ext.lower() | |
54 |
|
54 | |||
55 |
|
55 | |||
56 | def bytes_to_file_obj(bytes_data): |
|
56 | def bytes_to_file_obj(bytes_data): | |
57 | return io.StringIO(bytes_data) |
|
57 | return io.StringIO(bytes_data) |
General Comments 0
You need to be logged in to leave comments.
Login now