支持带密码文件夹.

This commit is contained in:
zhaojun1998
2019-09-08 21:05:58 +08:00
parent 0d47969b97
commit 0dd57e3889
8 changed files with 95 additions and 27 deletions

View File

@@ -0,0 +1,11 @@
package im.zhaojun.common.constant;
public class ZfileConstant {
public static final String HEADER_FILE_NAME = "header.md";
public static final String README_FILE_NAME = "readme.md";
public static final String PASSWORD_FILE_NAME = "password.txt";
}

View File

@@ -1,8 +1,8 @@
package im.zhaojun.common.controller;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.URLUtil;
import im.zhaojun.common.config.StorageTypeFactory;
import im.zhaojun.common.constant.ZfileConstant;
import im.zhaojun.common.enums.FileTypeEnum;
import im.zhaojun.common.enums.StorageTypeEnum;
import im.zhaojun.common.model.FileItem;
@@ -14,11 +14,13 @@ import im.zhaojun.common.service.SystemConfigService;
import im.zhaojun.common.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
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.RestController;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -34,9 +36,22 @@ public class FileController {
private SystemConfigService systemConfigService;
@GetMapping("/list")
public ResultBean list(String path, String sortBy, boolean descending) throws Exception {
public ResultBean list(String path, String sortBy, boolean descending, @RequestParam(required = false) String password) throws Exception {
List<FileItem> fileItems = fileService.fileList(StringUtils.removeDuplicateSeparator("/" + URLUtil.decode(path)));
for (FileItem fileItem : fileItems) {
if (ZfileConstant.PASSWORD_FILE_NAME.equals(fileItem.getName())) {
String url = StringUtils.removeDuplicateSeparator("/" + fileItem.getPath() + "/" + fileItem.getName());
if (!fileService.getTextContent(url).equals(password)) {
if (password != null && !"".equals(password)) return ResultBean.error("密码错误.");
return ResultBean.error("此文件夹需要密码.", ResultBean.REQUIRED_PASSWORD);
}
}
}
// 排序, 先按照文件类型比较, 文件夹在前, 文件在后, 然后根据 sortBy 字段排序, 默认为升序;
fileItems.sort((o1, o2) -> {
FileTypeEnum o1Type = o1.getType();
@@ -61,7 +76,6 @@ public class FileController {
if (descending) {
Collections.reverse(fileItems);
}
return ResultBean.successData(fileItems);
}

View File

@@ -6,9 +6,11 @@ public class ResultBean implements Serializable {
private static final long serialVersionUID = -8276264968757808344L;
private static final int SUCCESS = 0;
public static final int SUCCESS = 0;
private static final int FAIL = -1;
public static final int FAIL = -1;
public static final int REQUIRED_PASSWORD = -2;
private String msg = "操作成功";
@@ -53,6 +55,13 @@ public class ResultBean implements Serializable {
return resultBean;
}
public static ResultBean error(String msg, Integer code) {
ResultBean resultBean = new ResultBean();
resultBean.setCode(code);
resultBean.setMsg(msg);
return resultBean;
}
public String getMsg() {
return msg;
}

View File

@@ -1,20 +1,18 @@
package im.zhaojun.common.service;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpUtil;
import com.mpatric.mp3agic.ID3v1;
import com.mpatric.mp3agic.ID3v2;
import com.mpatric.mp3agic.Mp3File;
import im.zhaojun.common.config.ZfileCacheConfiguration;
import im.zhaojun.common.constant.ZfileConstant;
import im.zhaojun.common.enums.FileTypeEnum;
import im.zhaojun.common.enums.StorageTypeEnum;
import im.zhaojun.common.model.AudioInfo;
import im.zhaojun.common.model.FileItem;
import im.zhaojun.common.model.ImageInfo;
import im.zhaojun.common.model.SiteConfig;
import im.zhaojun.common.util.AudioHelper;
import im.zhaojun.common.util.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.cache.annotation.CacheConfig;
@@ -52,9 +50,9 @@ public interface FileService {
List<FileItem> fileItemList = fileService.fileList(path);
path = StringUtils.removeLastSeparator(path);
for (FileItem fileItem : fileItemList) {
if ("readme.md".equalsIgnoreCase(fileItem.getName())) {
if (ZfileConstant.README_FILE_NAME.equalsIgnoreCase(fileItem.getName())) {
siteConfig.setFooter(getTextContent(path + "/" + fileItem.getName()));
} else if ("header.md".equalsIgnoreCase(fileItem.getName())) {
} else if (ZfileConstant.HEADER_FILE_NAME.equalsIgnoreCase(fileItem.getName())) {
siteConfig.setHeader(getTextContent(path + "/" + fileItem.getName()));
}
}

View File

@@ -10,7 +10,8 @@ requirejs.config({
layer: '/layer/layer',
highlight: '/highlight/highlight.min',
DPlayer: '/DPlayer/DPlayer.min',
Shikwasa: '/shikwasa/shikwasa.min'
Shikwasa: '/shikwasa/shikwasa.min',
swal: '/sweetalert/sweetalert.min'
},
shim: {
zfile: {
@@ -33,5 +34,4 @@ requirejs.config({
});
requirejs(['index'], function(index) {
});

View File

@@ -1,7 +1,7 @@
define(['jquery', 'zfile', 'QRCode', 'contextMenu', 'marked'], function($, zfile, QRCode, contextMenu, marked) {
var index = {
LOCAL_STORAGE_KEY: "zadmin-config",
LOCAL_STORAGE_CONFIG_KEY: "zadmin-config",
buildInfo: function (info) {
$("#info .block .name").html(info.name);
$("#info .block .time").html(info.time);
@@ -15,10 +15,10 @@ define(['jquery', 'zfile', 'QRCode', 'contextMenu', 'marked'], function($, zfile
}
},
updateConfig: function (key, value) {
var systemConfigStr = localStorage.getItem(index.LOCAL_STORAGE_KEY);
var systemConfigStr = localStorage.getItem(index.LOCAL_STORAGE_CONFIG_KEY);
var systemConfig = JSON.parse(systemConfigStr);
systemConfig[key] = value;
localStorage.setItem(index.LOCAL_STORAGE_KEY, JSON.stringify(systemConfig));
localStorage.setItem(index.LOCAL_STORAGE_CONFIG_KEY, JSON.stringify(systemConfig));
},
updateListSize: function (value) {
var size;
@@ -50,7 +50,7 @@ define(['jquery', 'zfile', 'QRCode', 'contextMenu', 'marked'], function($, zfile
result = data.data;
var systemConfig = result.systemConfig;
var systemConfigCache = localStorage.getItem(index.LOCAL_STORAGE_KEY);
var systemConfigCache = localStorage.getItem(index.LOCAL_STORAGE_CONFIG_KEY);
if (systemConfigCache) {
systemConfig = JSON.parse(systemConfigCache);
}
@@ -120,7 +120,7 @@ define(['jquery', 'zfile', 'QRCode', 'contextMenu', 'marked'], function($, zfile
}
}
localStorage.setItem(index.LOCAL_STORAGE_KEY, JSON.stringify(systemConfig));
localStorage.setItem(index.LOCAL_STORAGE_CONFIG_KEY, JSON.stringify(systemConfig));
},
error: function (textStatus, errorThrown) {
alert("加载站点配置失败.!");

View File

@@ -1,4 +1,4 @@
define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwasa'], function ($, Mustache, layer, marked, hljs, DPlayer, Shikwasa){
define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwasa', 'swal'], function ($, Mustache, layer, marked, hljs, DPlayer, Shikwasa, swal){
var zfile = {
prefixPath: '/file',
@@ -7,6 +7,7 @@ define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwa
sizeObj: $(".size"),
infoObj: $("#info"),
listObj: $("#list"),
LOCAL_STORAGE_PWD_KEY_PREFIX: "zadmin-pwd-",
util: {
bytesToSize: function (bytes) {
if (bytes === 0) return '0 B';
@@ -29,8 +30,6 @@ define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwa
}
},
buildList: function (data) {
var path = zfile.getPath();
var list = [];
$.each(data, function (i, val) {
var fileType, url;
@@ -73,11 +72,16 @@ define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwa
path: zfile.getPath(),
sortBy: zfile.getSortBy(),
descending: zfile.getDescending() === 'desc',
search: false
search: false,
password: zfile.getPassword(zfile.getPath())
};
console.log("尝试获取 key: " + zfile.getPath());
$.extend(defaultSetting, settings);
if (!defaultSetting.search && defaultSetting.path !== '' && defaultSetting.path.charAt(defaultSetting.path.length - 1) !== '/') {
defaultSetting.path += '/';
}
@@ -88,20 +92,32 @@ define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwa
}
$("#items").attr("style", "opacity: 0.5;-moz-opacit: 0.5;");
$.ajax({
type: 'GET',
url: url,
data: {
path: encodeURIComponent(decodeURIComponent(defaultSetting.path)), // 先解码, 再编码, 防止重复编码
sortBy: defaultSetting.sortBy,
descending: defaultSetting.descending
descending: defaultSetting.descending,
password: defaultSetting.password
},
dataType: 'json',
success: function (result) {
zfile.clearList();
var data = result.data;
if (data.length === 0) {
if (result.code === -1) {
swal({
title: result.msg,
text: "请重新输入密码.",
icon: "error"
}).then(zfile.pwdModal);
} else if (result.code === -2) {
zfile.pwdModal();
} else if (data.length === 0) {
zfile.listObj.html('<div id="view-hint" class="l10n-empty">空文件夹</div>');
} else {
zfile.savePassword(zfile.getPath(), defaultSetting.password);
zfile.buildList(data);
}
},
@@ -122,6 +138,26 @@ define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwa
}
});
},
pwdModal: function () {
swal({
content: {
element: "input",
attributes: {
placeholder: "输入密码进行查看."
}
}
}).then(function (value) {
if (value !== null) {
zfile.listObjects({password: value});
}
});
},
savePassword: function(path, pwd) {
localStorage.setItem(zfile.LOCAL_STORAGE_PWD_KEY_PREFIX + path, pwd);
},
getPassword: function(path) {
return localStorage.getItem(zfile.LOCAL_STORAGE_PWD_KEY_PREFIX + path);
},
clearList: function () {
this.listObj.html('');
},
@@ -368,7 +404,6 @@ define(['jquery', 'Mustache', 'layer', 'marked', 'highlight', 'DPlayer', 'Shikwa
layer.open({
type: 1,
title: false,
// area: ['66%', '120px'],
area: screen.availWidth > 768 ? ['66%', '120px'] : ['100%', '60px'],
offset: screen.availWidth > 768 ? 'auto' : 'b',
shade: 0.3,

File diff suppressed because one or more lines are too long