Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
quick refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed Sep 6, 2017
1 parent 9dc8c99 commit 5ed444c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

const filesize = require('filesize')

const HTML_PAGE_STYLE = require('./style')
const PathUtil = require('./path')
const mainStyle = require('./style')
const pathUtil = require('../utils/path')

const getParentDirectoryURL = (originalParts) => {
function getParentDirectoryURL (originalParts) {
const parts = originalParts.splice()

if (parts.length > 1) {
Expand All @@ -15,11 +15,11 @@ const getParentDirectoryURL = (originalParts) => {
return [ '', 'ipfs' ].concat(parts).join('/')
}

const buildFilesList = (path, links) => {
function buildFilesList (path, links) {
const rows = links.map((link) => {
let row = [
`<div class="ipfs-icon ipfs-_blank">&nbsp;</div>`,
`<a href="${PathUtil.joinURLParts(path, link.name)}">${link.name}</a>`,
`<a href="${pathUtil.joinURLParts(path, link.name)}">${link.name}</a>`,
filesize(link.size)
]

Expand All @@ -31,9 +31,9 @@ const buildFilesList = (path, links) => {
return rows.join('')
}

const buildTable = (path, links) => {
const parts = PathUtil.splitPath(path)
let parentDirectoryURL = getParentDirectoryURL(parts)
function buildTable (path, links) {
const parts = pathUtil.splitPath(path)
const parentDirectoryURL = getParentDirectoryURL(parts)

return `
<table class="table table-striped">
Expand All @@ -53,14 +53,14 @@ const buildTable = (path, links) => {
`
}

module.exports.build = (path, links) => {
function render (path, links) {
return `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>${path}</title>
<style>${HTML_PAGE_STYLE}</style>
<style>${mainStyle}</style>
</head>
<body>
<div id="header" class="row">
Expand All @@ -81,3 +81,6 @@ module.exports.build = (path, links) => {
</html>
`
}

exports = module.exports
exports.render = render
File renamed without changes.
28 changes: 18 additions & 10 deletions src/http/gateway/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,19 @@ const Unixfs = require('ipfs-unixfs')
const debug = require('debug')
const log = debug('jsipfs:http-gateway:resolver')
log.error = debug('jsipfs:http-gateway:resolver:error')
const html = require('./utils/html')
const PathUtil = require('./utils/path')

const INDEX_HTML_FILES = [ 'index.html', 'index.htm', 'index.shtml' ]
const dirView = require('./dir-view')
const pathUtil = require('./utils/path')

function getIndexFiles (links) {
const INDEX_HTML_FILES = [
'index.html',
'index.htm',
'index.shtml'
]

return links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
}

function noop () {}

Expand All @@ -23,22 +32,20 @@ const resolveDirectory = promisify((ipfs, path, multihash, callback) => {
ipfs.object.get(multihash, { enc: 'base58' }, (err, dagNode) => {
if (err) { return callback(err) }

const links = dagNode.links
const indexFiles = links.filter((link) => INDEX_HTML_FILES.indexOf(link.name) !== -1)
const indexFiles = getIndexFiles(dagNode.links)

// found index file in links
if (indexFiles.length > 0) {
return callback(null, indexFiles)
}

return callback(null, html.build(path, links))
return callback(null, dirView.build(path, dagNode.links))
})
})

const resolveMultihash = promisify((ipfs, path, callback) => {
callback = callback || noop

const parts = PathUtil.splitPath(path)
const parts = pathUtil.splitPath(path)
const partsLength = parts.length

let currentMultihash = parts[0]
Expand All @@ -47,8 +54,8 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
// throws error when invalid CID is passed
try {
currentCid = new CID(mh.fromB58String(currentMultihash))
} catch (e) {
if (e) throw e
} catch (err) {
return next(err)
}

log('currentMultihash: ', currentMultihash)
Expand Down Expand Up @@ -94,6 +101,7 @@ const resolveMultihash = promisify((ipfs, path, callback) => {
})
}, (err) => {
if (err) { return callback(err) }

callback(null, { multihash: currentMultihash })
})
})
Expand Down
3 changes: 2 additions & 1 deletion src/http/gateway/resources/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ const pull = require('pull-stream')
const toPull = require('stream-to-pull-stream')
const fileType = require('file-type')
const mime = require('mime-types')
const Stream = require('readable-stream')

const GatewayResolver = require('../resolver')
const PathUtils = require('../utils/path')
const Stream = require('stream')

module.exports = {
checkCID: (request, reply) => {
Expand Down
32 changes: 21 additions & 11 deletions src/http/gateway/utils/path.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
'use strict'

const splitPath = (path) => {
if (path[path.length - 1] === '/') path = path.substring(0, path.length - 1)
function splitPath (path) {
if (path[path.length - 1] === '/') {
path = path.substring(0, path.length - 1)
}

return path.substring(6).split('/')
}

const removeLeadingSlash = (url) => {
if (url[0] === '/') url = url.substring(1)
function removeLeadingSlash (url) {
if (url[0] === '/') {
url = url.substring(1)
}

return url
}

const removeTrailingSlash = (url) => {
if (url.endsWith('/')) url = url.substring(0, url.length - 1)
function removeTrailingSlash (url) {
if (url.endsWith('/')) {
url = url.substring(0, url.length - 1)
}

return url
}

const removeSlashFromBothEnds = (url) => {
function removeSlashFromBothEnds (url) {
url = removeLeadingSlash(url)
url = removeTrailingSlash(url)

return url
}

const joinURLParts = (...urls) => {
function joinURLParts (...urls) {
urls = urls.filter((url) => url.length > 0)
urls = [ '' ].concat(urls.map((url) => removeSlashFromBothEnds(url)))

return urls.join('/')
}

module.exports = {
splitPath,
removeTrailingSlash,
joinURLParts
splitPath: splitPath,
removeTrailingSlash: removeTrailingSlash,
joinURLParts: joinURLParts
}

0 comments on commit 5ed444c

Please sign in to comment.