Skip to content

Commit

Permalink
Frontend for exe uploads (PR #345)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalinovsky Konstantin authored and Brutus5000 committed Sep 26, 2019
1 parent 545a0b8 commit 4d87093
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8_VALUE;
import static org.springframework.http.MediaType.TEXT_HTML_VALUE;

@RestController
@RequestMapping(path = "/exe")
Expand All @@ -32,6 +36,11 @@ public ExeUploaderController(
this.exeUploaderService = exeUploaderService;
}

@RequestMapping(path = "/upload", method = RequestMethod.GET, produces = TEXT_HTML_VALUE)
public ModelAndView showValidationForm(Map<String, Object> model) {
return new ModelAndView("upload_exe_form.html");
}

@ApiOperation("Upload an exe file and override the existing one for the current version")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Success"),
Expand Down
73 changes: 73 additions & 0 deletions src/main/resources/templates/upload_exe_form.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Upload exe for Beta or Develop</title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js" type="text/javascript"></script>
<link href="https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz" rel="stylesheet" type="text/css"/>
<link href="https://faforever.com/styles/css/site.min.css" rel="stylesheet" type="text/css"/>
<link rel="stylesheet" th:href="@{/css/style.css}" type="text/css"/>
</head>
<body>
<div class="background hero"></div>
<div class="card">
<h1>Upload exe for Beta or Develop</h1>
<div id="info-block"></div>
<form id="upload-form" class="form-group" action="#">
<input th:if="${_csrf != null }" th:name="${_csrf.parameterName}" th:value="${_csrf.token}" type="hidden"/>
<div>
<div>
<input class="form-control" id="file" name="file" type="file"/>
</div>
<select class="form-control" id="modName" name="modName">
<option value="fafdevelop">Develop</option>
<option value="fafbeta">Beta</option>
</select>
<div>
<input class="form-control" id="apiKey" name="apiKey" placeholder="Api Key" size="80" type="text"/>
</div>
</div>
<div>
<input class="btn btn-danger" id="upload" name="upload" type="submit" value="Upload exe" />
</div>
</form>
</div>

<script type="text/javascript">
$(document).ready(function () {
$("#upload-form").submit(function (e) {
e.preventDefault();
var data = new FormData();
var files = $('#file')[0].files;
if (files.length == 1) {
data.append("file", files[0], files[0].name);
}
data.append("modName", $("#modName").val());
data.append("apiKey", $("#apiKey").val());
$.ajax({
url: '/exe/upload',
type: 'POST',
data: data,
contentType: 'multipart/form-data',
cache: false,
contentType: false,
processData: false,
}).done(
function (msg) {
$("#info-block").empty();
$("#info-block").append('<div class="alert alert-success"> Ok </div>');
$("#upload-form")[0].reset();
}
).fail(
function (xhr, status, error) {
xhr.responseJSON.errors.forEach(function (nextError) {
$("#info-block").empty();
$("#info-block").append('<div class="alert alert-danger">' + nextError.detail + '</div>');
});
}
);
});
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.fileUpload;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
Expand Down Expand Up @@ -97,4 +98,9 @@ public void testBadRequestUploadWithoutApiKey() throws Exception {
.param("modName", "fafbeta")
).andExpect(status().isBadRequest());
}

@Test
public void testWeGetHtmlForUpload() throws Exception {
this.mvc.perform(get("/exe/upload")).andExpect(status().is2xxSuccessful());
}
}

0 comments on commit 4d87093

Please sign in to comment.