From cfb99fa926ec177c57e54d8784a59648a3fe2758 Mon Sep 17 00:00:00 2001 From: Xabi Spacebiker Date: Mon, 2 Oct 2017 23:42:11 +0200 Subject: [PATCH 1/4] Update pypofile.py 1st- Changes the file permissions of cache files from 444 to 644 2nd- Put the host variables into a single array, this way we can pass the full variable to the report_file_size_and_md5_to_airtime function. --- python_apps/pypo/pypo/pypofile.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/python_apps/pypo/pypo/pypofile.py b/python_apps/pypo/pypo/pypofile.py index 9363bf6db..17de17c53 100644 --- a/python_apps/pypo/pypo/pypofile.py +++ b/python_apps/pypo/pypo/pypofile.py @@ -35,7 +35,7 @@ class PypoFile(Thread): """ Copy media_item from local library directory to local cache directory. """ - src = media_item['uri'] + src = media_item['uri'] dst = media_item['dst'] src_size = media_item['filesize'] @@ -62,12 +62,14 @@ class PypoFile(Thread): try: CONFIG_SECTION = "general" username = self._config.get(CONFIG_SECTION, 'api_key') - host = self._config.get(CONFIG_SECTION, 'base_url') - port = self._config.get(CONFIG_SECTION, 'base_port', 80) - url = "%s://%s:%s/rest/media/%s/download" % (str(("http", "https")[int(port) == 443]), - host, - port, + host = [str(("http", "https")[int(self._config.get(CONFIG_SECTION, 'base_port', 80)) == 443]), + self._config.get(CONFIG_SECTION, 'base_url'), + self._config.get(CONFIG_SECTION, 'base_port', 80)] + + url = "%s://%s:%s/rest/media/%s/download" % (host[0], + host[1], + host[2], media_item["id"]) self.logger.info(url) with open(dst, "wb") as handle: @@ -87,7 +89,7 @@ class PypoFile(Thread): os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) if media_item['filesize'] == 0: - file_size = self.report_file_size_and_md5_to_airtime(dst, media_item["id"], host, username) + file_size = self.report_file_size_and_md5_to_airtime(dst, media_item["id"], host[2], username) media_item["filesize"] = file_size media_item['file_ready'] = True @@ -95,7 +97,7 @@ class PypoFile(Thread): self.logger.error("Could not copy from %s to %s" % (src, dst)) self.logger.error(e) - def report_file_size_and_md5_to_airtime(self, file_path, file_id, host_name, api_key): + def report_file_size_and_md5_to_airtime(self, file_path, file_id, host, api_key): try: file_size = os.path.getsize(file_path) @@ -114,8 +116,9 @@ class PypoFile(Thread): # Make PUT request to Airtime to update the file size and hash error_msg = "Could not update media file %s with file size and md5 hash" % file_id - try: - put_url = "http://%s/rest/media/%s" % (host_name, file_id) + + try: + put_url = "%s://%s:%s/rest/media/%s" % (host[0],host[1],host[2], file_id) payload = json.dumps({'filesize': file_size, 'md5': md5_hash}) response = requests.put(put_url, data=payload, auth=requests.auth.HTTPBasicAuth(api_key, '')) if not response.ok: @@ -214,4 +217,3 @@ class PypoFile(Thread): self.logger.error('PypoFile Exception: %s', top) time.sleep(5) self.logger.info('PypoFile thread exiting') - From 216294428ae931c92b83142a3addb162e3670f68 Mon Sep 17 00:00:00 2001 From: Xabi Spacebiker Date: Tue, 3 Oct 2017 01:28:01 +0200 Subject: [PATCH 2/4] Update pypofile.py --- python_apps/pypo/pypo/pypofile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python_apps/pypo/pypo/pypofile.py b/python_apps/pypo/pypo/pypofile.py index 17de17c53..673b9d1af 100644 --- a/python_apps/pypo/pypo/pypofile.py +++ b/python_apps/pypo/pypo/pypofile.py @@ -89,7 +89,7 @@ class PypoFile(Thread): os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) if media_item['filesize'] == 0: - file_size = self.report_file_size_and_md5_to_airtime(dst, media_item["id"], host[2], username) + file_size = self.report_file_size_and_md5_to_airtime(dst, media_item["id"], host, username) media_item["filesize"] = file_size media_item['file_ready'] = True From 5160fc64d709184f85857e544de376c3042c9c07 Mon Sep 17 00:00:00 2001 From: Xabi Spacebiker Date: Tue, 3 Oct 2017 03:37:06 +0200 Subject: [PATCH 3/4] Update pypofile.py Last update --- python_apps/pypo/pypo/pypofile.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/python_apps/pypo/pypo/pypofile.py b/python_apps/pypo/pypo/pypofile.py index 673b9d1af..ba2fea1c0 100644 --- a/python_apps/pypo/pypo/pypofile.py +++ b/python_apps/pypo/pypo/pypofile.py @@ -35,7 +35,7 @@ class PypoFile(Thread): """ Copy media_item from local library directory to local cache directory. """ - src = media_item['uri'] + src = media_item['uri'] dst = media_item['dst'] src_size = media_item['filesize'] @@ -59,19 +59,20 @@ class PypoFile(Thread): if do_copy: self.logger.info("copying from %s to local cache %s" % (src, dst)) - try: - CONFIG_SECTION = "general" - username = self._config.get(CONFIG_SECTION, 'api_key') - host = [str(("http", "https")[int(self._config.get(CONFIG_SECTION, 'base_port', 80)) == 443]), - self._config.get(CONFIG_SECTION, 'base_url'), - self._config.get(CONFIG_SECTION, 'base_port', 80)] + CONFIG_SECTION = "general" + username = self._config.get(CONFIG_SECTION, 'api_key') + baseurl = self._config.get(CONFIG_SECTION, 'base_url') + port = self._config.get(CONFIG_SECTION, 'base_port', 80) + protocol = self._config.get(CONFIG_SECTION, 'protocol', str(("http", "https")[int(port) == 443])) + + try: + host = [protocol, baseurl, port] url = "%s://%s:%s/rest/media/%s/download" % (host[0], host[1], host[2], media_item["id"]) - self.logger.info(url) with open(dst, "wb") as handle: response = requests.get(url, auth=requests.auth.HTTPBasicAuth(username, ''), stream=True, verify=False) @@ -85,7 +86,7 @@ class PypoFile(Thread): handle.write(chunk) - #make file world readable + #make file world readable and owner writable os.chmod(dst, stat.S_IRUSR | stat.S_IWUSR | stat.S_IRGRP | stat.S_IROTH) if media_item['filesize'] == 0: @@ -116,9 +117,8 @@ class PypoFile(Thread): # Make PUT request to Airtime to update the file size and hash error_msg = "Could not update media file %s with file size and md5 hash" % file_id - - try: - put_url = "%s://%s:%s/rest/media/%s" % (host[0],host[1],host[2], file_id) + try: + put_url = "%s://%s:%s/rest/media/%s" % (host[0], host[1], host[2], file_id) payload = json.dumps({'filesize': file_size, 'md5': md5_hash}) response = requests.put(put_url, data=payload, auth=requests.auth.HTTPBasicAuth(api_key, '')) if not response.ok: From 6e35cfb44b8a215674d01dc0b380db31c5ddae2d Mon Sep 17 00:00:00 2001 From: Lucas Bickel Date: Sun, 8 Oct 2017 16:14:33 +0200 Subject: [PATCH 4/4] Fix whitespace and move config getting into try --- python_apps/pypo/pypo/pypofile.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/python_apps/pypo/pypo/pypofile.py b/python_apps/pypo/pypo/pypofile.py index ba2fea1c0..14c8fba9c 100644 --- a/python_apps/pypo/pypo/pypofile.py +++ b/python_apps/pypo/pypo/pypofile.py @@ -61,14 +61,13 @@ class PypoFile(Thread): self.logger.info("copying from %s to local cache %s" % (src, dst)) CONFIG_SECTION = "general" - username = self._config.get(CONFIG_SECTION, 'api_key') - baseurl = self._config.get(CONFIG_SECTION, 'base_url') - port = self._config.get(CONFIG_SECTION, 'base_port', 80) - protocol = self._config.get(CONFIG_SECTION, 'protocol', str(("http", "https")[int(port) == 443])) - - try: - host = [protocol, baseurl, port] + try: + username = self._config.get(CONFIG_SECTION, 'api_key') + baseurl = self._config.get(CONFIG_SECTION, 'base_url') + port = self._config.get(CONFIG_SECTION, 'base_port', 80) + protocol = self._config.get(CONFIG_SECTION, 'protocol', str(("http", "https")[int(port) == 443])) + host = [protocol, baseurl, port] url = "%s://%s:%s/rest/media/%s/download" % (host[0], host[1], host[2],