94 lines
3.5 KiB
Java
94 lines
3.5 KiB
Java
package com.ycwl.basic.utils;
|
||
|
||
import cn.hutool.core.thread.ThreadFactoryBuilder;
|
||
import cn.hutool.http.HttpUtil;
|
||
import com.ycwl.basic.constant.StorageConstant;
|
||
import com.ycwl.basic.mapper.SourceMapper;
|
||
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
|
||
import com.ycwl.basic.service.pc.ScenicService;
|
||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||
import com.ycwl.basic.storage.utils.StorageUtil;
|
||
import lombok.extern.slf4j.Slf4j;
|
||
import org.apache.commons.lang3.StringUtils;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.stereotype.Component;
|
||
|
||
import java.io.File;
|
||
import java.io.FileNotFoundException;
|
||
import java.io.FileOutputStream;
|
||
import java.io.IOException;
|
||
import java.io.InputStream;
|
||
import java.io.OutputStream;
|
||
import java.net.MalformedURLException;
|
||
import java.net.URL;
|
||
import java.util.UUID;
|
||
import java.util.concurrent.ArrayBlockingQueue;
|
||
import java.util.concurrent.ThreadFactory;
|
||
import java.util.concurrent.ThreadPoolExecutor;
|
||
import java.util.concurrent.TimeUnit;
|
||
import java.util.concurrent.atomic.AtomicInteger;
|
||
|
||
@Component
|
||
@Slf4j
|
||
public class VideoReUploader {
|
||
private static final ThreadFactory threadFactory = new ThreadFactoryBuilder()
|
||
.setNamePrefix("Vid-ReUp-")
|
||
.build();
|
||
private static final ThreadPoolExecutor executor = new ThreadPoolExecutor(8, 1024, 0L, TimeUnit.MILLISECONDS,
|
||
new ArrayBlockingQueue<>(1024),
|
||
threadFactory
|
||
);
|
||
|
||
@Autowired
|
||
private SourceMapper sourceMapper;
|
||
@Autowired
|
||
private ScenicService scenicService;
|
||
|
||
public void addTask(String url, Long sourceId) {
|
||
try {
|
||
URL _url = new URL(url);
|
||
if (!StringUtils.startsWith(_url.getHost(), "100.64.")) {
|
||
return;
|
||
}
|
||
} catch (MalformedURLException ignored) {
|
||
return;
|
||
}
|
||
SourceEntity entity = sourceMapper.getEntity(sourceId);
|
||
if (entity == null) {
|
||
return;
|
||
}
|
||
if (entity.getScenicId() == null) {
|
||
return;
|
||
}
|
||
if (entity.getType() != 1) {
|
||
return;
|
||
}
|
||
String tmpFilePath = UUID.randomUUID().toString();
|
||
executor.execute(() -> {
|
||
// 先下载,后上传
|
||
File dstFile = new File(tmpFilePath);
|
||
log.info("下载视频:{};sourceId:{}", url, sourceId);
|
||
long size = HttpUtil.downloadFile(url, dstFile);
|
||
log.info("下载视频完成:{};大小:{};sourceId:{}", url, size, sourceId);
|
||
String dstFilePath = StorageUtil.joinPath(StorageConstant.VIDEO_PIECE_PATH, entity.getId().toString() + ".mp4");
|
||
IStorageAdapter dstAdapter = scenicService.getScenicStorageAdapter(entity.getScenicId());
|
||
try {
|
||
log.info("开始上传:{};sourceId:{}", dstFilePath, sourceId);
|
||
String newUrl = dstAdapter.uploadFile("video/mp4", dstFile, dstFilePath);
|
||
log.info("上传成功:{};sourceId:{}", newUrl, sourceId);
|
||
SourceEntity updateEntity = new SourceEntity();
|
||
updateEntity.setId(sourceId);
|
||
updateEntity.setVideoUrl(newUrl);
|
||
sourceMapper.update(updateEntity);
|
||
} catch (Exception e) {
|
||
log.info("上传失败:{};sourceId:{}", dstFilePath, sourceId, e);
|
||
} finally {
|
||
try {
|
||
dstFile.delete();
|
||
} catch (Exception ignored) {
|
||
}
|
||
}
|
||
});
|
||
}
|
||
}
|