Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kevinkindom
Created March 11, 2016 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinkindom/cefd860c1d01332b5282 to your computer and use it in GitHub Desktop.
Save kevinkindom/cefd860c1d01332b5282 to your computer and use it in GitHub Desktop.
随机文件名算法

随机文件名长度算法

通过当前时间可计算出随机的长度文件算法,结合先前的文件名md5算法得出:

00 - 04 区间:使用目前16位MD5

05 - 16 区间:使用32位md5,例目前是05分,则从md5字符串第0个字符截取到第五个字符,共5个字符做文件名

17 - 59 区间:使用当前分钟数和16求余,得到的值再匹配上述两条规则即可,例如

例1:目前是17分,和16求余是1(相当于01分),匹配到了00-04的区间,则使用16位长度的文件名

例2:目前是38分,和16求余是6(相当于06分),匹配到了05-16的区间,则使用5位长度的文件名

下以javascript代码举例:

// md5 为32位,minutes为当前时间的分钟
var getFilename = function(md5, minutes){
    var minutes = parseInt(minutes);
    if(minutes <= 4){
        return md5.slice(0, 16);
    }else if(minutes <= 16){
        return md5.slice(0, minutes);
    }else{
        return getFilename(md5, minutes % 16);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment