Browse code

cache totoz metadata

devnewton authored on 06/03/2018 15:30:46
Showing 1 changed files
... ...
@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value;
13 13
 import org.springframework.core.io.FileSystemResource;
14 14
 import org.springframework.http.MediaType;
15 15
 import org.springframework.http.ResponseEntity;
16
+import org.springframework.scheduling.annotation.Async;
16 17
 import org.springframework.stereotype.Controller;
17 18
 import org.springframework.web.bind.annotation.PathVariable;
18 19
 import org.springframework.web.bind.annotation.RequestMapping;
... ...
@@ -23,42 +24,47 @@ import org.springframework.web.util.UriComponentsBuilder;
23 24
 @RequestMapping("/totoz")
24 25
 public class TotozController {
25 26
 
26
-	private File totozDir;
27
+    private File totozDir;
27 28
 
28
-	@Value("${jb3.totoz.dir:}")
29
-	public void setPasteDir(String totozDir) {
30
-		if (StringUtils.isEmpty(totozDir)) {
31
-			String cacheDir = System.getenv("XDG_CACHE_HOME");
32
-			if (StringUtils.isEmpty(cacheDir)) {
33
-				cacheDir = new File(System.getProperty("user.home"), ".cache").getAbsolutePath();
34
-			}
35
-			this.totozDir = new File(new File(cacheDir, "jb3"), "totoz");
36
-			this.totozDir.mkdirs();
37
-		} else {
38
-			this.totozDir = new File(totozDir);
39
-		}
40
-	}
29
+    @Value("${jb3.totoz.dir:}")
30
+    public void setTotozDir(String totozDir) {
31
+        if (StringUtils.isEmpty(totozDir)) {
32
+            String cacheDir = System.getenv("XDG_CACHE_HOME");
33
+            if (StringUtils.isEmpty(cacheDir)) {
34
+                cacheDir = new File(System.getProperty("user.home"), ".cache").getAbsolutePath();
35
+            }
36
+            this.totozDir = new File(new File(cacheDir, "jb3"), "totoz");
37
+            this.totozDir.mkdirs();
38
+        } else {
39
+            this.totozDir = new File(totozDir);
40
+        }
41
+    }
41 42
 
42
-	@RequestMapping("/img/{totoz}")
43
-	@ResponseBody
44
-	public ResponseEntity<FileSystemResource> img(@PathVariable("totoz") String totoz)
45
-			throws MalformedURLException, IOException {
46
-		File totozFile = new File(totozDir, totoz);
47
-		if (!totozFile.exists()) {
48
-			URL totozUrl = UriComponentsBuilder.fromHttpUrl("https://nsfw.totoz.eu/img/").path(totoz).build().toUri().toURL();
49
-			FileUtils.copyURLToFile(totozUrl, totozFile);
50
-		}
51
-		return ResponseEntity.ok().lastModified(totozFile.lastModified()).contentType(detectContentType(totozFile))
52
-				.contentLength(totozFile.length()).body(new FileSystemResource(totozFile));
53
-	}
43
+    @RequestMapping("/img/{totoz}")
44
+    @ResponseBody
45
+    public ResponseEntity<FileSystemResource> img(@PathVariable("totoz") String totoz)
46
+            throws MalformedURLException, IOException {
47
+        File totozFile = new File(totozDir, totoz);
48
+        if (!totozFile.exists()) {
49
+            URL totozUrl = UriComponentsBuilder.fromHttpUrl("https://nsfw.totoz.eu/img/").path(totoz).build().toUri().toURL();
50
+            FileUtils.copyURLToFile(totozUrl, totozFile);
51
+        }
52
+        File totozMetadataFile = new File(totozDir, totoz + ".json");
53
+        if (!totozMetadataFile.exists()) {
54
+            URL totozMetadataUrl = UriComponentsBuilder.fromHttpUrl("https://nsfw.totoz.eu/img/").path(totoz).path("info.json").build().toUri().toURL();
55
+            FileUtils.copyURLToFile(totozMetadataUrl, totozMetadataFile);
56
+        }
57
+        return ResponseEntity.ok().lastModified(totozFile.lastModified()).contentType(detectContentType(totozFile))
58
+                .contentLength(totozFile.length()).body(new FileSystemResource(totozFile));
59
+    }
54 60
 
55
-	private MediaType detectContentType(File totozFile) {
56
-		try {
57
-			return MediaType.parseMediaType(Files.probeContentType(totozFile.toPath()));
58
-		} catch (Exception e) {
59
-			return MediaType.ALL;
60
-		}
61
+    private MediaType detectContentType(File totozFile) {
62
+        try {
63
+            return MediaType.parseMediaType(Files.probeContentType(totozFile.toPath()));
64
+        } catch (Exception e) {
65
+            return MediaType.ALL;
66
+        }
61 67
 
62
-	}
68
+    }
63 69
 
64 70
 }