package org.springblade.modules.resource.controller;


import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.boot.file.BladeFileUtil;
import org.springblade.core.boot.file.FileProxyManager;
import org.springblade.core.boot.file.LocalFile;
import org.springblade.core.tool.api.R;
import org.springblade.modules.resource.enums.SystemConstant;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

@RestController
@RequestMapping({"blade-resource/disk"})
@Api(
	value = "上传管理",
	tags = {"上传管理"}
)
public class UploadController extends BladeController {
	private static Map<String, String> dirs = Maps.newHashMap();
	private static String DIR = "file";

	@RequestMapping({"/put-file"})
	public R<?> fileDummyUpload(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (!MultipartHttpServletRequest.class.isInstance(request)) {
			return R.fail("非文件上传协议");
		} else {
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
			Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
			if (fileMap.isEmpty()) {
				return R.fail("无上传内容");
			} else {
				Object fs = Collections.emptyMap();
				String domain = SystemConstant.me().getDomain();

				LocalFile file;
				for(Iterator var7 = fileMap.entrySet().iterator(); var7.hasNext(); fs = this.create(domain + StringUtils.replace(file.getUploadVirtualPath(), "//", "/"), file.getFileName())) {
					Entry<String, MultipartFile> entry = (Entry)var7.next();
					String ext = ((MultipartFile)entry.getValue()).getOriginalFilename();
					int end = StringUtils.lastIndexOf(ext, ".");
					String dir = DIR;
					if (end > 0) {
						ext = StringUtils.lowerCase(StringUtils.substring(ext, end));
//						dir = (String)dirs.get(ext);
					}

					file = this.getFile((MultipartFile)entry.getValue(), dir);
					file.transfer(FileProxyManager.me().getDefaultFileProxyFactory(), StringUtils.equals("image", dir));
				}

				return R.data(fs);
			}
		}
	}

	private org.springblade.core.oss.model.BladeFile create(String link, String name) {
		org.springblade.core.oss.model.BladeFile file = new org.springblade.core.oss.model.BladeFile();
		file.setLink(link);
		file.setName(name);
		return file;
	}

	public UploadController() {
	}

	/*static {
		try {
			Map<String, String> extMap = (Map)FieldUtils.readDeclaredStaticField(BladeFileUtil.class, "extMap", true);
			Iterator var1 = extMap.entrySet().iterator();

			label30:
			while(true) {
				Entry entry;
				do {
					if (!var1.hasNext()) {
						break label30;
					}

					entry = (Entry)var1.next();
				} while("allfile".equals(entry.getKey()));

				String[] sub = StringUtils.split((String)entry.getValue(), ",");
				String[] var4 = sub;
				int var5 = sub.length;

				for(int var6 = 0; var6 < var5; ++var6) {
					String key = var4[var6];
					dirs.put(key, entry.getKey() + "");
				}
			}
		} catch (IllegalAccessException var8) {
			var8.printStackTrace();
		}

	}*/
}
