Skip to content
This repository has been archived by the owner on Nov 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #118 from mesg-foundation/feature/limit-upload-size
Browse files Browse the repository at this point in the history
Limit size of the upload to 10MB
  • Loading branch information
Nicolas Mahé authored Aug 5, 2019
2 parents cf1e9db + e01624d commit 17fe2ec
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/utils/deployer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {existsSync, lstatSync, readdirSync, readFileSync} from 'fs'
import {existsSync, lstatSync, readdirSync, readFileSync, Stats, statSync} from 'fs'
import {fromUrl} from 'hosted-git-info'
import ignore from 'ignore'
import isGitUrl from 'is-git-url'
Expand All @@ -10,6 +10,9 @@ import tar from 'tar'
import {v4 as uuid} from 'uuid'
import {isURL} from 'validator'

const MB = 1024 * 1024
const MAX_UPLOAD_SIZE = MB * 10

const downloadTarball = require('download-tarball')
const gitclone = require('git-clone')

Expand Down Expand Up @@ -48,15 +51,32 @@ const preprocessPath = (path: string): string => {
return path
}

const directorySize = (path: string, accept: (path: string) => boolean): number => {
const fileAndStat = (x: string) => ({file: x, stat: statSync(join(path, x))})
const sizeOf = ({file, stat}: { file: string, stat: Stats }) => stat.isDirectory()
? directorySize(join(path, file), accept)
: stat.size

return readdirSync(path, 'utf8')
.filter(accept)
.map(fileAndStat)
.map(sizeOf)
.reduce((prev, next) => prev + next, 0)
}

export const createTar = (path: string): Readable => {
const mesgignore = join(path, '.mesgignore')
const ig = ignore().add([
'.git',
...(existsSync(mesgignore) ? readFileSync(mesgignore).toString().split('\n') : [])
])
const filter = ig.createFilter()
const dirSize = directorySize(path, filter)
if (dirSize > MAX_UPLOAD_SIZE) throw new Error(`Max size upload ${MAX_UPLOAD_SIZE / MB}MB`)

return tar.create({
cwd: path,
filter: ig.createFilter(),
filter,
strict: true,
gzip: true,
portable: true,
Expand Down

0 comments on commit 17fe2ec

Please sign in to comment.