def get_uploaded_dates(self, program: Program): self.open_ssh_archive() ftp_client = self.ssh_client.open_sftp() try: ftp_client.chdir(settings.ARCHIVE_SERVER_UPLOAD_DIRECTORY + program.normalized_name()) except IOError: return [] files = ftp_client.listdir() self.close_connections() dates = [] for file in files: date_weekday = file[:-4].replace(program.normalized_name(), "") if len(date_weekday) == 9: # has weekday without_weekday = date_weekday[:-1] dates.append(without_weekday[:4] + "-" + without_weekday[4:6] + "-" + without_weekday[6:]) else: dates.append(date_weekday[:4] + "-" + date_weekday[4:6] + "-" + date_weekday[6:]) return dates
def save_file(uploaded_file, emission_date: str, program: Program): uploaded_file.name = "uploaded_" + uploaded_file.name uploaded_file_path = settings.FILE_UPLOAD_DIR + uploaded_file.name output_file_path = settings.FILE_UPLOAD_DIR + program.get_filename_for_date( emission_date) if os.path.isfile(uploaded_file_path) or os.path.isfile( output_file_path): raise FileBeingProcessedException upload_service = RemoteService() upload_service.check_file_for_date(program.normalized_name(), emission_date) with open(uploaded_file_path, 'wb+') as destination: for chunk in uploaded_file.chunks(): destination.write(chunk)
def download_archive_file(self, program: Program, emission_date: str): filename = program.get_filename_for_date(emission_date) archive_url = "http://{}".format(settings.ARCHIVE_SERVER_IP) session = requests.Session() session.auth = (settings.ARCHIVE_SERVER_USERNAME, settings.ARCHIVE_SERVER_PASSWORD) session.post(archive_url) r = session.get(archive_url + '/archive/' + program.normalized_name() + "/" + filename, stream=True) resp = StreamingHttpResponse(r.iter_content(8096)) resp['headers'] = "X-Accel-Redirect" resp['Content-Disposition'] = 'attachment; filename={}'.format( filename) resp['Accept-Ranges'] = 'bytes' resp['Content-Type'] = 'audio/mpeg' return resp