Browse code

fix cache totoz metadata

devnewton authored on 06/03/2018 21:22:13
Showing 2 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,61 @@
1
+package im.bci.jb3.totoz;
2
+
3
+import java.io.File;
4
+import java.io.IOException;
5
+import java.net.URL;
6
+import org.apache.commons.io.FileUtils;
7
+import org.apache.commons.lang3.StringUtils;
8
+import org.apache.commons.logging.Log;
9
+import org.apache.commons.logging.LogFactory;
10
+import org.springframework.beans.factory.annotation.Value;
11
+import org.springframework.scheduling.annotation.Async;
12
+import org.springframework.stereotype.Component;
13
+import org.springframework.web.util.UriComponentsBuilder;
14
+
15
+/**
16
+ *
17
+ * @author devnewton
18
+ */
19
+@Component
20
+public class TotozCache {
21
+
22
+    private final Log LOGGER = LogFactory.getLog(this.getClass());
23
+    private File totozDir;
24
+
25
+    @Value("${jb3.totoz.dir:}")
26
+    public void setTotozDir(String totozDir) {
27
+        if (StringUtils.isEmpty(totozDir)) {
28
+            String cacheDir = System.getenv("XDG_CACHE_HOME");
29
+            if (StringUtils.isEmpty(cacheDir)) {
30
+                cacheDir = new File(System.getProperty("user.home"), ".cache").getAbsolutePath();
31
+            }
32
+            this.totozDir = new File(new File(cacheDir, "jb3"), "totoz");
33
+            this.totozDir.mkdirs();
34
+        } else {
35
+            this.totozDir = new File(totozDir);
36
+        }
37
+    }
38
+
39
+    @Async("mouleExecutor")
40
+    public void saveTotozMetaData(String totoz) {
41
+        try {
42
+            File totozMetadataFile = new File(totozDir, totoz + ".json");
43
+            if (!totozMetadataFile.exists()) {
44
+                URL totozMetadataUrl = UriComponentsBuilder.fromHttpUrl("https://nsfw.totoz.eu/totoz/").path(totoz).path("/info.json").build().toUri().toURL();
45
+                FileUtils.copyURLToFile(totozMetadataUrl, totozMetadataFile);
46
+            }
47
+        } catch (Exception e) {
48
+            LOGGER.warn("Cannot save totoz metadata for " + totoz, e);
49
+        }
50
+    }
51
+
52
+    public File cacheTotoz(String totoz) throws IOException {
53
+        File totozFile = new File(totozDir, totoz);
54
+        if (!totozFile.exists()) {
55
+            URL totozUrl = UriComponentsBuilder.fromHttpUrl("https://nsfw.totoz.eu/img/").path(totoz).build().toUri().toURL();
56
+            FileUtils.copyURLToFile(totozUrl, totozFile);
57
+        }
58
+        return totozFile;
59
+    }
60
+
61
+}
... ...
@@ -8,11 +8,15 @@ import java.nio.file.Files;
8 8
 
9 9
 import org.apache.commons.io.FileUtils;
10 10
 import org.apache.commons.lang3.StringUtils;
11
+import org.apache.commons.logging.Log;
12
+import org.apache.commons.logging.LogFactory;
13
+import org.springframework.beans.factory.annotation.Autowired;
11 14
 
12 15
 import org.springframework.beans.factory.annotation.Value;
13 16
 import org.springframework.core.io.FileSystemResource;
14 17
 import org.springframework.http.MediaType;
15 18
 import org.springframework.http.ResponseEntity;
19
+import org.springframework.scheduling.annotation.Async;
16 20
 import org.springframework.stereotype.Controller;
17 21
 import org.springframework.web.bind.annotation.PathVariable;
18 22
 import org.springframework.web.bind.annotation.RequestMapping;
... ...
@@ -21,38 +25,17 @@ import org.springframework.web.util.UriComponentsBuilder;
21 25
 
22 26
 @Controller
23 27
 @RequestMapping("/totoz")
24
-public class TotozController {
25
-
26
-    private File totozDir;
27
-
28
-    @Value("${jb3.totoz.dir:}")
29
-    public void setTotozDir(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
-    }
28
+public class TotozController  {
41 29
 
30
+    @Autowired
31
+    private TotozCache cache;
32
+    
42 33
     @RequestMapping("/img/{totoz}")
43 34
     @ResponseBody
44 35
     public ResponseEntity<FileSystemResource> img(@PathVariable("totoz") String totoz)
45 36
             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
-        File totozMetadataFile = new File(totozDir, totoz + ".json");
52
-        if (!totozMetadataFile.exists()) {
53
-            URL totozMetadataUrl = UriComponentsBuilder.fromHttpUrl("https://nsfw.totoz.eu/img/").path(totoz).path("info.json").build().toUri().toURL();
54
-            FileUtils.copyURLToFile(totozMetadataUrl, totozMetadataFile);
55
-        }
37
+        File totozFile = cache.cacheTotoz(totoz);
38
+        cache.saveTotozMetaData(totoz);
56 39
         return ResponseEntity.ok().lastModified(totozFile.lastModified()).contentType(detectContentType(totozFile))
57 40
                 .contentLength(totozFile.length()).body(new FileSystemResource(totozFile));
58 41
     }