2017-08-24 09:15:06 +00:00
|
|
|
var async = require('async')
|
|
|
|
var filesize = require('filesize')
|
|
|
|
var fs = require('fs.extra')
|
|
|
|
var path = require('path')
|
|
|
|
var SteamWorkshop = require('steam-workshop')
|
2016-10-29 14:34:54 +00:00
|
|
|
|
|
|
|
var Missions = function (config) {
|
2017-08-24 09:15:06 +00:00
|
|
|
this.config = config
|
|
|
|
this.steamWorkshop = new SteamWorkshop(this.missionsPath())
|
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Missions.prototype.missionsPath = function () {
|
|
|
|
return path.join(this.config.path, 'mpmissions')
|
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
|
|
|
Missions.prototype.missionPath = function (name) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return path.join(this.missionsPath(), name)
|
2016-10-29 14:34:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
Missions.prototype.list = function (cb) {
|
|
|
|
var self = this
|
2016-10-29 14:34:54 +00:00
|
|
|
fs.readdir(this.missionsPath(), function (err, files) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(err)
|
2016-10-29 14:34:54 +00:00
|
|
|
} else {
|
2016-10-29 15:05:39 +00:00
|
|
|
async.map(files, function (filename, cb) {
|
|
|
|
fs.stat(self.missionPath(filename), function (err, stat) {
|
|
|
|
if (err) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(err)
|
2016-10-29 15:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cb(null, {
|
|
|
|
dateCreated: new Date(stat.ctime),
|
|
|
|
dateModified: new Date(stat.mtime),
|
|
|
|
name: filename,
|
|
|
|
size: stat.size,
|
2017-08-24 09:15:06 +00:00
|
|
|
sizeFormatted: filesize(stat.size)
|
|
|
|
})
|
|
|
|
})
|
2016-10-29 15:05:39 +00:00
|
|
|
}, function (err, missions) {
|
|
|
|
if (cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
cb(err, missions)
|
2016-10-29 15:05:39 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
2016-10-29 14:34:54 +00:00
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
|
|
|
Missions.prototype.handleUpload = function (uploadedFile, cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
var filename = decodeURI(uploadedFile.originalname.toLowerCase())
|
|
|
|
fs.move(uploadedFile.path, path.join(this.missionsPath(), filename), function (err) {
|
|
|
|
cb(err)
|
|
|
|
})
|
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
|
|
|
Missions.prototype.delete = function (missionName, cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
fs.unlink(path.join(this.missionsPath(), missionName), cb)
|
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
2017-04-01 09:19:30 +00:00
|
|
|
Missions.prototype.downloadSteamWorkshop = function (id, cb) {
|
|
|
|
if (!id) {
|
2017-08-24 09:15:06 +00:00
|
|
|
return cb(new Error('Not a valid Steam Workshop ID: ' + id))
|
2017-04-01 09:19:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
this.steamWorkshop.downloadFile(id, cb)
|
2017-04-01 09:19:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
module.exports = Missions
|