Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Mostly complete ES2015+ rebuild #41

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const config = {
"wwwPort": 3000,
"dbHost": "127.0.0.1",
"dbPort": 27017,
"dbName": "floorplan",
"mountPoint": "/"
}

export default config;
2 changes: 0 additions & 2 deletions index.js

This file was deleted.

9 changes: 9 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// From https://thewebdev.info/2022/02/27/how-to-use-__dirname-in-node-js-when-using-es6-modules/
import { dirname } from 'path';
import { chdir } from 'process';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));

// Load server
chdir(__dirname);
import server from './lib/server.mjs';
40 changes: 21 additions & 19 deletions lib/database.js → lib/database.mjs
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
var config = require('../config');
var logger = console;
var mongo = require('mongodb');
var ObjectID = mongo.ObjectID;
var Q = require('q');
import config from '../config.mjs';
const logger = console;
import mongo from 'mongodb';
const { ObjectID } = mongo;
import Q from 'q';

var dbOptions = {
const dbOptions = {
journal: true,
numberOfRetries: Number.POSITIVE_INFINITY
};

var serverOptions = {
const serverOptions = {
auto_reconnect: true
};

var server = new mongo.Server(config.dbHost, config.dbPort, serverOptions);
server.allServerInstances().forEach(function(serverInstance){
const server = new mongo.Server(config.dbHost, config.dbPort, serverOptions);
server.allServerInstances().forEach(serverInstance => {
serverInstance.dbInstances = serverInstance.dbInstances || [];
});

var db = module.exports = new mongo.Db(config.dbName, server, dbOptions);
const db = new mongo.Db(config.dbName, server, dbOptions);

var dbPromise;
export default db;

module.exports.OID = function(objectIdOrHexString){
let dbPromise;

export const OID = function(objectIdOrHexString){
if(!objectIdOrHexString){
return null;
} else if(objectIdOrHexString instanceof ObjectID){
return objectIdOrHexString;
} else {
return new ObjectID(objectIdOrHexString);
}
}
};

module.exports.connect = function(){
export const connect = function(){
return Q.ninvoke(db, "open")
.then(onConnect)
.fail(function(err){
.fail(err => {
logger.error(err.message);
});
};

module.exports.shutdown = function(){
export const shutdown = function(){
return dbPromise
.then(function(){
var deferred = Q.defer();
.then(() => {
const deferred = Q.defer();
db.close(deferred.makeNodeResolver());
return deferred.promise;
})
.finally(function(){
.finally(() => {
logger.log("Shut down.");
});
};
Expand Down
62 changes: 0 additions & 62 deletions lib/personRepository.js

This file was deleted.

59 changes: 59 additions & 0 deletions lib/personRepository.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import db from './database.mjs';
import Q from 'q';
const { OID } = db;

const peopleCollection = db.collection('people');
const deletedPeopleCollection = db.collection('people_deleted');

const find = exports.find = function(query){
let cursor;

return Q.ninvoke(peopleCollection, 'find', query)
.then(_cursor => {
cursor = _cursor;
return Q.ninvoke(cursor, 'toArray');
})
.finally(() => {
cursor && cursor.close();
});
};

export const findAll = function(){
return find({});
};

/**
* @param id The object ID to search for. Can be a String or an ObjectID instance
* @returns the result object, or null if no result was found
*/
export const findOne = function(id){
return Q.ninvoke(peopleCollection, 'findOne', { _id: OID(id) })
};

export const findByOffice = function(office){
return find({ office });
};

/*
* Currently performs full updates, not deltas. If we want PATCH support on the API, we'll need to
* use $set to avoid deleting the attributes omitted from the delta.
*/
export const save = function(attrs){
const id = OID(attrs._id);
delete attrs._id;
return Q.ninvoke(peopleCollection, 'update', { _id: id }, attrs, { upsert: true })
.then(report => {
const id2 = id || report[1].upserted;
return findOne(id2);
});
};

export const remove = function(id){
let person;
return findOne(id)
.then(person_ => {
person = person_;
return Q.ninvoke(deletedPeopleCollection, 'insert', person);
})
.then(() => Q.ninvoke(peopleCollection, 'remove', person));
};
73 changes: 31 additions & 42 deletions lib/photoManager.js → lib/photoManager.mjs
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
var _ = require('lodash');
var fs = require('fs');
var gm = require('gm');
var path = require('path');
var Q = require('q');
var verror = require('verror');

var PHOTO_DIR = './data/photos/';
var MAX_PHOTO_EDGE_LENGTH = 618;
var QUALITY = 75; //0 to 100 (best)

exports.importPhoto = function(originPath, destinationFilename){
import extend from 'lodash-es';
import fs from 'fs';
import gm from 'gm';
import path from 'path';
import Q from 'q';
import verror from 'verror';

const PHOTO_DIR = './data/photos/';
const MAX_PHOTO_EDGE_LENGTH = 618;
const QUALITY = 75; //0 to 100 (best)

export const importPhoto = function(originPath, destinationFilename){
return getImageInfo(originPath)
.then(function(imageInfo){
if(isProcessingRequired(imageInfo)){
return processImage(originPath, imageInfo)
.then(function(processedImage){
return saveProcessedImage(processedImage, destinationFilename);
});
} else {
return moveUnprocessedImage(originPath, destinationFilename);
}
})
.then(function(){
return {
path: getDestinationPath(destinationFilename)
};
})
.fail(function(err){
.then(imageInfo => isProcessingRequired(imageInfo) ? processImage(originPath, imageInfo)
.then(processedImage => saveProcessedImage(processedImage, destinationFilename)) : moveUnprocessedImage(originPath, destinationFilename))
.then(() => ({
path: getDestinationPath(destinationFilename)
}))
.fail(err => {
throw new verror.VError(err, "unable to import photo");
});
};
Expand All @@ -51,17 +41,17 @@ Orientation=1
* JSON metadata.
*/
function getImageInfo(originPath){
var image = gm(originPath);
const image = gm(originPath);
return Q.all([
Q.ninvoke(image, 'identify', '{ "width": %w, "height": %h, "format": "%m" }'),
Q.ninvoke(image, 'identify', '%[EXIF:Orientation]')
])
.spread(function(rawInfo, orientation){
var result;
.spread((rawInfo, orientation) => {
let result;
try {
result = JSON.parse(rawInfo);
} catch(err){
throw new verror.VError(err, "gm.identify returned invalid JSON: "+rawInfo);
throw new verror.VError(err, `gm.identify returned invalid JSON: ${rawInfo}`);
}
result.orientation = parseInt(orientation, 10) || undefined;
return result;
Expand Down Expand Up @@ -94,7 +84,7 @@ function isStandardOrientation(imageInfo){
}

function processImage(originPath, imageInfo){
var image = gm(originPath);
const image = gm(originPath);

// Apply orientation from flag
if(!isStandardOrientation(imageInfo)){
Expand All @@ -103,7 +93,7 @@ function processImage(originPath, imageInfo){
//We can't call size() to read these, because gm is a builder that queues all changes to be applied later.
//size() would just return the stale original data because we haven't written yet.
if(imageInfo.orientation >= 5){
_.extend(imageInfo, {
extend(imageInfo, {
width: imageInfo.height,
height: imageInfo.width
});
Expand All @@ -114,16 +104,15 @@ function processImage(originPath, imageInfo){

// Crop to center square
if(!isSquare(imageInfo)){
var inWidth = imageInfo.width;
var inHeight = imageInfo.height;
var outWidth = Math.min(inWidth, inHeight);
var outHeight = outWidth;
var halfDimensionDifference = Math.abs((inWidth-inHeight)/2);
var x = (inWidth > inHeight) ? halfDimensionDifference : 0;
var y = (inHeight > inWidth) ? halfDimensionDifference : 0;
const inWidth = imageInfo.width;
const inHeight = imageInfo.height;
const outWidth = Math.min(inWidth, inHeight);
const halfDimensionDifference = Math.abs((inWidth-inHeight)/2);
const x = (inWidth > inHeight) ? halfDimensionDifference : 0;
const y = (inHeight > inWidth) ? halfDimensionDifference : 0;

imageInfo.width = imageInfo.height = outWidth;
image.crop(outWidth, outHeight, x, y);
image.crop(outWidth, outWidth, x, y);
}

// Resize to <= max size
Expand Down
Loading