Skip to content
Browse files

First pass at deleting expired files from mongo and filepicker

  • Loading branch information...
1 parent 57214fa commit ed1574bfc1c433b47f5d49edeacf5a26cb498643 @oliversong committed Oct 20, 2013
Showing with 25 additions and 2 deletions.
  1. +3 −1 client/views/pages/box.coffee
  2. +2 −1 collections/boxes.coffee
  3. +2 −0 collections/files.coffee
  4. +18 −0 server/crons.coffee
View
4 client/views/pages/box.coffee
@@ -19,6 +19,7 @@ Template.boxPage.events(
name: ink.filename
size: ink.size
type: ink.mimetype
+ inkBlob: ink
Meteor.call 'makeFile', file, (error, id)->
if error
Errors.throw(error.reason)
@@ -34,7 +35,7 @@ Template.boxPage.rendered = ()->
)
unless Template.boxPage.dropPaneSet
- filepicker.setKey(Template.boxPage.filepickerKey)
+ filepicker.setKey(Meteor.filepickerKey)
# create a drop pane
filepicker.makeDropPane $(".fileDrop")[0],
@@ -60,6 +61,7 @@ Template.boxPage.rendered = ()->
name: ink.filename
size: ink.size
type: ink.mimetype
+ inkBlob: ink
Meteor.call 'makeFile', file, (error, id)->
if error
Errors.throw(error.reason)
View
3 collections/boxes.coffee
@@ -3,7 +3,8 @@
Meteor.methods(
makeBox: ()->
box =
- files = []
+ # current unix timestamp (seconds)
+ dateCreated : Math.round(new Date().getTime() / 1000)
boxId = Boxes.insert(box)
boxId
)
View
2 collections/files.coffee
@@ -3,6 +3,7 @@
Meteor.methods(
makeFile: (fileDetails)->
file = _.pick(fileDetails, 'boxId', 'url','name','size', 'type')
+ file.dateCreated = Math.round(new Date().getTime() / 1000)
count = FileCount.findOne()
FileCount.update(
@@ -12,6 +13,7 @@ Meteor.methods(
$inc: {count: 1}
}
)
+
fileId = Files.insert(file)
fileId
)
View
18 server/crons.coffee
@@ -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.
Something went wrong with that request. Please try again.