image view

This commit is contained in:
devezhao-mbp 2019-05-02 20:26:50 +08:00
parent aabccaf66e
commit be3e110bb7
5 changed files with 40 additions and 49 deletions

View file

@ -190,8 +190,14 @@ public class QiniuCloud {
if (fileName.contains("__")) { if (fileName.contains("__")) {
fileName = fileName.replace("__", "_"); fileName = fileName.replace("__", "_");
} }
if (fileName.length() > 43) { if (fileName.contains("+")) {
fileName = fileName.substring(0, 20) + "..." + fileName.substring(fileName.length() - 20); 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()); String datetime = CalendarUtils.getDateFormat("yyyyMMddHHmmssSSS").format(CalendarUtils.now());

View file

@ -27,7 +27,6 @@ import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
@ -36,6 +35,7 @@ import com.rebuild.server.helper.QiniuCloud;
import com.rebuild.server.helper.SysConfiguration; import com.rebuild.server.helper.SysConfiguration;
import com.rebuild.web.BaseControll; import com.rebuild.web.BaseControll;
import cn.devezhao.commons.CodecUtils;
import cn.devezhao.commons.web.ServletUtils; import cn.devezhao.commons.web.ServletUtils;
/** /**
@ -53,12 +53,17 @@ public class FileViewerControll extends BaseControll {
String filePath = request.getRequestURI(); String filePath = request.getRequestURI();
filePath = filePath.split("/filex/img/")[1]; filePath = filePath.split("/filex/img/")[1];
int minutes = 60; int minutes = 24 * 60;
ServletUtils.addCacheHead(response, minutes); ServletUtils.addCacheHead(response, minutes);
// Local storage // Local storage
if (!QiniuCloud.instance().available()) { if (!QiniuCloud.instance().available()) {
response.setContentType("image/jpeg"); String fileName = parseFileName(filePath);
String mimeType = request.getServletContext().getMimeType(fileName);
if (mimeType != null) {
response.setContentType(mimeType);
}
writeLocalFile(filePath, response); writeLocalFile(filePath, response);
return; return;
} }
@ -80,6 +85,7 @@ public class FileViewerControll extends BaseControll {
if (!QiniuCloud.instance().available()) { if (!QiniuCloud.instance().available()) {
String fileName = parseFileName(filePath); String fileName = parseFileName(filePath);
response.setHeader("Content-Disposition", "attachment;filename=" + fileName); response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
ServletUtils.setNoCacheHeaders(response); ServletUtils.setNoCacheHeaders(response);
writeLocalFile(filePath, response); writeLocalFile(filePath, response);
return; return;
@ -108,22 +114,21 @@ public class FileViewerControll extends BaseControll {
* @param response * @param response
*/ */
protected static boolean writeLocalFile(String filePath, HttpServletResponse response) throws IOException { protected static boolean writeLocalFile(String filePath, HttpServletResponse response) throws IOException {
filePath = CodecUtils.urlDecode(filePath);
File tmp = SysConfiguration.getFileOfTemp(filePath); File tmp = SysConfiguration.getFileOfTemp(filePath);
if (!tmp.exists()) { if (!tmp.exists()) {
response.sendError(404); response.sendError(HttpServletResponse.SC_NOT_FOUND);
return false; return false;
} }
try (InputStream fis = new FileInputStream(tmp)) { try (InputStream fis = new FileInputStream(tmp)) {
response.setContentLength(fis.available());
OutputStream os = response.getOutputStream(); OutputStream os = response.getOutputStream();
try { int count = 0;
int count = 0; byte[] buffer = new byte[1024 * 1024];
byte[] buffer = new byte[1024 * 1024]; while ((count = fis.read(buffer)) != -1) {
while ((count = fis.read(buffer)) != -1) { os.write(buffer, 0, count);
os.write(buffer, 0, count);
}
} finally {
IOUtils.closeQuietly(os);
} }
return true; return true;
} }

View file

@ -78,7 +78,7 @@
<img alt="Avatar" src="<%=theUser.getAvatarUrl(true)%>"> <img alt="Avatar" src="<%=theUser.getAvatarUrl(true)%>">
<label> <label>
<i class="zmdi zmdi-camera"></i><br>更改头像 <i class="zmdi zmdi-camera"></i><br>更改头像
<input type="file" id="avatar-input" accept="image/*"> <input type="file" id="avatar-input" accept="image/png,image/jpeg,image/gif">
</label> </label>
</div> </div>
<div class="clearfix"></div> <div class="clearfix"></div>

View file

@ -527,9 +527,10 @@ class RbFormImage extends RbFormElement {
let that = this let that = this
let mp let mp
$createUploader(this.refs['upload-input'], function (res) { $createUploader(this.refs['upload-input'], function (res) {
if (!mp) mp = new Mprogress({ template: 2, start: true }) if (!mp) mp = new Mprogress({ template: 1, start: true })
mp.set(res.percent / 100) mp.set(res.percent / 100) // 0.x
}, function (res) { }, function (res) {
if (mp) mp.end()
let paths = that.state.value let paths = that.state.value
paths.push(res.key) paths.push(res.key)
that.handleChange({ target: { value: paths } }, true) that.handleChange({ target: { value: paths } }, true)
@ -736,7 +737,7 @@ class RbFormAvatar extends RbFormElement {
return ( return (
<div className="img-field avatar"> <div className="img-field avatar">
<span title="选择头像图片"> <span title="选择头像图片">
<input type="file" className="inputfile" ref="upload-input" id={this.props.field + '-input'} accept="image/*" /> <input type="file" className="inputfile" ref="upload-input" id={this.props.field + '-input'} accept="image/png,image/jpeg,image/gif" />
<label htmlFor={this.props.field + '-input'} className="img-thumbnail img-upload"> <label htmlFor={this.props.field + '-input'} className="img-thumbnail img-upload">
<img src={aUrl} /> <img src={aUrl} />
</label> </label>
@ -755,21 +756,13 @@ class RbFormAvatar extends RbFormElement {
componentDidMount() { componentDidMount() {
super.componentDidMount() super.componentDidMount()
let that = this let that = this
$(that.refs['upload-input']).html5Uploader({ let mp
name: that.props.field, $createUploader(this.refs['upload-input'], function (res) {
postUrl: rb.baseUrl + '/filex/upload?cloud=auto&type=image', if (!mp) mp = new Mprogress({ template: 1, start: true })
onClientLoad: function (e, file) { mp.set(res.percent / 100) // 0.x
if (file.type.substr(0, 5) !== 'image') { }, function (res) {
rb.highbar('请上传图片') if (mp) mp.end()
return false that.handleChange({ target: { value: res.key } }, true)
}
},
onSuccess: function (d) {
d = JSON.parse(d.currentTarget.response)
if (d.error_code === 0) {
that.handleChange({ target: { value: d.data } }, true)
} else rb.hberror(d.error_msg || '上传失败,请稍后重试')
}
}) })
} }
} }

View file

@ -2,22 +2,9 @@
$(document).ready(function () { $(document).ready(function () {
if (location.hash === '#secure') $('.nav-tabs a:eq(1)').trigger('click') if (location.hash === '#secure') $('.nav-tabs a:eq(1)').trigger('click')
$('#avatar-input').html5Uploader({ $createUploader('#avatar-input', null, function (res) {
name: 'avatar-input', let aUrl = `${rb.baseUrl}/filex/img/${res.key}?imageView2/2/w/100/interlace/1/q/100`
postUrl: rb.baseUrl + '/filex/upload?cloud=auto&type=image', $('.avatar img').attr({ 'src': aUrl, 'data-src': res.key })
onClientLoad: function (e, file) {
if (file.type.substr(0, 5) !== 'image') {
rb.highbar('请上传图片')
return false
}
},
onSuccess: function (d) {
d = JSON.parse(d.currentTarget.response)
if (d.error_code === 0) {
let aUrl = `${rb.baseUrl}/filex/img/${d.data}?imageView2/2/w/100/interlace/1/q/100`
$('.avatar img').attr({ 'src': aUrl, 'data-src': d.data })
} else rb.hberror(d.error_msg || '上传失败,请稍后重试')
}
}) })
$('.J_email').click(() => { renderRbcomp(<DlgChangeEmail />) }) $('.J_email').click(() => { renderRbcomp(<DlgChangeEmail />) })