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

Limit size of the upload to 10MB #118

Merged
merged 4 commits into from
Aug 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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