2017-08-24 09:15:06 +00:00
|
|
|
var async = require('async')
|
2018-08-18 11:04:33 +00:00
|
|
|
var events = require('events')
|
2017-08-24 09:15:06 +00:00
|
|
|
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
|
2018-08-18 11:04:33 +00:00
|
|
|
this.missions = []
|
2017-08-24 09:15:06 +00:00
|
|
|
this.steamWorkshop = new SteamWorkshop(this.missionsPath())
|
2018-08-18 11:04:33 +00:00
|
|
|
|
|
|
|
this.updateMissions()
|
2017-08-24 09:15:06 +00:00
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
2018-08-18 11:04:33 +00:00
|
|
|
Missions.prototype = new events.EventEmitter()
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-18 11:04:33 +00:00
|
|
|
Missions.prototype.updateMissions = function (cb) {
|
2017-08-24 09:15:06 +00:00
|
|
|
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) {
|
2018-08-18 11:04:33 +00:00
|
|
|
if (!err) {
|
|
|
|
self.missions = missions
|
|
|
|
self.emit('missions', missions)
|
|
|
|
}
|
|
|
|
|
2016-10-29 15:05:39 +00:00
|
|
|
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())
|
2018-08-18 11:04:33 +00:00
|
|
|
var self = this
|
2017-08-24 09:15:06 +00:00
|
|
|
fs.move(uploadedFile.path, path.join(this.missionsPath(), filename), function (err) {
|
2018-08-18 11:04:33 +00:00
|
|
|
self.updateMissions()
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb(err)
|
|
|
|
}
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
|
|
|
}
|
2016-10-29 14:34:54 +00:00
|
|
|
|
|
|
|
Missions.prototype.delete = function (missionName, cb) {
|
2018-08-18 11:04:33 +00:00
|
|
|
var self = this
|
|
|
|
fs.unlink(path.join(this.missionsPath(), missionName), function (err) {
|
|
|
|
self.updateMissions()
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
})
|
2017-08-24 09:15:06 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-08-18 11:04:33 +00:00
|
|
|
var self = this
|
|
|
|
|
|
|
|
this.steamWorkshop.downloadFile(id, function (err) {
|
|
|
|
self.updateMissions()
|
|
|
|
|
|
|
|
if (cb) {
|
|
|
|
cb(err)
|
|
|
|
}
|
|
|
|
})
|
2017-04-01 09:19:30 +00:00
|
|
|
}
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
module.exports = Missions
|