api: Auto-generate title if none is passed on upload
This commit is contained in:
parent
7ab7e4918e
commit
c53796f9b6
4 changed files with 69 additions and 9 deletions
5
api/package-lock.json
generated
5
api/package-lock.json
generated
|
@ -5831,6 +5831,11 @@
|
|||
"integrity": "sha512-G2Lj61tXDnVFFOi8VZds+SoQjtQC3dgokKdDG2mTm1tx4m50NUHBOZSBwQQHyy0V12A0JTG4icfZQH+xPyh8VA==",
|
||||
"dev": true
|
||||
},
|
||||
"luxon": {
|
||||
"version": "1.26.0",
|
||||
"resolved": "https://registry.npmjs.org/luxon/-/luxon-1.26.0.tgz",
|
||||
"integrity": "sha512-+V5QIQ5f6CDXQpWNICELwjwuHdqeJM1UenlZWx5ujcRMc9venvluCjFb4t5NYLhb6IhkbMVOxzVuOqkgMxee2A=="
|
||||
},
|
||||
"make-dir": {
|
||||
"version": "3.1.0",
|
||||
"resolved": "https://registry.npmjs.org/make-dir/-/make-dir-3.1.0.tgz",
|
||||
|
|
|
@ -34,6 +34,7 @@
|
|||
"jest": "^26.6.3",
|
||||
"joi": "^17.4.0",
|
||||
"jsonwebtoken": "8.5.1",
|
||||
"luxon": "^1.26.0",
|
||||
"method-override": "3.0.0",
|
||||
"methods": "1.1.2",
|
||||
"mongoose": "^5.11.17",
|
||||
|
|
|
@ -1,15 +1,17 @@
|
|||
const mongoose = require('mongoose');
|
||||
const _ = require('lodash');
|
||||
const uniqueValidator = require('mongoose-unique-validator');
|
||||
const { DateTime } = require('luxon');
|
||||
const slug = require('slug');
|
||||
const path = require('path');
|
||||
const sanitize = require('sanitize-filename');
|
||||
const fs = require('fs')
|
||||
const fs = require('fs');
|
||||
|
||||
const { parseTrackPoints } = require('../logic/tracks');
|
||||
|
||||
const TrackData = require('./TrackData');
|
||||
|
||||
const DATA_DIR = process.env.DATA_DIR || path.resolve(__dirname, '../../data/')
|
||||
const DATA_DIR = process.env.DATA_DIR || path.resolve(__dirname, '../../data/');
|
||||
|
||||
const schema = new mongoose.Schema(
|
||||
{
|
||||
|
@ -57,6 +59,29 @@ schema.pre('validate', async function (next) {
|
|||
}
|
||||
});
|
||||
|
||||
// 0..4 Night, 4..10 Morning, 10..14 Noon, 14..18 Afternoon, 18..22 Evening, 22..00 Night
|
||||
// Two hour intervals
|
||||
const DAYTIMES = [
|
||||
'Night',
|
||||
'Night',
|
||||
'Morning',
|
||||
'Morning',
|
||||
'Morning',
|
||||
'Noon',
|
||||
'Noon',
|
||||
'Afternoon',
|
||||
'Afternoon',
|
||||
'Afternoon',
|
||||
'Evening',
|
||||
'Evening',
|
||||
'Evening',
|
||||
'Night',
|
||||
];
|
||||
|
||||
function getDaytime(dateTime) {
|
||||
return DAYTIMES[Math.floor((dateTime.hour % 24) / 2)];
|
||||
}
|
||||
|
||||
class Track extends mongoose.Model {
|
||||
slugify() {
|
||||
this.slug = slug(this.title || 'track') + '-' + ((Math.random() * Math.pow(36, 6)) | 0).toString(36);
|
||||
|
@ -89,20 +114,20 @@ class Track extends mongoose.Model {
|
|||
|
||||
async _ensureDirectoryExists() {
|
||||
if (!this.originalFilePath) {
|
||||
await this.generateOriginalFilePath()
|
||||
await this.generateOriginalFilePath();
|
||||
}
|
||||
|
||||
const dir = path.join(DATA_DIR, path.dirname(this.originalFilePath))
|
||||
await fs.promises.mkdir(dir, {recursive: true})
|
||||
const dir = path.join(DATA_DIR, path.dirname(this.originalFilePath));
|
||||
await fs.promises.mkdir(dir, { recursive: true });
|
||||
}
|
||||
|
||||
get fullOriginalFilePath() {
|
||||
return path.join(DATA_DIR, this.originalFilePath)
|
||||
return path.join(DATA_DIR, this.originalFilePath);
|
||||
}
|
||||
|
||||
async writeToOriginalFile(fileBody) {
|
||||
await this._ensureDirectoryExists()
|
||||
await fs.promises.writeFile(this.fullOriginalFilePath, fileBody)
|
||||
await this._ensureDirectoryExists();
|
||||
await fs.promises.writeFile(this.fullOriginalFilePath, fileBody);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -126,7 +151,7 @@ class Track extends mongoose.Model {
|
|||
|
||||
// Parse the points from the body.
|
||||
// TODO: Stream file contents, if possible
|
||||
const body = await fs.promises.readFile(this.fullOriginalFilePath)
|
||||
const body = await fs.promises.readFile(this.fullOriginalFilePath);
|
||||
const points = Array.from(parseTrackPoints(body));
|
||||
|
||||
const trackData = TrackData.createFromPoints(points);
|
||||
|
@ -142,6 +167,31 @@ class Track extends mongoose.Model {
|
|||
await this.save();
|
||||
}
|
||||
|
||||
async autoGenerateTitle() {
|
||||
if (this.title) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.originalFileName) {
|
||||
this.title = _.upperFirst(_.words(this.originalFileName.replace(/(\.obsdata)?\.csv$/, '')).join(' '));
|
||||
}
|
||||
|
||||
for (const property of ['publicTrackData', 'trackData']) {
|
||||
if (!this.title && this[property]) {
|
||||
await this.populate(property).execPopulate();
|
||||
if (this[property].recordedAt) {
|
||||
const dateTime = DateTime.fromJSDate(this[property].recordedAt);
|
||||
const daytime = getDaytime(dateTime);
|
||||
this.title = `${daytime} ride on ${dateTime.toLocaleString(DateTime.DATE_MED)}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (this.title) {
|
||||
await this.save();
|
||||
}
|
||||
}
|
||||
|
||||
toJSONFor(user) {
|
||||
const includePrivateFields = user && user._id.equals(this.author._id);
|
||||
|
||||
|
|
|
@ -199,6 +199,8 @@ router.post(
|
|||
await track.save()
|
||||
}
|
||||
|
||||
await track.autoGenerateTitle()
|
||||
|
||||
// console.log(track.author);
|
||||
return res.json({ track: track.toJSONFor(req.user) });
|
||||
}),
|
||||
|
@ -255,6 +257,8 @@ router.put(
|
|||
await track.save();
|
||||
}
|
||||
|
||||
await track.autoGenerateTitle()
|
||||
|
||||
return res.json({ track: track.toJSONFor(req.user) });
|
||||
}),
|
||||
);
|
||||
|
|
Loading…
Add table
Reference in a new issue