You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: backend/README.md
+91-54Lines changed: 91 additions & 54 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# File Sync Backend
2
2
3
-
This project provides backend APIs for transferring files using a custom TCP-over-UDP protocol. It includes endpoints for sending and receiving files, allowing you to specify the file storage location dynamically in each request.
3
+
This project provides backend APIs for transferring files using a custom TCP-over-UDP protocol and S3 for file storage. It includes endpoints for sending, receiving, uploading, and downloading files.
4
4
5
5
## Related Repository
6
6
@@ -16,15 +16,86 @@ Example:
16
16
http://localhost:8080/file-transfer
17
17
```
18
18
19
+
## Environment Configuration
20
+
21
+
To use Amazon S3 for file storage, set up a `.env` file in the root of your project directory with the following environment variables:
22
+
23
+
```plaintext
24
+
# .env
25
+
AWS_ACCESS_KEY_ID=your-access-key-id
26
+
AWS_SECRET_ACCESS_KEY=your-secret-access-key
27
+
AWS_REGION=your-aws-region
28
+
```
29
+
30
+
These environment variables are necessary for the application to authenticate with AWS and interact with your S3 bucket.
31
+
32
+
> **Note:** Ensure that the `.env` file is included in `.gitignore` to keep your credentials secure.
33
+
19
34
## Endpoints
20
35
21
-
### 1. Upload and Send File
36
+
### 1. Upload File to S3
22
37
23
-
Upload a file and send it to a specified receiver.
38
+
Uploads a file to an Amazon S3 bucket for storage.
39
+
40
+
-**URL**: `/file-transfer/upload`
41
+
-**Method**: `POST`
42
+
-**Description**: Uploads a file to a specified S3 bucket.
43
+
44
+
#### Request Parameters
45
+
46
+
-**file**: (multipart/form-data) The file to be uploaded.
47
+
-**bucketName**: (string) The name of the S3 bucket where the file will be stored.
48
+
-**fileName**: (string) The name to save the file as in S3.
49
+
50
+
#### Example `curl` Request
51
+
52
+
```bash
53
+
curl -X POST -F "file=@/path/to/your/file.txt" \
54
+
-F "bucketName=your-s3-bucket-name" \
55
+
-F "fileName=uploaded-file.txt" \
56
+
http://localhost:8080/file-transfer/upload
57
+
```
58
+
59
+
#### Response
60
+
61
+
-**Success**: Returns a message with the S3 ETag confirming the file was uploaded successfully.
62
+
-**Error**: Returns a `500 Internal Server Error` if the upload fails.
63
+
64
+
---
65
+
66
+
### 2. Download File from S3
67
+
68
+
Downloads a file from S3 and serves it as a downloadable resource.
69
+
70
+
-**URL**: `/file-transfer/download`
71
+
-**Method**: `GET`
72
+
-**Description**: Fetches a file from S3 based on the provided bucket and file name.
73
+
74
+
#### Request Parameters
75
+
76
+
-**bucketName**: (query parameter) The name of the S3 bucket where the file is stored.
77
+
-**fileName**: (query parameter) The name of the file in S3.
78
+
79
+
#### Example `curl` Request
80
+
81
+
```bash
82
+
curl -X GET "http://localhost:8080/file-transfer/download?bucketName=your-s3-bucket-name&fileName=your-file-name" -o downloaded-file.txt
83
+
```
84
+
85
+
#### Response
86
+
87
+
-**Success**: Returns the file as a downloadable resource.
88
+
-**Error**: Returns a `404 Not Found` if the file does not exist or a `500 Internal Server Error` if the download fails.
89
+
90
+
---
91
+
92
+
### 3. Send File via TCP-over-UDP
93
+
94
+
Uploads and sends a file to a specified receiver over a custom TCP-over-UDP protocol.
24
95
25
96
-**URL**: `/file-transfer/send`
26
97
-**Method**: `POST`
27
-
-**Description**: Uploads a file to the backend and sends it to the specified receiver over a custom TCP-over-UDP protocol.
98
+
-**Description**: Uploads a file and sends it to a designated receiver.
28
99
29
100
#### Request Parameters
30
101
@@ -50,13 +121,13 @@ curl -X POST -F "file=@/path/to/your/file.txt" \
50
121
51
122
---
52
123
53
-
### 2. Receive and Download File
124
+
### 4. Receive File via TCP-over-UDP
54
125
55
-
Trigger the receiver to download a file over TCP-over-UDP, then serve it as a downloadable resource.
126
+
Initiates the receiver process to download a file over TCP-over-UDP, then serves it as a downloadable resource.
56
127
57
128
-**URL**: `/file-transfer/receive`
58
129
-**Method**: `GET`
59
-
-**Description**: Initiates the file reception via the custom TCP-over-UDP protocol and provides the file for download.
130
+
-**Description**: Starts the receiver to download the file over the TCP-over-UDP protocol.
60
131
61
132
#### Request Parameters
62
133
@@ -95,95 +166,61 @@ To run the backend locally, follow these steps:
The backend requires the `sender` and `receiver` binaries from the [tcp-over-udp repository](https://github.com/eomielan/tcp-over-udp). Compile these binaries by running `make` in the `src/tcpOverUdp` directory:
108
-
109
176
```bash
110
177
cd tcpOverUdp
111
178
make
112
179
```
113
180
114
-
- This will generate the `sender` and `receiver` executables required for file transfer.
115
-
- Ensure both binaries are executable (if not, use `chmod +x sender receiver`).
116
-
117
-
3.**Configure Application Properties**
181
+
3.**Configure the .env File**
118
182
119
-
Open `src/main/resources/application.properties` to configure any necessary properties, such as server port (optional):
183
+
Create a `.env` file in the project root with your AWS credentials and region:
120
184
121
-
```properties
122
-
# Default server port (optional)
123
-
server.port=8080
185
+
```plaintext
186
+
AWS_ACCESS_KEY_ID=your-access-key-id
187
+
AWS_SECRET_ACCESS_KEY=your-secret-access-key
188
+
AWS_REGION=your-aws-region
124
189
```
125
190
126
191
4.**Run the Backend**
127
192
128
-
Start the Spring Boot backend using the Gradle Wrapper:
129
-
130
193
```bash
131
-
cd ..# Navigate back to the project root
194
+
cd ..
132
195
./gradlew bootRun
133
196
```
134
197
135
-
Alternatively, if you have Gradle installed, you can also use:
136
-
137
-
```bash
138
-
gradle bootRun
139
-
```
140
-
141
-
This command will start the backend server on `http://localhost:8080` (or on the port specified in `application.properties`).
142
-
143
198
5.**Test the Endpoints**
144
199
145
-
You can now test the `/file-transfer/send`and `/file-transfer/receive` endpoints using `curl` or Postman. Ensure that the receiver is set up and listening on the specified port when testing the `send` endpoint.
200
+
You can now test the `/file-transfer/send`, `/file-transfer/receive`, `/file-transfer/upload`, and `/file-transfer/download` endpoints using `curl` or Postman.
146
201
147
202
---
148
203
149
204
## How to Run Tests
150
205
151
-
The backend includes unit and integration tests for verifying file transfer functionality, error handling, and endpoint responses. These tests are implemented using **JUnit 5** and **Mockito**.
152
-
153
-
### Running Tests with Gradle
154
-
155
206
1.**Navigate to the Backend Directory**
156
207
157
-
Make sure you’re in the `backend` directory:
158
-
159
208
```bash
160
209
cd file-sync/backend
161
210
```
162
211
163
212
2.**Run All Tests**
164
213
165
-
To run all tests, use the following Gradle command:
166
-
167
214
```bash
168
215
./gradlew test
169
216
```
170
217
171
-
Alternatively, if Gradle is installed, you can use:
172
-
173
-
```bash
174
-
gradle test
175
-
```
176
-
177
218
3.**View Test Results**
178
219
179
-
Test results will be available in the `build/reports/tests/test/index.html` file. Open this file in a browser to view a detailed test report.
180
-
181
-
### Running Tests for a Specific Class
182
-
183
-
If you want to run tests for a specific class, use the following command:
220
+
Test results are available in `build/reports/tests/test/index.html`.
184
221
185
-
```bash
186
-
./gradlew test --tests "com.networking.filetransfer.controller.FileControllerTest"
187
-
```
222
+
4.**Run Specific Tests**
188
223
189
-
Replace `FileControllerTest` with the specific test class name.
224
+
```bash
225
+
./gradlew test --tests "com.networking.filetransfer.controller.FileControllerTest"
0 commit comments