arma-server-web-admin/lib/missions.js

100 lines
2.2 KiB
JavaScript
Raw Normal View History

var async = require('async')
2018-08-18 11:04:33 +00:00
var events = require('events')
var filesize = require('filesize')
var fs = require('fs.extra')
var path = require('path')
var SteamWorkshop = require('steam-workshop')
var Missions = function (config) {
this.config = config
2018-08-18 11:04:33 +00:00
this.missions = []
this.steamWorkshop = new SteamWorkshop(this.missionsPath())
2018-08-18 11:04:33 +00:00
this.updateMissions()
}
2018-08-18 11:04:33 +00:00
Missions.prototype = new events.EventEmitter()
Missions.prototype.missionsPath = function () {
return path.join(this.config.path, 'mpmissions')
}
Missions.prototype.missionPath = function (name) {
return path.join(this.missionsPath(), name)
}
2018-08-18 11:04:33 +00:00
Missions.prototype.updateMissions = function (cb) {
var self = this
fs.readdir(this.missionsPath(), function (err, files) {
if (err) {
cb(err)
} else {
async.map(files, function (filename, cb) {
fs.stat(self.missionPath(filename), function (err, stat) {
if (err) {
cb(err)
}
cb(null, {
dateCreated: new Date(stat.ctime),
dateModified: new Date(stat.mtime),
name: filename,
size: stat.size,
sizeFormatted: filesize(stat.size)
})
})
}, function (err, missions) {
2018-08-18 11:04:33 +00:00
if (!err) {
self.missions = missions
self.emit('missions', missions)
}
if (cb) {
cb(err, missions)
}
})
}
})
}
Missions.prototype.handleUpload = function (uploadedFile, cb) {
var filename = decodeURI(uploadedFile.originalname.toLowerCase())
2018-08-18 11:04:33 +00:00
var self = this
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)
}
})
}
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-04-01 09:19:30 +00:00
Missions.prototype.downloadSteamWorkshop = function (id, cb) {
if (!id) {
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
}
module.exports = Missions