-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathupload.coffee
62 lines (50 loc) · 1.76 KB
/
upload.coffee
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
fs = require "fs"
winston = require "winston"
path = require "path"
util = require "util"
#Stores File Info in json
class Upload
constructor: (config, fileId) ->
@fileId = fileId
@filePath = path.join config.files, fileId
@infoPath = path.resolve "#{@filePath}.json"
@info = null
create: (finalLength) ->
try
fs.closeSync(fs.openSync(@filePath, 'w'))
catch error
winston.error util.inspect error
return {error: [500, "Create Failed"]}
try
info = {finalLength: finalLength, state: "created", createdOn: Date.now(), offset: 0}
fs.writeFileSync @infoPath, JSON.stringify info
@info = info
catch error
winston.error util.inspect error
return {error: [500, "Create Failed - Metadata"]}
return {info: @info}
save: ->
try
fs.writeFileSync @infoPath, JSON.stringify @info
catch error
winston.error util.inspect error
return {error: [500, "Save Failed - Metadata"]}
return {info: @info}
load: ->
return {error: [404, "File Not Found"]} unless fs.existsSync @filePath
try
@info = require @infoPath
catch error
winston.error util.inspect error
return {error: [404, "Not Found - Metadata"]}
#Force Update offset
try
stat = fs.statSync @filePath
@info.offset = stat.size
catch e
winston.error "file error #{fileId} #{util.inspect e}"
return {error: [500, "File Load Error"]}
return {info: @info}
stream: ->
return fs.createReadStream @filePath
exports.Upload = (config, fileId) -> new Upload config, fileId