package com.bocloud.ims.booter.controller.resource.storage;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.Pager;
import com.bocloud.common.model.RequestContext;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.JSONTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.ims.entity.resource.BucketObject;
import com.bocloud.ims.interfaces.resource.BucketObjectService;

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

@RestController
@RequestMapping("/v1/bucket/object")
@Api(value = "存储桶对象管理", tags = "存储桶对象管理")
public class BucketObjectController {

	@Autowired
	private BucketObjectService bucketObjectService;

	/**
	 * 查询对象
	 *
	 * @param pager
	 * @param context
	 * @return
	 * @throws Exception
	 */
	@GetMapping
	@ApiOperation(value = "存储桶对象列表", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult list(Pager pager, @Value(Common.REQ_CONTEXT) RequestContext context) {
		return bucketObjectService.list(pager.getPage(), pager.getRows(), pager.getParams(), pager.getSorter(),
				pager.getSimple(), context);
	}

	/**
	 * 创建对象
	 *
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/upload", method = { RequestMethod.POST })
	@ApiOperation(value = "存储桶对象新增", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult upload(@RequestBody BucketObject bucketObject,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != bucketObject) {
			return bucketObjectService.upload(bucketObject, context);
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 查询对象详情
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = { RequestMethod.GET })
	@ApiOperation(value = "存储桶对象详情", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult detail(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return bucketObjectService.detail(id, context);
	}

	/**
	 * 删除对象
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = { RequestMethod.DELETE })
	@ApiOperation(value = "存储桶对象删除", response = BocloudResult.class, httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult remove(@PathVariable(value = Common.ID) Long id,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		return bucketObjectService.remove(id, context);
	}

	/**
	 * 下载对象
	 *
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}/download", method = { RequestMethod.GET })
	@ApiOperation(value = "存储桶对象下载", response = BocloudResult.class, httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult download(@PathVariable(value = Common.ID) Long id, HttpServletRequest request,
			HttpServletResponse response) {
		BocloudResult bocloudResult = bucketObjectService.download(id);
		if (bocloudResult.isSuccess()) {
			JSONObject object = (JSONObject) bocloudResult.getData();
			String name = object.getString("name");
			byte[] fileBytes = object.getBytes("fileBytes");
			InputStream inputStream = new ByteArrayInputStream(fileBytes);
			try {
				response.addHeader("Content-Disposition", "attachment;filename=" + name);
				response.setContentType("application/octet-stream");
				IOUtils.copy(inputStream, response.getOutputStream(), 8 * 1000 * 1000);
			} catch (IOException e) {
			} finally {
				try {
					if (inputStream != null) {
						inputStream.close();
					}
				} catch (IOException e) {
				}
			}
			return null;
		} else {
			return bocloudResult;
		}
	}

	/**
	 * 设置acl
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/acl", method = { RequestMethod.PUT })
	@ApiOperation(value = "存储桶对象设置ACL", response = BocloudResult.class, httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult acl(@RequestBody BucketObject object, @Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != object) {
			return bucketObjectService.acl(object, context);
		}
		return ResultTools.formatErrResult();
	}

	/**
	 * 创建文件夹
	 * 
	 * @param params
	 * @param user
	 * @return
	 */
	@PostMapping("/mkdir")
	@ApiOperation(value = "存储桶对象新增", response = BocloudResult.class, httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	public BocloudResult mkdir(@RequestBody BucketObject bucketObject,
			@Value(Common.REQ_CONTEXT) RequestContext context) {
		if (null != bucketObject) {
			return bucketObjectService.mkdir(bucketObject, context);
		}
		return ResultTools.formatErrResult();
	}

}
