纯粹的图床网站

This commit is contained in:
hellohao
2019-01-30 22:19:38 +08:00
commit 12f29cd2a1
82 changed files with 24770 additions and 0 deletions
@@ -0,0 +1,146 @@
package cn.hellohao.controller;
import cn.hellohao.pojo.Images;
import cn.hellohao.pojo.Keys;
import cn.hellohao.pojo.User;
import cn.hellohao.pojo.vo.PageResultBean;
import cn.hellohao.service.ImgService;
import cn.hellohao.service.KeysService;
import cn.hellohao.service.UserService;
import cn.hellohao.service.impl.ImgServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private ImgService imgService;
@Autowired
private KeysService keysService;
@Autowired
private UserService userService;
// 进入后台页面
@RequestMapping(value = "/admin.do")
public String goadmin(HttpSession session, Model model, HttpServletRequest request) {
Integer storageType1 = (Integer) session.getAttribute("storageType");
Integer storageType = keysService.selectKeysType();
Keys key = keysService.selectKeys(storageType);
User u = (User) session.getAttribute("user");
if(u.getLevel()>1) {
model.addAttribute("counts", imgService.counts(null));
model.addAttribute("getUserTotal", userService.getUserTotal());
}else {
model.addAttribute("counts", imgService.countimg(u.getId()));
}
model.addAttribute("username", u.getUsername());
model.addAttribute("level", u.getLevel());
model.addAttribute("email", u.getEmail());
model.addAttribute("loginid", 100);
//key信息
model.addAttribute("AccessKey", key.getAccessKey());
model.addAttribute("AccessSecret", key.getAccessSecret());
model.addAttribute("Endpoint", key.getEndpoint());
model.addAttribute("Bucketname", key.getBucketname());
model.addAttribute("RequestAddress", key.getRequestAddress());
model.addAttribute("StorageType", storageType);
model.addAttribute("text_foot", "<li><a href=\"http://www.hellohao.cn/\" target=\"_blank\">Hellohao &copy;</a></li><li>切勿上传违反中华人民共和国互联网法律条约资源</li>");
if (u.getLevel() > 1) {
model.addAttribute("htgl",
"<input id=\"isadmin\" onclick=\"openLoginModal();\" class=\"btn btn-default\" type=\"button\" value=\"后台管理\">");
}
return "admin/table";
}
@RequestMapping(value = "/selecttable.do")
@ResponseBody
public PageResultBean<Images> selectByFy(HttpSession session, Integer pageNum, Integer pageSize) {
User u = (User) session.getAttribute("user");
// 使用Pagehelper传入当前页数和页面显示条数会自动为我们的select语句加上limit查询
// 从他的下一条sql开始分页
PageHelper.startPage(pageNum, pageSize);
List<Images> images = null;
if(u.getLevel()>1){ //根据用户等级查询管理员查询所有的信息
images = imgService.selectimg(null);// 这是我们的sql
}else{
images = imgService.selectimg(u.getId());// 这是我们的sql
}
// 使用pageInfo包装查询
PageInfo<Images> rolePageInfo = new PageInfo<>(images);//
return new PageResultBean<>(rolePageInfo.getTotal(), rolePageInfo.getList());
}
@PostMapping("/updatekey.do")
@ResponseBody
public String updatekey(Keys key) {
Integer ret = keysService.updateKey(key);
JSONArray jsonArray = new JSONArray();
jsonArray.add(ret);
return jsonArray.toString();
}
@PostMapping("/deleimg.do")
@ResponseBody
public String deleimg(HttpSession session,Integer id) {
Integer storageType = (Integer) session.getAttribute("storageType");
Images images = imgService.selectByPrimaryKey(id);
Keys key = keysService.selectKeys(storageType);
ImgServiceImpl de = new ImgServiceImpl();
de.delect(key, images.getImgname());
JSONArray jsonArray = new JSONArray();
Integer ret = imgService.deleimg(id);
jsonArray.add(ret);
return jsonArray.toString();
}
//修改资料
@PostMapping("/change.do")
@ResponseBody
public String change(HttpSession session,User user) {
System.out.println(user.getUsername());
Integer count = userService.checkUsername(user.getUsername());
User u = (User) session.getAttribute("user");
user.setEmail(u.getEmail());
JSONArray jsonArray = new JSONArray();
if(count==0) {
Integer ret = userService.change(user);
jsonArray.add(ret);
if(u.getEmail()!=null&&u.getPassword()!=null) {
session.removeAttribute("user");
//刷新view
session.invalidate();
}
}else {
jsonArray.add("-1");
}
// -1 用户名重复
return jsonArray.toString();
}
@GetMapping(value = "/images/{id}")
@ResponseBody
public Images selectByFy(@PathVariable("id") Integer id) {
return imgService.selectByPrimaryKey(id);
}
}
@@ -0,0 +1,158 @@
package cn.hellohao.controller;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.fastjson.JSONArray;
import cn.hellohao.pojo.Images;
import cn.hellohao.pojo.Keys;
import cn.hellohao.pojo.User;
import cn.hellohao.service.KeysService;
import cn.hellohao.service.UserService;
import cn.hellohao.service.impl.NOSImageupload;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
@Controller
public class UpdateImgController {
@Autowired
private NOSImageupload nOSImageupload;
@Autowired
private UserService userService;
@Autowired
private KeysService keysService;
public static String getSubString(String text, String left, String right) {
String result = "";
int zLen;
if (left == null || left.isEmpty()) {
zLen = 0;
} else {
zLen = text.indexOf(left);
if (zLen > -1) {
zLen += left.length();
} else {
zLen = 0;
}
}
int yLen = text.indexOf(right, zLen);
if (yLen < 0 || right == null || right.isEmpty()) {
yLen = text.length();
}
result = text.substring(zLen, yLen);
return result;
}
@RequestMapping({"/","/index.do"})
public String indexImg(Model model,HttpServletRequest request,HttpSession httpSession){
Integer storageType = keysService.selectKeysType();
System.out.println("类型=========="+storageType);
Keys key = keysService.selectKeys(storageType);
nOSImageupload.Initialize(key);
//查询哪种对象存储
httpSession.setAttribute("storageType", storageType);
model.addAttribute("text_foot", "<li><a href=\"http://www.hellohao.cn/\" target=\"_blank\">Hellohao &copy;</a></li><li>切勿上传违反中华人民共和国互联网法律条约资源</li>");
User u = (User) httpSession.getAttribute("user");
String email = (String) httpSession.getAttribute("email");
String pass = (String) httpSession.getAttribute("pass");
if(email!=null && pass!=null) {
//登陆成功
Integer ret = userService.login(u.getEmail(), u.getPassword());
if(ret>0) {
User user = userService.getUsers(u.getEmail());
model.addAttribute("username",user.getUsername());
model.addAttribute("level",user.getLevel());
model.addAttribute("loginid",100);
model.addAttribute("imgcount",3);
model.addAttribute("fileSize",5120);
}else {
model.addAttribute("loginid",-1);
model.addAttribute("imgcount",1);
}
}else {
model.addAttribute("loginid",-2);
model.addAttribute("imgcount",1);
model.addAttribute("imgcount",1);
model.addAttribute("fileSize",3072);
}
return "index";
}
@RequestMapping(value="/upimg.do")
@ResponseBody
public String exit(HttpServletRequest request,HttpServletResponse response,HttpSession session
,@RequestParam(value = "file", required = false) MultipartFile[] file,String filename) throws Exception{
long stime = System.currentTimeMillis();
User u = (User) session.getAttribute("user");
JSONArray jsonArray = new JSONArray();
Map<String, MultipartFile> map = new HashMap<>();
for (MultipartFile multipartFile : file) {
//获取文件名
String fileName = multipartFile.getOriginalFilename();
String lastname = fileName.substring(fileName.lastIndexOf(".") + 1);
if (!multipartFile.isEmpty()) { //判断文件是否为空
map.put(lastname, multipartFile);
//multipartFile.getOriginalFilename(); //文件名
//multipartFile.getSize(); //文件大小
}
}
Map<String, Integer> m =nOSImageupload.Imageupload(map);
Images img = new Images();
// for (String string : m) {
// jsonArray.add(string);
// System.out.println("文件名字:==="+string);
// if(userid!=null) {
// //img.setImgname(imgname);//图片名字
// img.setImgurl(string);//图片链接
// img.setUserid(userid);//用户id
// userService.insertimg(img);
// }
//
// }
for (Map.Entry<String, Integer> entry : m.entrySet()) {
jsonArray.add(entry.getKey());
if(u!=null) {
//img.setImgname(imgname);//图片名字
img.setImgurl(entry.getKey());//图片链接
img.setUserid(u.getId());//用户id
img.setSizes((entry.getValue())/1024);
img.setImgname(getSubString(entry.getKey(), "hellohao.nos-eastchina1.126.net/", ""));
userService.insertimg(img);
long etime = System.currentTimeMillis();
System.out.println("上传图片所用时长:"+String.valueOf(etime-stime)+"ms");
}
}
return jsonArray.toString();
}
}
@@ -0,0 +1,81 @@
package cn.hellohao.controller;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import cn.hellohao.pojo.User;
import cn.hellohao.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/register.do")
@ResponseBody
public String Register(User user) {
//取当前时间
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
String birthder = df.format(new Date());// new Date()为获取当前系统时间
user.setLevel(1);
user.setBirthder(birthder);
Integer ret = userService.register(user);
JSONArray jsonArray = new JSONArray();
jsonArray.add(ret);
return jsonArray.toString();
}
@RequestMapping("/login.do")
@ResponseBody
public String login(HttpServletRequest request,HttpSession httpSession,String email,String password) {
JSONArray jsonArray = new JSONArray();
Integer ret = userService.login(email, password);
if(ret>0) {
jsonArray.add(1);
User user = userService.getUsers(email);
httpSession.setAttribute("user",user);
httpSession.setAttribute("email",user.getEmail());
httpSession.setAttribute("pass",user.getPassword());
//登录成功之后把账号密码存入session
}else {
jsonArray.add(0);
}
return jsonArray.toString();
}
//退出
@RequestMapping(value="/exit.do")
@ResponseBody
public String exit(Model model,HttpServletRequest request,HttpSession session){
JSONObject jsonObject = new JSONObject();
//注销,移除session
User user = (User) session.getAttribute("user");
if(user.getEmail()!=null&&user.getPassword()!=null) {
session.removeAttribute(user.getEmail());
session.removeAttribute(user.getPassword());
//刷新view
session.invalidate();
jsonObject.put("exit", 1);
}
return jsonObject.toString();
}
}