OSDN Git Service

Merge branch テーブルのtooltipにサムネイル情報を表示する機能
[coroid/inqubus.git] / frontend / src / yukihane / inqubus / thumbnail / Repository.java
1 package yukihane.inqubus.thumbnail;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.net.URL;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.concurrent.ExecutorService;
11 import java.util.concurrent.Executors;
12 import javax.imageio.ImageIO;
13 import nicobrowser.NicoHttpClient;
14 import nicobrowser.util.RssContent;
15 import nicobrowser.util.RssContentParser;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import saccubus.worker.profile.ProxyProfile;
19
20 /**
21  *
22  * @author yuki
23  */
24 public class Repository {
25
26     private static final Logger logger = LoggerFactory.getLogger(Repository.class);
27     private final ExecutorService executorService = Executors.newCachedThreadPool();
28     private final Map<String, Thumbnail> thumbnailMap = new HashMap<>();
29
30     public void request(ProxyProfile proxy, File tempDir, String videoId) {
31         synchronized (thumbnailMap) {
32             final Thumbnail value = thumbnailMap.get(videoId);
33             if (value != null) {
34                 return;
35             }
36         }
37
38         executorService.execute(new ThumnailFetchWorker(proxy, tempDir, videoId));
39     }
40
41     public Thumbnail getThumnail(String videoId) {
42         synchronized (thumbnailMap) {
43             return thumbnailMap.get(videoId);
44         }
45     }
46
47     private class ThumnailFetchWorker implements Runnable {
48
49         private final ProxyProfile proxy;
50         private final String videoId;
51         private final File tempDir;
52
53         private ThumnailFetchWorker(ProxyProfile proxy, File tempDir, String videoId) {
54             this.proxy = proxy;
55             this.videoId = videoId;
56             this.tempDir = tempDir;
57         }
58
59         @Override
60         public void run() {
61             work();
62         }
63
64         private void work() {
65             final NicoHttpClient client = createClient(proxy);
66
67             try (InputStream is = client.getThumbInfo(videoId)) {
68                 final RssContent cont = new RssContentParser().parse(is);
69                 final URL thumbUrl = new URL(cont.getThumbnail_url());
70
71                 final File imageFile;
72                 try (InputStream imageStream = client.get(thumbUrl)) {
73                     final BufferedImage image = ImageIO.read(imageStream);
74                     imageFile = File.createTempFile("thumb", ".jpg", tempDir);
75                     imageFile.deleteOnExit();
76                     ImageIO.write(image, "jpeg", imageFile);
77                 }
78                 final Thumbnail thumb = new Thumbnail(cont.getTitle(), cont.getLength(), imageFile);
79
80                 synchronized (thumbnailMap) {
81                     thumbnailMap.put(videoId, thumb);
82                 }
83             } catch (IOException ex) {
84                 logger.error(null, ex);
85             }
86         }
87
88         private NicoHttpClient createClient(ProxyProfile proxy) {
89             if (proxy.use()) {
90                 return new NicoHttpClient(proxy.getHost(), proxy.getPort());
91             } else {
92                 return new NicoHttpClient();
93             }
94         }
95     }
96 }