-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathLetterSoundAssessmentEventsRestController.java
More file actions
91 lines (77 loc) · 4.29 KB
/
LetterSoundAssessmentEventsRestController.java
File metadata and controls
91 lines (77 loc) · 4.29 KB
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
package ai.elimu.rest.v2.analytics;
import ai.elimu.model.v2.enums.Language;
import ai.elimu.util.AnalyticsHelper;
import ai.elimu.util.ConfigHelper;
import java.io.File;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
/**
* REST API endpoint for receiving letter-sound assessment events from the
* <a href="https://github.com/elimu-ai/analytics">Analytics</a> application.
*/
@RestController
@RequestMapping(value = "/rest/v2/analytics/letter-sound-assessment-events/csv", produces = MediaType.APPLICATION_JSON_VALUE)
@Slf4j
public class LetterSoundAssessmentEventsRestController {
@PostMapping
public String handleUploadCsvRequest(
@RequestParam("file") MultipartFile multipartFile,
HttpServletResponse response
) {
log.info("handleUploadCsvRequest");
JSONObject jsonResponseObject = new JSONObject();
try {
String contentType = multipartFile.getContentType();
log.info("contentType: " + contentType);
long size = multipartFile.getSize();
log.info("size: " + size);
if (size == 0) {
throw new IllegalArgumentException("Empty file");
}
// Expected format: "7161a85a0e4751cd_3002023_letter-sound-assessment-events_2025-05-28.csv"
String originalFilename = multipartFile.getOriginalFilename();
log.info("originalFilename: " + originalFilename);
if (originalFilename.length() != "7161a85a0e4751cd_3002023_letter-sound-assessment-events_2025-05-28.csv".length()) {
throw new IllegalArgumentException("Unexpected filename");
}
String androidIdExtractedFromFilename = AnalyticsHelper.extractAndroidIdFromCsvFilename(originalFilename);
log.info("androidIdExtractedFromFilename: \"" + androidIdExtractedFromFilename + "\"");
Integer versionCodeExtractedFromFilename = AnalyticsHelper.extractVersionCodeFromCsvFilename(originalFilename);
log.info("versionCodeExtractedFromFilename: " + versionCodeExtractedFromFilename);
byte[] bytes = multipartFile.getBytes();
log.info("bytes.length: " + bytes.length);
// Store the original CSV file on the filesystem
File elimuAiDir = new File(System.getProperty("user.home"), ".elimu-ai");
File languageDir = new File(elimuAiDir, "lang-" + Language.valueOf(ConfigHelper.getProperty("content.language")));
File analyticsDir = new File(languageDir, "analytics");
File androidIdDir = new File(analyticsDir, "android-id-" + androidIdExtractedFromFilename);
File versionCodeDir = new File(androidIdDir, "version-code-" + versionCodeExtractedFromFilename);
File letterSoundAssessmentEventsDir = new File(versionCodeDir, "letter-sound-assessment-events");
letterSoundAssessmentEventsDir.mkdirs();
File csvFile = new File(letterSoundAssessmentEventsDir, originalFilename);
log.info("Storing CSV file at " + csvFile);
FileUtils.writeByteArrayToFile(csvFile, bytes);
log.info("csvFile.exists(): " + csvFile.exists());
jsonResponseObject.put("result", "success");
jsonResponseObject.put("successMessage", "The CSV file was uploaded");
response.setStatus(HttpStatus.OK.value());
} catch (Exception ex) {
log.error(ex.getMessage());
jsonResponseObject.put("result", "error");
jsonResponseObject.put("errorMessage", ex.getMessage());
response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
}
String jsonResponse = jsonResponseObject.toString();
log.info("jsonResponse: " + jsonResponse);
return jsonResponse;
}
}