kapsikkum-unmanic – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | #!/usr/bin/env python3 |
2 | # -*- coding: utf-8 -*- |
||
3 | |||
4 | """ |
||
5 | unmanic.downloads.py |
||
6 | |||
7 | Written by: Josh.5 <jsunnex@gmail.com> |
||
8 | Date: 31 Oct 2021, (4:41 PM) |
||
9 | |||
10 | Copyright: |
||
11 | Copyright (C) Josh Sunnex - All Rights Reserved |
||
12 | |||
13 | Permission is hereby granted, free of charge, to any person obtaining a copy |
||
14 | of this software and associated documentation files (the "Software"), to deal |
||
15 | in the Software without restriction, including without limitation the rights |
||
16 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
17 | copies of the Software, and to permit persons to whom the Software is |
||
18 | furnished to do so, subject to the following conditions: |
||
19 | |||
20 | The above copyright notice and this permission notice shall be included in all |
||
21 | copies or substantial portions of the Software. |
||
22 | |||
23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
||
26 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
||
27 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
||
28 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE |
||
29 | OR OTHER DEALINGS IN THE SOFTWARE. |
||
30 | |||
31 | """ |
||
32 | import os |
||
33 | import threading |
||
34 | import time |
||
35 | import uuid |
||
36 | |||
37 | from tornado import iostream, web |
||
38 | |||
39 | from unmanic.libs.singleton import SingletonType |
||
40 | |||
41 | |||
42 | class DownloadsLinks(object, metaclass=SingletonType): |
||
43 | _download_links = {} |
||
44 | |||
45 | def __remove_expired(self): |
||
46 | """ |
||
47 | Find and remove expired links |
||
48 | |||
49 | :return: |
||
50 | """ |
||
51 | time_now = time.time() |
||
52 | keys = [t for t in self._download_links] |
||
53 | lock = threading.RLock() |
||
54 | with lock: |
||
55 | for k in keys: |
||
56 | if k in self._download_links: |
||
57 | if self._download_links[k].get('expires', 0) < time_now: |
||
58 | # Item has expired. Remove this item |
||
59 | del self._download_links[k] |
||
60 | |||
61 | def generate_download_link(self, link_data): |
||
62 | link_id = str(uuid.uuid4()) |
||
63 | lock = threading.RLock() |
||
64 | with lock: |
||
65 | # Expire in 1 min |
||
66 | link_data['expires'] = (time.time() + 60) |
||
67 | self._download_links[link_id] = link_data |
||
68 | return link_id |
||
69 | |||
70 | def get_download_link(self, link_id): |
||
71 | # Find and remove expired links |
||
72 | self.__remove_expired() |
||
73 | return self._download_links.get(link_id, {}) |
||
74 | |||
75 | |||
76 | class DownloadsHandler(web.RequestHandler): |
||
77 | |||
78 | async def get(self, link_id): |
||
79 | |||
80 | # Fetch link from |
||
81 | download_links = DownloadsLinks() |
||
82 | link_data = download_links.get_download_link(link_id) |
||
83 | # Set file details |
||
84 | abspath = link_data.get('abspath', '') |
||
85 | basename = link_data.get('basename', '') |
||
86 | # Return 404 on file not found |
||
87 | if not os.path.exists(abspath): |
||
88 | # Link ID must not be valid |
||
89 | self.write_error(404) |
||
90 | return |
||
91 | |||
92 | self.set_header('Content-Type', 'application/octet-stream') |
||
93 | self.set_header('Content-Disposition', 'attachment; filename={}'.format(basename)) |
||
94 | |||
95 | # Serve file download in 1MB chunks |
||
96 | with open(abspath, 'rb') as f: |
||
97 | while True: |
||
98 | data = f.read(1024 * 1024) |
||
99 | if not data: |
||
100 | break |
||
101 | try: |
||
102 | self.write(data) |
||
103 | await self.flush() |
||
104 | except iostream.StreamClosedError: |
||
105 | break |
||
106 | finally: |
||
107 | del data |