|
7 | 7 | import java.net.URLEncoder; |
8 | 8 | import java.util.ArrayList; |
9 | 9 | import java.util.Date; |
| 10 | +import java.util.Map; |
10 | 11 |
|
11 | 12 | import org.apache.log4j.Logger; |
12 | 13 |
|
| 14 | +import com.qiniu.cdn.CdnResult; |
| 15 | +import com.qiniu.cdn.CdnResult.LogData; |
13 | 16 | import com.qiniu.common.QiniuException; |
14 | 17 | import com.qiniu.http.Response; |
15 | 18 | import com.qiniu.storage.BucketManager; |
@@ -39,27 +42,83 @@ public enum FileAction { |
39 | 42 | } |
40 | 43 |
|
41 | 44 | /** |
42 | | - * 私有下载 |
| 45 | + * 日志下载,cdn相关 |
43 | 46 | */ |
| 47 | + public void downloadCdnLog(String logDate) { |
| 48 | + if (Checker.isNotEmpty(QiniuApplication.buckets) && Checker.isDate(logDate)) { |
| 49 | + String[] domains = new String[QiniuApplication.buckets.size()]; |
| 50 | + int i = 0; |
| 51 | + for (Map.Entry<String, String> bucket : QiniuApplication.buckets.entrySet()) { |
| 52 | + domains[i] = bucket.getValue().split(" ")[1]; |
| 53 | + i++; |
| 54 | + } |
| 55 | + try { |
| 56 | + CdnResult.LogListResult logRes = QiniuApplication.cdnManager.getCdnLogList(domains, logDate); |
| 57 | + Downloader downloader = new Downloader(); |
| 58 | + for (Map.Entry<String, LogData[]> logs : logRes.data.entrySet()) { |
| 59 | + for (LogData log : logs.getValue()) { |
| 60 | + downloader.downloadFromNet(log.url); |
| 61 | + } |
| 62 | + } |
| 63 | + } catch (QiniuException e) { |
| 64 | + logger.error("get cdn log url error, message: " + e.getMessage()); |
| 65 | + Dialogs.showException(e); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * 刷新文件,cdn相关 |
| 72 | + */ |
| 73 | + public void refreshFile(ObservableList<FileInfo> fileInfos, String domain) { |
| 74 | + if (Checker.isNotEmpty(fileInfos)) { |
| 75 | + String[] files = new String[fileInfos.size()]; |
| 76 | + int i = 0; |
| 77 | + for (FileInfo fileInfo : fileInfos) { |
| 78 | + files[i] = getPublicURL(fileInfo.getName(), domain); |
| 79 | + i++; |
| 80 | + } |
| 81 | + try { |
| 82 | + // 单次方法调用刷新的链接不可以超过100个 |
| 83 | + QiniuApplication.cdnManager.refreshUrls(files); |
| 84 | + logger.info("refresh files success"); |
| 85 | + } catch (QiniuException e) { |
| 86 | + logger.error("refresh files error, message: " + e.getMessage()); |
| 87 | + Dialogs.showException(e); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
44 | 92 | public void privateDownload(String fileName, String domain) { |
| 93 | + privateDownload(fileName, domain, new Downloader()); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * 私有下载 |
| 98 | + */ |
| 99 | + public void privateDownload(String fileName, String domain, Downloader downloader) { |
45 | 100 | try { |
46 | 101 | String encodedFileName = URLEncoder.encode(fileName, "utf-8"); |
47 | 102 | String publicUrl = String.format("%s/%s", domain, encodedFileName); |
48 | 103 | // 自定义链接过期时间(小时) |
49 | 104 | long expireInSeconds = 24; |
50 | | - new Downloader().downloadFromNet(QiniuApplication.auth.privateDownloadUrl(publicUrl, expireInSeconds)); |
| 105 | + downloader.downloadFromNet(QiniuApplication.auth.privateDownloadUrl(publicUrl, expireInSeconds)); |
51 | 106 | } catch (UnsupportedEncodingException e) { |
52 | 107 | urlError(e); |
53 | 108 | } |
54 | 109 | } |
55 | 110 |
|
| 111 | + public void publicDownload(String fileName, String domain) { |
| 112 | + publicDownload(fileName, domain, new Downloader()); |
| 113 | + } |
| 114 | + |
56 | 115 | /** |
57 | 116 | * 公有下载 |
58 | 117 | */ |
59 | | - public void publicDownload(String fileName, String domain) { |
| 118 | + public void publicDownload(String fileName, String domain, Downloader downloader) { |
60 | 119 | String url = getPublicURL(fileName, domain); |
61 | 120 | if (Checker.isNotEmpty(url)) { |
62 | | - new Downloader().downloadFromNet(url); |
| 121 | + downloader.downloadFromNet(url); |
63 | 122 | } |
64 | 123 | } |
65 | 124 |
|
|
0 commit comments