-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.py
161 lines (105 loc) · 4.45 KB
/
converter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from base import *
#from Model_Encoder import *
from flask_socketio import SocketIO,send
from server import *
import wget
import paramiko
proxies = {
'http': 'http://172.16.2.30:5000',
'https': 'http://172.16.2.30:5000',
}
socketio= SocketIO(app)
global name
name=''
@app.route('/')
def upl():
return render_template('upload.html')
#ROUTE WHERE COMPRESSION FUNCTION IS CALLED
@app.route('/uploader', methods=['GET' , 'POST'])
def uploader():
if request.method == 'POST':
url_dict = request.get_json() #RECEIVIES FILE URL/DATA (DROPBOX/DRIVE) FROM CLIENT SIDE
socketio.send('5') #Sending progress to client side(For Progress bar)
socketio.sleep(0)
if url_dict:
url=url_dict.get('link') #link is a json key here
filename = url.split("/")[-1]
#USUAL GET,WGET,URLLIB RESULTS IN META DATA LOSS(REASON:UNKNOWN) SO BYTES ARE WRITTEN DOWN
r = requests.get(url)
with open('uploads/'+filename,'wb') as f:
f.write(r.content)
f.close()
else:
f = request.files.get('file')
filename=secure_filename(f.filename)
f.save(os.path.join(app.config['UPLOADED_PATH'], filename))
f.close()
lstfilesDCM=[] #empty list(ext:COULD BE USED TO HANDLE MULTIPLE FILES)
global name
name=filename
socketio.send('13')
socketio.sleep(0)
if ".dcm" in filename.lower():
SCRIPT_DIR = '/home/miriad1c/compression/'
src_fold = os.path.abspath('/home/rakshith/dicom_converter/Dicom-fileconverter/uploads/')
dcm_file = filename
dest_fold = SCRIPT_DIR # <- don't change
if os.path.exists(os.path.join(src_fold, dcm_file)):
client = copy_to_and_from(None,
credential_dict = {'hostname': '10.9.7.9','username': 'miriad1c','password': 'sipiitkgp'},
src_dcm_file = os.path.join(src_fold, dcm_file),
dest_dcm_file = os.path.join(dest_fold, dcm_file)
)
socketio.send('35')
socketio.sleep(0)
run_compression(client, dcm_file)
client = copy_to_and_from(client,
src_dcm_file = os.path.join(src_fold, dcm_file.split('.')[0] + '.kmxm'),
dest_dcm_file = os.path.join(dest_fold, dcm_file.split('.')[0] + '.kmxm'),
to = False,
end_conn_on_finish = True)
if client is None:
print('connection ended')
#THIS IS TO DELETE THE FILES AFTER ITS USE/DOWNLOADED
@after_this_request
def remove_file(response):
try:
os.remove(app.config['UPLOADED_PATH']+'/'+filename)
except Exception as error:
app.logger.error("Error removing or closing downloaded file handle", error)
return response
socketio.send('80')
socketio.sleep(0)
socketio.send('100')
socketio.sleep(0)
socketio.send('Compressed 100')
socketio.sleep(0)
return redirect(url_for('preview'))
else:
return render_template('upload.html')
@app.route('/downloader',methods=['GET' , 'POST'])
def downloader():
conv_name=name[:-3]+'kmxm'
file_handle = open(app.config['UPLOADED_PATH']+'/'+conv_name, 'r')
@after_this_request
def remove_file(response):
try:
os.remove(app.config['UPLOADED_PATH']+'/'+conv_name)
except Exception as error:
app.logger.error("Error removing or closing downloaded file handle", error)
return response
return send_file(file_handle, as_attachment='True',attachment_filename=conv_name)
@app.route('/script_download')
def script_download():
return send_from_directory('/home/rakshith/dicom_converter/Dicom-fileconverter/downloadables','czb_to_dcm.tar.gz', as_attachment='True')
@app.route('/preview',methods=['GET','POST'])
def preview():
return render_template('upload.html')
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_error(error):
return render_template('500.html'), 500
if __name__ == '__main__':
socketio.run(app)