All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.base.web.FilesUploadContrller Maven / Gradle / Ivy

/*
 * @(#)FilesUploadContrller.java	2020年8月28日
 *
 * Copyright (c) 2020. All Rights Reserved.
 *
 */

package com.github.javaclub.base.web;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.github.javaclub.base.annotation.WithApiResult;
import com.github.javaclub.ossclient.OSS;
import com.github.javaclub.ossclient.OssConstants;
import com.github.javaclub.sword.annotation.swagger.ApiResponseObject;
import com.github.javaclub.sword.annotation.swagger.ApiResponseProperty;
import com.github.javaclub.sword.core.Maps;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * FilesUploadContrller
 *
 * @version $Id: FilesUploadContrller.java 2020-08-28 14:32:06 Exp $
 */
@RestController
@WithApiResult
@Api(tags = "管理端: 文件上传")
@RequestMapping("/manage")
public class FilesUploadContrller {

	static final Logger log = LoggerFactory.getLogger(FilesUploadContrller.class);

	@PostMapping(value = "/files/imageUpload")
	@ApiOperation(value = "上传图片")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "token", value = "鉴权token", paramType = "header", dataType = "string", required = true), 
		@ApiImplicitParam(name = "key", value = "指定上传用途key(可选英文字符串)", paramType = "query", dataType = "string", required = false), 
		@ApiImplicitParam(name = "file", value = "要上传的文件", paramType = "form", dataType = "__file", required = true)
	})
	@ApiResponseObject(name = "AdminFileUploadResult", description = "文件上传结果", 
		properties = {
			@ApiResponseProperty(name = "url", description = "资源访问URL", type = "string"), 
			@ApiResponseProperty(name = "total", description = "文件大小(byte字节)", type = "long")
		}
	)
	public Map imageUpload(@RequestParam(name = "key", required = false) String key, 
											@RequestParam("file") MultipartFile file) throws Exception {
		Map params = Maps.createMap(
			OssConstants.FILE_FORMAT_PARAM_KEY, "jpg, jpeg, png",
			OssConstants.EXTRA_BIZ_KEY_PARAM, key, 
			"total", file.getSize()
		);
		params.put("total", file.getSize());
		
		String relativePath = "images/";
		String imageUrl = OSS.get().upload(file, relativePath, params);
		
		return Maps.createMap(
			"url", imageUrl,
			"total", file.getSize()
		);
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy