-
Notifications
You must be signed in to change notification settings - Fork 13
/
folderView.js
117 lines (110 loc) · 4.63 KB
/
folderView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import emojiRegex from 'emoji-regex/RGI_Emoji'
import { getClassNameForMimeType, getClassNameForFilename } from 'font-awesome-filetypes'
import { renderHTML } from './render/htmlWrapper'
import { renderPath } from './render/pathUtil'
import { renderMarkdown } from './render/mdRenderer'
/**
* Convert bytes to human readable file size
*
* @param {Number} bytes File size in bytes
* @param {Boolean} si 1000 - true; 1024 - false
*/
function readableFileSize(bytes, si) {
bytes = parseInt(bytes, 10)
var thresh = si ? 1000 : 1024
if (Math.abs(bytes) < thresh) {
return bytes + ' B'
}
var units = si
? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
var u = -1
do {
bytes /= thresh
++u
} while (Math.abs(bytes) >= thresh && u < units.length - 1)
return bytes.toFixed(1) + ' ' + units[u]
}
/**
* Render Folder Index
*
* @param {*} items
* @param {*} isIndex don't show ".." on index page.
*/
export async function renderFolderView(items, path, request) {
const isIndex = path === '/'
//const str='null'
const el = (tag, attrs, content) => `<${tag} ${attrs.join(' ')}>${content}</${tag}>`
const div = (className, content) => el('div', [`class=${className}`], content)
const item = (icon, fileName, fileAbsoluteUrl, size, emojiIcon) =>
el(
'a',
[`href="${fileAbsoluteUrl}"`, 'class="item"', size ? `size="${size}"` : ''],
(emojiIcon ? el('i', ['style="font-style: normal"'], emojiIcon) : el('i', [`class="${icon}"`], '')) +
fileName +
el('div', ['style="flex-grow: 1;"'], '') +
(fileName === '..' ? '' : el('span', ['class="size"'], readableFileSize(size)))
)
const intro = `<div class="intro markdown-body" style="text-align: left; margin-top: 2rem;">
<h2>AnimeKuro</h2>
<p>This is AnimeKuro Drive.... for more info visit our <a href="https://animekuro.org">website</a> </p>
<p><a href="https://animekuro.org">Download Anime's</a> · <a href="https://anicore.org">Watch Anime Online</a> · <a href="https://dramaheaven.org">Watch Drama Online</a></p>
</div>`
// Check if current directory contains README.md, if true, then render spinner
let readmeExists = false
let readmeFetchUrl = ''
const body = div(
'container',
div('path', renderPath(path)) +
div(
'items',
el(
'div',
['style="min-width: 600px"'],
(!isIndex ? item('far fa-folder', '..', `${path}..`) : '') +
items
.map(i => {
// Check if the current item is a folder or a file
if ('folder' in i && !isIndex) {
let emoji = emojiRegex().exec(i.name)
if (emoji && !emoji.index) {
return item('', i.name.replace(emoji, '').trim(), `${path}${i.name}/`, i.size, emoji[0])
} else {
return item('far fa-folder', i.name, `${path}${i.name}/`, i.size)
}
} else if ('file' in i && !isIndex) {
// Check if README.md exists
if (!readmeExists) {
readmeExists = i.name.toLowerCase() === 'readme.md'
readmeFetchUrl = i['@microsoft.graph.downloadUrl']
}
// Render file icons
let fileIcon = getClassNameForMimeType(i.file.mimeType)
if (fileIcon === 'fa-file') {
// Check for files that haven't been rendered as expected
const extension = i.name.split('.').pop()
if (extension === 'md') {
fileIcon = 'fab fa-markdown'
} else if (['7z', 'rar', 'bz2', 'xz', 'tar', 'wim'].includes(extension)) {
fileIcon = 'far fa-file-archive'
} else if (['flac', 'oga', 'opus'].includes(extension)) {
fileIcon = 'far fa-file-audio'
} else {
fileIcon = `far ${getClassNameForFilename(i.name)}`
}
} else {
fileIcon = `far ${fileIcon}`
}
return item(fileIcon, i.name, `${path}${i.name}`, i.size)
} else {
console.log(`unknown item type ${i}`)
}
})
.join('')
)
) +
(readmeExists && !isIndex ? await renderMarkdown(readmeFetchUrl, 'fade-in-fwd', '') : '') +
(isIndex ? intro : '')
)
return renderHTML(body, ...[request.pLink, request.pIdx])
}