增加文件命名方式

This commit is contained in:
邹景立 2021-08-27 10:52:10 +08:00
parent e5a58717ae
commit 75de2fdd7a
4 changed files with 29 additions and 11 deletions

View File

@ -49,6 +49,7 @@ class Upload extends Controller
}
$template = realpath(__DIR__ . '/../../view/api/upload.js');
$data['exts'] = json_encode($data['exts'], JSON_UNESCAPED_UNICODE);
$data['nameType'] = sysconf('storage.name_type') ?: 'xmd5';
return view($template, $data)->contentType('application/x-javascript');
}

View File

@ -106,12 +106,11 @@ define(['md5'], function (SparkMD5, allowMime) {
file.xext = file.name.indexOf('.') > -1 ? file.name.split('.').pop() : 'tmp';
/*! 兼容不能计算文件 HASH 的情况 */
if (!window.FileReader) return jQuery.when((function (date, chars) {
date = new Date(), chars = 'abcdefhijkmnprstwxyz0123456789';
this.xmd5 = '' + date.getFullYear() + (date.getMonth() + 1) + date.getDay() + date.getHours() + date.getMinutes() + date.getSeconds();
while (this.xmd5.length < 32) this.xmd5 += chars.charAt(Math.floor(Math.random() * chars.length));
return setFileXdata(file, this.xmd5), deferred.resolve(file, file.xmd5, file.xkey), deferred;
}).call(this));
var IsDate = '{$nameType|default=""}'.indexOf('date') > -1;
if (!window.FileReader || IsDate) return jQuery.when((function (xmd5, chars) {
while (xmd5.length < 32) xmd5 += chars.charAt(Math.floor(Math.random() * chars.length));
return setFileXdata(file, xmd5, 6), deferred.resolve(file, file.xmd5, file.xkey), deferred;
})(layui.util.toDateString(Date.now(), 'yyyyMMddHHmmss-'), '0123456789'));
/*! 读取文件并计算 HASH 值 */
var spark = new SparkMD5.ArrayBuffer();
@ -119,8 +118,8 @@ define(['md5'], function (SparkMD5, allowMime) {
file.chunkIdx = 0, file.chunkSize = 2097152, file.chunkTotal = Math.ceil(this.size / this.chunkSize);
return jQuery.when(loadNextChunk(file));
function setFileXdata(file, xmd5) {
file.xmd5 = xmd5, file.xkey = file.xmd5.substr(0, 2) + '/' + file.xmd5.substr(2, 30) + '.' + file.xext;
function setFileXdata(file, xmd5, slice) {
file.xmd5 = xmd5, file.xkey = file.xmd5.substr(0, slice || 2) + '/' + file.xmd5.substr(slice || 2, 30) + '.' + file.xext;
return delete file.chunkIdx, delete file.chunkSize, delete file.chunkTotal, file;
}

View File

@ -1,3 +1,21 @@
<div class="layui-form-item">
<label class="layui-form-label label-required">
<span class="color-green font-w7">命名方式</span><br><span class="nowrap color-desc">NameType</span>
</label>
<div class="layui-input-block">
{if !sysconf('storage.name_type')}{php}sysconf('storage.name_type','xmd5');{/php}{/if}
{foreach ['xmd5'=>'文件哈希值','date'=>'日期+随机'] as $k=>$v}
<label class="think-radio notselect">
{if sysconf('storage.name_type') eq $k}
<input checked type="radio" name="storage.name_type" value="{$k}" lay-ignore> {$v}
{else}
<input type="radio" name="storage.name_type" value="{$k}" lay-ignore> {$v}
{/if}
</label>
{/foreach}
<p class="help-block">类型为“文件哈希”时可以实时文件秒传功能,同一个文件只需上传一次节省存储空间,推荐使用。</p>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label label-required">
<span class="color-green font-w7">链接类型</span><br><span class="nowrap color-desc">LinkType</span>
@ -23,6 +41,6 @@
</label>
<div class="layui-input-block">
<input id="storage.allow_exts" type="text" name="storage.allow_exts" required value="{:sysconf('storage.allow_exts')}" placeholder="请输入系统文件上传后缀" class="layui-input">
<p class="help-block">设置系统允许上传文件的后缀多个以英文逗号隔开如png,jpg,rar,doc</p>
<p class="help-block">设置系统允许上传文件的后缀多个以英文逗号隔开如png,jpg,rar,doc,未设置允许上传的后缀</p>
</div>
</div>

View File

@ -50,7 +50,7 @@
</label>
<div class="layui-input-block">
<input id="storage.alioss_bucket" type="text" name="storage.alioss_bucket" required value="{:sysconf('storage.alioss_bucket')}" placeholder="请输入阿里云OSS存储 Bucket (空间名称)" class="layui-input">
<p class="help-block">填写阿里云OSS存储空间名称think-admin-oss</p>
<p class="help-block">填写阿里云OSS存储空间名称think-admin-oss(需要是全区唯一的值,不存在时会自动创建)</p>
</div>
</div>
@ -92,7 +92,7 @@
<button class="layui-btn layui-btn-danger" type='button' data-confirm="确定要取消修改吗?" data-close>取消修改</button>
</div>
<script>layui.form.render()</script>
<script>form.render()</script>
</div>
</form>