user-avatar

This commit is contained in:
devezhao 2019-05-10 13:57:39 +08:00
parent 23793f4283
commit 36b2ae52e8
8 changed files with 71 additions and 19 deletions

View file

@ -21,8 +21,10 @@ package com.rebuild.server.helper;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;
@ -38,6 +40,7 @@ import com.qiniu.util.StringMap;
import com.rebuild.server.RebuildException;
import cn.devezhao.commons.CalendarUtils;
import cn.devezhao.commons.CodecUtils;
import cn.devezhao.commons.http4.HttpClientEx;
/**
@ -201,17 +204,38 @@ public class QiniuCloud {
* @see #parseFileName(String)
*/
public static String formatFileKey(String fileName) {
while (fileName.contains("__")) {
fileName = fileName.replace("__", "_");
}
if (fileName.contains("+")) {
fileName = fileName.replace("+", "");
}
if (fileName.contains("#")) {
fileName = fileName.replace("#", "");
}
if (fileName.length() > 41) {
fileName = fileName.substring(0, 20) + "-" + fileName.substring(fileName.length() - 20);
return formatFileKey(fileName, true);
}
/**
* @param fileName
* @param keepName
* @return
* @see #parseFileName(String)
*/
public static String formatFileKey(String fileName, boolean keepName) {
if (!keepName) {
String fileName_s[] = fileName.split("\\.");
fileName = UUID.randomUUID().toString().replace("-", "");
if (fileName_s.length > 1 && StringUtils.isNotBlank(fileName_s[fileName_s.length - 1])) {
fileName += "." + fileName_s[fileName_s.length - 1];
}
} else {
while (fileName.contains("__")) {
fileName = fileName.replace("__", "_");
}
if (fileName.contains("+")) {
fileName = fileName.replace("+", "");
}
if (fileName.contains("#")) {
fileName = fileName.replace("#", "");
}
if (fileName.contains("?")) {
fileName = fileName.replace("?", "");
}
if (fileName.length() > 41) {
fileName = fileName.substring(0, 20) + "-" + fileName.substring(fileName.length() - 20);
}
}
String datetime = CalendarUtils.getDateFormat("yyyyMMddHHmmssSSS").format(CalendarUtils.now());
@ -233,6 +257,28 @@ public class QiniuCloud {
return fileName;
}
/**
* URL 编码中文或特殊字符
*
* @param url
* @return
*/
public static String encodeUrl(String url) {
if (StringUtils.isBlank(url)) {
return url;
}
String url_s[] = url.split("/");
for (int i = 0; i < url_s.length; i++) {
String e = CodecUtils.urlEncode(url_s[i]);
if (e.contains("+")) {
e = e.replace("+", "%20");
}
url_s[i] = e;
}
return StringUtils.join(url_s, "/");
}
private static final QiniuCloud INSTANCE = new QiniuCloud();
/**
* @return

View file

@ -71,8 +71,6 @@ public class RequestWatchHandler extends HandlerInterceptorAdapter {
ServletUtils.setNoCacheHeaders(response);
}
System.err.println(requestUrl);
// If server status is not passed
if (!requestUrl.contains("/gw/server-status") && !Application.serversReady()) {
response.sendRedirect(ServerListener.getContextPath() + "/gw/server-status?s=" + CodecUtils.urlEncode(requestUrl));

View file

@ -31,6 +31,7 @@ import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
@ -69,7 +70,7 @@ public class FileUploader {
uploadName = QiniuCloud.formatFileKey(uploadName);
File file = null;
// 上传临时文件
if ("1".equals(request.getParameter("temp"))) {
if (BooleanUtils.toBoolean(request.getParameter("temp"))) {
uploadName = uploadName.split("/")[2];
file = SysConfiguration.getFileOfTemp(uploadName);
} else {

View file

@ -33,6 +33,7 @@ import com.rebuild.server.service.bizz.privileges.User;
import com.rebuild.utils.AppUtils;
import com.rebuild.web.BaseControll;
import cn.devezhao.commons.web.ServletUtils;
import cn.devezhao.persist4j.engine.ID;
/**
@ -75,11 +76,15 @@ public class UserAvatar extends BaseControll {
return;
}
final int minutes = 15;
ServletUtils.addCacheHead(response, minutes);
String avatarUrl = realUser.getAvatarUrl();
avatarUrl = QiniuCloud.encodeUrl(avatarUrl);
if (avatarUrl != null) {
avatarUrl = avatarUrl + "?imageView2/2/w/100/interlace/1/q/100";
if (QiniuCloud.instance().available()) {
avatarUrl = QiniuCloud.instance().url(avatarUrl);
avatarUrl = QiniuCloud.instance().url(avatarUrl, minutes * 60);
} else {
avatarUrl = AppUtils.getContextPath() + "/filex/img/" + avatarUrl;
}

View file

@ -19,7 +19,7 @@ final User currentUser = Application.getUserStore().getUser(AppUtils.getRequestU
<ul class="nav navbar-nav float-right rb-user-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="${baseUrl}/account/settings" data-toggle="dropdown">
<img src="<%=currentUser.getAvatarUrl(true)%>" alt="Avatar">
<img src="${baseUrl}/account/user-avatar" alt="Avatar">
<span class="user-name"><%=currentUser.getFullName()%></span>
</a>
<div class="dropdown-menu">

View file

@ -16,7 +16,7 @@ final User currentUser = Application.getUserStore().getUser(AppUtils.getRequestU
<ul class="nav navbar-nav float-right rb-user-nav">
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="${baseUrl}/account/settings" data-toggle="dropdown">
<img src="<%=currentUser.getAvatarUrl(true)%>" alt="Avatar">
<img src="${baseUrl}/account/user-avatar" alt="Avatar">
<span class="user-name"><%=currentUser.getFullName()%></span>
</a>
<div class="dropdown-menu">

View file

@ -92,7 +92,7 @@ $(document).ready(() => {
const init_upload = () => {
$('#upload-input').html5Uploader({
postUrl: rb.baseUrl + '/filex/upload?temp=1',
postUrl: rb.baseUrl + '/filex/upload?temp=yes',
onSelectError: function (field, error) {
if (error === 'ErrorType') rb.highbar('请上传 Excel/CSV 文件')
else if (error === 'ErrorMaxSize') rb.highbar('文件不能大于 20M')

View file

@ -54,6 +54,8 @@ public class QiniuCloudTest extends TestSupport {
String fileName2 = QiniuCloud.parseFileName(fileKey);
assertEquals(fileName, fileName2);
System.out.println("File2 key ... " + QiniuCloud.formatFileKey("_____1123++545#e+++f d fefe.txt"));
System.out.println("File2 key ... " + QiniuCloud.formatFileKey("_____1123++545#e+++?&&f d fefe.txt"));
System.out.println("File3 key ... " + QiniuCloud.formatFileKey("_____1123++545#e+++?&&f d fefe.txt", false));
System.out.println("File4 key ... " + QiniuCloud.formatFileKey("_____1123++545#e+++?&&f d fefe", false));
}
}