Skip to content

Commit

Permalink
First pass at deleting expired files from mongo and filepicker
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Song committed Oct 21, 2013
1 parent 57214fa commit ed1574b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
4 changes: 3 additions & 1 deletion client/views/pages/box.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Template.boxPage.events(
name: ink.filename name: ink.filename
size: ink.size size: ink.size
type: ink.mimetype type: ink.mimetype
inkBlob: ink
Meteor.call 'makeFile', file, (error, id)-> Meteor.call 'makeFile', file, (error, id)->
if error if error
Errors.throw(error.reason) Errors.throw(error.reason)
Expand All @@ -34,7 +35,7 @@ Template.boxPage.rendered = ()->
) )


unless Template.boxPage.dropPaneSet unless Template.boxPage.dropPaneSet
filepicker.setKey(Template.boxPage.filepickerKey) filepicker.setKey(Meteor.filepickerKey)


# create a drop pane # create a drop pane
filepicker.makeDropPane $(".fileDrop")[0], filepicker.makeDropPane $(".fileDrop")[0],
Expand All @@ -60,6 +61,7 @@ Template.boxPage.rendered = ()->
name: ink.filename name: ink.filename
size: ink.size size: ink.size
type: ink.mimetype type: ink.mimetype
inkBlob: ink
Meteor.call 'makeFile', file, (error, id)-> Meteor.call 'makeFile', file, (error, id)->
if error if error
Errors.throw(error.reason) Errors.throw(error.reason)
Expand Down
3 changes: 2 additions & 1 deletion collections/boxes.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Meteor.methods( Meteor.methods(
makeBox: ()-> makeBox: ()->
box = box =
files = [] # current unix timestamp (seconds)
dateCreated : Math.round(new Date().getTime() / 1000)
boxId = Boxes.insert(box) boxId = Boxes.insert(box)
boxId boxId
) )
2 changes: 2 additions & 0 deletions collections/files.coffee
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Meteor.methods( Meteor.methods(
makeFile: (fileDetails)-> makeFile: (fileDetails)->
file = _.pick(fileDetails, 'boxId', 'url','name','size', 'type') file = _.pick(fileDetails, 'boxId', 'url','name','size', 'type')
file.dateCreated = Math.round(new Date().getTime() / 1000)


count = FileCount.findOne() count = FileCount.findOne()
FileCount.update( FileCount.update(
Expand All @@ -12,6 +13,7 @@ Meteor.methods(
$inc: {count: 1} $inc: {count: 1}
} }
) )

fileId = Files.insert(file) fileId = Files.insert(file)
fileId fileId
) )
Expand Down
18 changes: 18 additions & 0 deletions server/crons.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,18 @@
MyCron = new Cron()

MyCron.addJob(1, ()->
console.log "Garbage collecting old files and boxes..."
# find all expired files and boxes and delete them, also remove files from filepicker
# current timestamp
ts = Math.round(new Date().getTime() / 1000)
expiredFiles = Files.find({dateCreated: {$lte: ts - 604800}}).fetch()
expiredBoxes = Boxes.find({dateCreated: {$lte: ts - 604800}}).fetch()
for file in expiredFiles
HTTP.del(file.url+"?key="+Meteor.filepickerKey)
console.log "File " + file.name + " removed from filepicker."

Boxes.remove({dateCreated: {$lte: ts - 604800}})
console.log("Removed " + expiredBoxes.length + " boxes")
Files.remove({dateCreated: {$lte: ts - 604800}})
console.log("Removed " + expiredFiles.length + " files")
)

0 comments on commit ed1574b

Please sign in to comment.