| | |
| | | package com.qxueyou.scc.teach.res.service.impl; |
| | | |
| | | |
| | | //import com.example.study.springboot.background.service.HweiYunOBSService; |
| | | //import com.example.study.springboot.config.HweiOBSConfig; |
| | | import com.obs.services.ObsClient; |
| | | import com.obs.services.exception.ObsException; |
| | | import com.obs.services.model.*; |
| | | import com.qxueyou.scc.base.model.FileMeta; |
| | | import com.qxueyou.scc.base.model.Result; |
| | | import com.qxueyou.scc.base.util.CollectionUtils; |
| | | import com.qxueyou.scc.base.util.QFileUtils; |
| | | import com.qxueyou.scc.base.util.UUIDUtils; |
| | | import com.qxueyou.scc.config.HweiOBSConfig; |
| | | import com.qxueyou.scc.teach.res.model.ResFile; |
| | | import com.qxueyou.scc.teach.res.service.HweiYunOBSService; |
| | | import com.qxueyou.scc.teach.res.service.IFileService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.catalina.core.ApplicationPart; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.apache.logging.log4j.LogManager; |
| | | import org.apache.logging.log4j.Logger; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.util.Calendar; |
| | | import java.util.GregorianCalendar; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * @ClassName: HweiYunOBSServiceImpl |
| | |
| | | private final Logger log = LogManager.getLogger(HweiYunOBSServiceImpl.class); |
| | | @Autowired |
| | | private HweiOBSConfig hweiOBSConfig; |
| | | |
| | | @Autowired |
| | | FileService fileService; |
| | | |
| | | public final static String FILE_TYPE_VIDEO = "video"; |
| | | |
| | |
| | | "JPG", FILE_TYPE_IMG,"JPEG", FILE_TYPE_IMG, "GIF", FILE_TYPE_IMG, "BMP", FILE_TYPE_IMG, "PNG", FILE_TYPE_IMG |
| | | |
| | | ); |
| | | |
| | | |
| | | @Override |
| | | public boolean delete(String objectKey) { |
| | | ObsClient obsClient = null; |
| | |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean delete(List<String> objectKeys) { |
| | | ObsClient obsClient = null; |
| | |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | public String fileUpload(MultipartFile uploadFile, String objectKey) { |
| | | public List<FileMeta> fileUpload(MultipartFile uploadFile, String objectKey) { |
| | | ObsClient obsClient = null; |
| | | List<FileMeta> files = new ArrayList<FileMeta>(2); |
| | | FileMeta fileMeta = null; |
| | | try { |
| | | String destPath = getDestPath(objectKey); |
| | | String bucketName = hweiOBSConfig.getBucketName(); |
| | |
| | | } |
| | | InputStream inputStream = uploadFile.getInputStream(); |
| | | long available = inputStream.available(); |
| | | // PutObjectRequest request = new PutObjectRequest(bucketName,objectKey,inputStream); |
| | | PutObjectRequest request = new PutObjectRequest(bucketName,destPath,inputStream); |
| | | ObjectMetadata objectMetadata = new ObjectMetadata(); |
| | | objectMetadata.setContentLength(available); |
| | | request.setMetadata(objectMetadata); |
| | | // request. |
| | | // 设置对象访问权限为公共读 |
| | | request.setAcl(AccessControlList.REST_CANNED_PUBLIC_READ); |
| | | PutObjectResult result = obsClient.putObject(request); |
| | | |
| | | fileMeta = new FileMeta(); |
| | | ResFile file = fileService.insertFileToDBTwo(objectKey, uploadFile.getSize(), result.getObjectUrl(), uploadFile.getContentType()); |
| | | // SetObjectMetadataRequest ObjectMetadataRequest = new SetObjectMetadataRequest(bucketName, destPath); |
| | | // ObjectMetadataRequest.setContentDisposition("inline"); |
| | | // obsClient.setObjectMetadata(ObjectMetadataRequest); |
| | | |
| | | fileMeta.setFileId(file.getFileId()); |
| | | fileMeta.setPath(result.getObjectUrl()); |
| | | fileMeta.setFileSize(uploadFile.getSize() / 1024 + "kb"); |
| | | fileMeta.setFileType(uploadFile.getContentType()); |
| | | fileMeta.setFileName(objectKey); |
| | | files.add(fileMeta); |
| | | // 读取该已上传对象的URL |
| | | log.info("已上传对象的URL" + result.getObjectUrl()); |
| | | return result.getObjectUrl(); |
| | | return files; |
| | | // return result.getObjectUrl(); |
| | | } catch (ObsException e) { |
| | | log.error("obs上传失败", e); |
| | | } catch (IOException e) { |
| | |
| | | String fileType = getFileType(name); |
| | | |
| | | StringBuffer path = new StringBuffer(128); |
| | | path.append(fileType); |
| | | // path.append(fileType); |
| | | |
| | | Calendar now = new GregorianCalendar(); |
| | | |
| | | path.append('/'); |
| | | path.append(now.get(Calendar.YEAR)); |
| | | path.append(StringUtils.leftPad(String.valueOf(now.get(Calendar.MONTH)), 2, '0')); |
| | | path.append('/'); |
| | | path.append(now.get(Calendar.DAY_OF_MONTH)); |
| | | path.append('/'); |
| | | // path.append('/'); |
| | | // path.append(now.get(Calendar.YEAR)); |
| | | // path.append(StringUtils.leftPad(String.valueOf(now.get(Calendar.MONTH)), 2, '0')); |
| | | // path.append('/'); |
| | | // path.append(now.get(Calendar.DAY_OF_MONTH)); |
| | | // path.append('/'); |
| | | path.append(UUIDUtils.UUID()); |
| | | path.append('.'); |
| | | path.append(QFileUtils.getFileFormat(name)); |
| | |
| | | } |
| | | return null; |
| | | } |
| | | } |
| | | } |