Skip to content

Commit

Permalink
hardcode download path, add test, dummy file
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 23, 2023
1 parent 614fae3 commit 0407572
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
@SpringBootApplication
public class WarehouseApplication {

public static void main(String[] args) {
public static void main(String[] args) {

SpringApplication.run(WarehouseApplication.class, args);
}
SpringApplication.run(WarehouseApplication.class, args);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class DownloadController {
DownloadStatusMapper downloadStatusMapper;

String userDirectory = new File("").getAbsolutePath();
final String prefix = userDirectory + "/src/main/resources/report/";
//final String prefix = userDirectory + "/src/main/resources/report/";
final String prefix = userDirectory + "/src/main/resources/";

@GetMapping("/list")
public String list(Map<String, Object> map, @RequestParam(value="pageNo", required=false, defaultValue="1") String pageNoStr) {
Expand Down Expand Up @@ -77,6 +78,7 @@ public String createDownload() {
map.put("age", 17);
// save file to local // TODO : save it to remote file system (e.g. S3)
Boolean result = fileUtil.writeJsonFile(map, prefix + fileName);
//Boolean result = fileUtil.writeJsonFile(map, fileName);
if (result) {
DownloadStatus downloadStatus = new DownloadStatus();
downloadStatus.setStatus("completed");
Expand All @@ -97,10 +99,16 @@ public ResponseEntity<Resource> downloadFile(String url) throws IOException {

log.info(">>> (ResponseEntity<Resource> downloadFile) url = " + url);
//List<DownloadStatus> downloadStatusList = downloadStatusMapper.getAllDownloadStatus();
//String downloadUrl = "/report/" + downloadStatusList.get(0).getDownloadUrl();
String downloadUrl = "/report/" + url;
log.info("downloadUrl = " + downloadUrl);
Resource resource = new ClassPathResource(downloadUrl);
String downloadUrl = url;
log.info(">>> downloadUrl = " + downloadUrl);

// TODO : fix why can't read file from downloadUrl
//ClassPathResource resource = new ClassPathResource(String.valueOf(downloadUrl));
ClassPathResource resource = new ClassPathResource("demo_report.json");
log.info(resource.getPath());
log.info(String.valueOf(resource.exists()));
log.info(String.valueOf(resource.getURL()));

HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=%s".format("test.json"));

Expand Down
1 change: 1 addition & 0 deletions springWarehouse/src/main/resources/demo_report.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"king","age":17}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.yen.springWarehouse.util.DateTimeUtils;
import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.text.SimpleDateFormat;

import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -21,4 +24,21 @@ public void testPrepareDownloadFile(){
System.out.println(dateTimeUtils.getCurrentDateYYYYMMDDHHMMSS());
}

@Test
public void testGetResourcePath() throws IOException {

ClassPathResource r1 = new ClassPathResource("application.properties");
System.out.println(r1.getPath());
System.out.println(r1.exists());
System.out.println(r1.getURL());

//ClassPathResource r2 = new ClassPathResource("/report/20231123-16-16-09_report.json");
ClassPathResource r2 = new ClassPathResource("20231123-16-52-30_report.json");
System.out.println(r2.getPath());
System.out.println(r2.exists());
System.out.println(r2.getURL());


}

}

0 comments on commit 0407572

Please sign in to comment.