|
| 1 | +package io.ipfs.nbs; |
| 2 | + |
| 3 | +import com.nbs.entity.IPFSFileEntity; |
| 4 | +import com.sun.xml.internal.bind.v2.util.ByteArrayOutputStreamEx; |
| 5 | +import io.ipfs.api.IPFS; |
| 6 | +import io.ipfs.multihash.Multihash; |
| 7 | +import io.nbs.client.Launcher; |
| 8 | +import io.nbs.client.cnsts.AppGlobalCnst; |
| 9 | +import io.nbs.commons.helper.ConfigurationHelper; |
| 10 | +import org.apache.commons.lang3.StringUtils; |
| 11 | + |
| 12 | +import java.io.*; |
| 13 | +import java.util.concurrent.ArrayBlockingQueue; |
| 14 | +import java.util.concurrent.BlockingQueue; |
| 15 | +import java.util.concurrent.TimeUnit; |
| 16 | + |
| 17 | +/** |
| 18 | + * @Package : io.ipfs.nbs |
| 19 | + * @Description : <p></p> |
| 20 | + * @Author : lambor.c |
| 21 | + * @Date : 2018/8/2-15:10 |
| 22 | + * Copyright (c) 2018, NBS , lambor.c<[email protected]>. |
| 23 | + * All rights reserved. |
| 24 | + */ |
| 25 | +public class DownloadThreadBooster { |
| 26 | + public static final int MAX_SIZE = 10; |
| 27 | + private BlockingQueue<IPFSFileEntity> queue ; |
| 28 | + private String DEF_PATH; |
| 29 | + private static String savePath; |
| 30 | + private boolean running =false; |
| 31 | + private DWConsumer dwConsumer; |
| 32 | + |
| 33 | + private IPFS ipfs1; |
| 34 | + |
| 35 | + private DownloadThreadBooster(){ |
| 36 | + DEF_PATH = AppGlobalCnst.consturactPath(Launcher.appBasePath,"download"); |
| 37 | + savePath = DEF_PATH; |
| 38 | + File desk = new File(savePath); |
| 39 | + if(!desk.exists())desk.mkdirs(); |
| 40 | + dwConsumer = new DWConsumer(); |
| 41 | + queue = new ArrayBlockingQueue<>(MAX_SIZE); |
| 42 | + } |
| 43 | + |
| 44 | + private static class ThreadBoosterHold { |
| 45 | + private static DownloadThreadBooster dtb = new DownloadThreadBooster(); |
| 46 | + } |
| 47 | + |
| 48 | + public static DownloadThreadBooster getInstance(){ |
| 49 | + return ThreadBoosterHold.dtb; |
| 50 | + } |
| 51 | + |
| 52 | + public BlockingQueue<IPFSFileEntity> getQueue() { |
| 53 | + return queue; |
| 54 | + } |
| 55 | + |
| 56 | + public String getSavePath() { |
| 57 | + return savePath; |
| 58 | + } |
| 59 | + |
| 60 | + public void setSavePath(String savePath) { |
| 61 | + this.savePath = savePath; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * |
| 66 | + */ |
| 67 | + class DWConsumer extends Thread{ |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + download(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * 下载 |
| 75 | + */ |
| 76 | + private void download(){ |
| 77 | + while (true){ |
| 78 | + try { |
| 79 | + IPFSFileEntity entity = queue.take(); |
| 80 | + IPFS ipfs = getIpfs(); |
| 81 | + String hash58 = entity.getHash(); |
| 82 | + String fileName = StringUtils.isBlank(entity.getSaveName()) ? entity.getHash()+(entity.getSuffix()==null?"":entity.getSuffix()) : entity.getSaveName(); |
| 83 | + ByteArrayOutputStream baos=null; |
| 84 | + FileOutputStream fos = null; |
| 85 | + try { |
| 86 | + Multihash multihash = Multihash.fromBase58(hash58); |
| 87 | + byte[] datas = ipfs.get(multihash); |
| 88 | + long ts = System.currentTimeMillis(); |
| 89 | + String tmpFileName = AppGlobalCnst.consturactPath(savePath,fileName); |
| 90 | + File tmpFile = new File(tmpFileName); |
| 91 | + if(tmpFile.exists()){ |
| 92 | + tmpFile.delete(); |
| 93 | + } |
| 94 | + tmpFile.createNewFile(); |
| 95 | + baos = new ByteArrayOutputStream(); |
| 96 | + baos.write(datas); |
| 97 | + |
| 98 | + fos = new FileOutputStream(tmpFile); |
| 99 | + |
| 100 | + baos.writeTo(fos); |
| 101 | + // DataOutputStream dos = new DataOutputStream(new FileOutputStream(tmpFile)); |
| 102 | + fos.flush(); |
| 103 | + File saveFile = new File(AppGlobalCnst.consturactPath(savePath,fileName+".mp4")); |
| 104 | + if(saveFile.exists())saveFile.delete(); |
| 105 | + tmpFile.renameTo(saveFile); |
| 106 | + System.out.println("success download..."); |
| 107 | + } catch (IOException e) { |
| 108 | + e.printStackTrace(); |
| 109 | + }finally { |
| 110 | + try { |
| 111 | + if(baos!=null)baos.close(); |
| 112 | + if(fos!=null)fos.close(); |
| 113 | + } catch (IOException e) { |
| 114 | + e.printStackTrace(); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + } catch (InterruptedException e) { |
| 119 | + e.printStackTrace(); |
| 120 | + } |
| 121 | + try { |
| 122 | + TimeUnit.SECONDS.sleep(2); |
| 123 | + } catch (InterruptedException e1) { |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * 添加下载队列 |
| 131 | + * @param entity |
| 132 | + * @return true add success false |
| 133 | + */ |
| 134 | + public boolean addDownloadTask(IPFSFileEntity entity){ |
| 135 | + boolean b = queue.offer(entity); |
| 136 | + return b; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * 启动下载 |
| 141 | + */ |
| 142 | + public void start(){ |
| 143 | + if(!running){ |
| 144 | + running = true; |
| 145 | + dwConsumer.start(); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * |
| 151 | + * @return |
| 152 | + */ |
| 153 | + private IPFS getIpfs(){ |
| 154 | + if(ipfs1==null){ |
| 155 | + String url = ConfigurationHelper.getInstance().getIPFSAddress(); |
| 156 | + ipfs1 = new IPFS(url); |
| 157 | + } |
| 158 | + return ipfs1; |
| 159 | + } |
| 160 | + |
| 161 | +} |
0 commit comments