Skip to content

Commit 80a9253

Browse files
committed
code till file upload working
1 parent c79046d commit 80a9253

File tree

5 files changed

+119
-25
lines changed

5 files changed

+119
-25
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ android {
2323
}
2424
packagingOptions {
2525
exclude 'org/eclipse/jetty/http/encoding.properties'
26+
resources.excludes.add("META-INF/*")
2627
}
2728
}
2829

@@ -33,4 +34,5 @@ dependencies {
3334
testImplementation 'junit:junit:4.12'
3435
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
3536
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
37+
api 'com.github.ok2c.hc5.android:httpclient-android:0.2.0'
3638
}

app/src/main/java/com/googleresearch/capturesync/MainActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ private void handleWebSocketMsg(@NotNull WsMessageContext wsMessageContext){
398398
filename = list[i].getName();
399399
if(filename.startsWith(infoParts[2])){
400400
String[] fileList = {filePath + filename};
401-
new RemoteFileUpload().execute(fileList);
401+
new UploadFileToServer().execute(fileList);
402402
}
403403
}
404404
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.googleresearch.capturesync;
2+
3+
import java.io.File;
4+
5+
6+
import org.apache.hc.client5.http.classic.methods.HttpPost;
7+
import org.apache.hc.client5.http.entity.mime.FileBody;
8+
import org.apache.hc.client5.http.entity.mime.HttpMultipartMode;
9+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
10+
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
11+
import org.apache.hc.client5.http.impl.classic.HttpClients;
12+
import org.apache.hc.core5.http.HttpEntity;
13+
import org.apache.hc.core5.http.HttpResponse;
14+
import java.io.IOException;
15+
import android.os.AsyncTask;
16+
17+
18+
19+
public class UploadFileToServer extends AsyncTask<String, Integer, String> {
20+
@Override
21+
protected void onPreExecute() {
22+
// setting progress bar to zero
23+
super.onPreExecute();
24+
}
25+
26+
@Override
27+
protected void onProgressUpdate(Integer... progress) {
28+
29+
}
30+
31+
@Override
32+
protected String doInBackground(String... selectedFilePaths) {
33+
return uploadFile(selectedFilePaths);
34+
}
35+
36+
@SuppressWarnings("deprecation")
37+
private String uploadFile(String... selectedFilePaths) {
38+
String responseString = "All Good";
39+
HttpResponse httpResponse;
40+
int statusCode;
41+
String response = "";
42+
43+
44+
try (final CloseableHttpClient httpClient = HttpClients.createDefault()) {
45+
final HttpPost httpPost = new HttpPost("http://192.168.5.1:5000/upload");
46+
47+
final File video_file = new File(selectedFilePaths[0]);
48+
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
49+
builder.setMode(HttpMultipartMode.LEGACY);
50+
builder.addPart("file", new FileBody(video_file));
51+
HttpEntity entity = builder.build();
52+
httpPost.setEntity(entity);
53+
httpResponse = httpClient.execute(httpPost);
54+
statusCode = httpResponse.getCode();
55+
System.out.println("Response Status:" + statusCode);
56+
System.out.println("FILE UPLOADED");
57+
58+
} catch (IOException e) {
59+
responseString = e.toString();
60+
}
61+
return responseString;
62+
63+
}
64+
65+
@Override
66+
protected void onPostExecute(String result) {
67+
68+
super.onPostExecute(result);
69+
}
70+
71+
}
72+
73+
74+
75+

fileserver/file_server.py

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
app.secret_key = "recSync-fileserver"
1616

17-
UPLOAD_FOLDER = 'static/uploads'
17+
UPLOAD_FOLDER = 'static/uploads/'
1818
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
1919

2020
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif', 'mov', 'mp4'])
@@ -28,26 +28,38 @@ def main():
2828

2929
@app.route('/upload', methods=['POST'])
3030
def upload_file():
31-
# check if the post request has the file part
32-
data = request.get_data()
33-
import pdb;pdb.set_trace()
34-
dd = b''.join(data.split(b'\r')[3:6])[2:]
35-
#dd = data.split(b'\r')[5]
36-
with open('binary.mp4', 'wb') as wfile:
37-
wfile.write(dd.decode())
38-
39-
if 'name' not in request.files:
40-
print("request inside files not found")
41-
resp = jsonify({'message' : 'No file part in the request'})
42-
print(resp.data)
43-
resp.status_code = 400
44-
return resp
45-
46-
errors = {}
47-
success = False
48-
resp = jsonify({"message" : "git files"})
49-
resp.status_code = 200
50-
return resp
31+
#import pdb; pdb.set_trace()
32+
if 'file' not in request.files:
33+
resp = jsonify({'message' : 'No file part in the request'})
34+
resp.status_code = 400
35+
return resp
36+
37+
files = request.files.getlist('file')
38+
39+
errors = {}
40+
success = False
41+
42+
for file in files:
43+
if file and allowed_file(file.filename):
44+
filename = secure_filename(file.filename)
45+
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
46+
success = True
47+
else:
48+
errors[file.filename] = 'File type is not allowed'
49+
50+
if success and errors:
51+
errors['message'] = 'File(s) successfully uploaded'
52+
resp = jsonify(errors)
53+
resp.status_code = 500
54+
return resp
55+
if success:
56+
resp = jsonify({'message' : 'Files successfully uploaded'})
57+
resp.status_code = 201
58+
return resp
59+
else:
60+
resp = jsonify(errors)
61+
resp.status_code = 500
62+
return resp
5163

5264
if __name__ == '__main__':
5365
app.run(host='0.0.0.0', port=5000,debug=True)

remote_control/remote_control.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ def statusBtn(self):
3434
self.label.setText(message)
3535
def downloadBtn(self):
3636
endpoint = self.api_input.toPlainText()
37-
self.ws.send("UPLOAD@@"+endpoint+"@@Test_UPLOAD")
37+
download_prefix = self.download_prefix_text.toPlainText()
38+
self.ws.send("UPLOAD@@"+endpoint+"@@"+download_prefix)
3839

3940
def setupUi(self, MainWindow):
4041
self.ws = websocket.WebSocket()
4142
#self.ws.connect("ws://172.16.62.107:7867/remotecon")
4243
self.ws.connect("ws://192.168.5.2:7867/remotecon")
4344
MainWindow.setObjectName("MainWindow")
44-
MainWindow.resize(800, 600)
45+
MainWindow.resize(800, 800)
4546
self.centralwidget = QtWidgets.QWidget(MainWindow)
4647
self.centralwidget.setObjectName("centralwidget")
4748
self.label = QtWidgets.QLabel(self.centralwidget)
@@ -69,8 +70,11 @@ def setupUi(self, MainWindow):
6970
self.api_input = QtWidgets.QTextEdit(self.centralwidget)
7071
self.api_input.setGeometry(QtCore.QRect(143, 390, 451, 31))
7172
self.api_input.setObjectName("textEdit")
73+
self.download_prefix_text = QtWidgets.QTextEdit(self.centralwidget)
74+
self.download_prefix_text.setGeometry(QtCore.QRect(280, 430, 161, 31))
75+
self.download_prefix_text.setObjectName("prefix_text")
7276
self.download_btn = QtWidgets.QPushButton(self.centralwidget)
73-
self.download_btn.setGeometry(QtCore.QRect(280, 450, 161, 61))
77+
self.download_btn.setGeometry(QtCore.QRect(280, 500, 161, 61))
7478
self.download_btn.setFont(font)
7579
self.download_btn.setObjectName("pushButton_4")
7680
self.download_btn.clicked.connect(self.downloadBtn)
@@ -97,6 +101,7 @@ def retranslateUi(self, MainWindow):
97101
self.stop_btn.setText(_translate("MainWindow", "Stop"))
98102
self.status_btn.setText(_translate("MainWindow", "Status"))
99103
self.api_input.setPlaceholderText(_translate("MainWindow", "Please enter the api endpoint where you want the files to be uploaded "))
104+
self.download_prefix_text.setPlaceholderText(_translate("MainWindow", "Session PREFIX for download "))
100105
self.download_btn.setText(_translate("MainWindow", "Download"))
101106
self.status_label.setPlaceholderText(_translate("MainWindow", "No status "))
102107

0 commit comments

Comments
 (0)