nix, shell, perl, php, mysql and mac os x tips and tricks

Monday, July 31, 2017

Ember.js stateful image loading component

Not sure how useful this is, but this uses javascript Image() and a promise to load an image. Allows you to set loading and error states.
import Ember from 'ember';

const {
    Component,
    get,
    set
} = Ember;

export default Component.extend({

    isLoading: true,
    isError: false,

    /* JavaScript Image Object used to do the loading */
    imageLoader: Ember.computed(function() {
        return new Image();
    },

    getImage(url, _this) {
        return new Ember.RSVP.Promise((resolve, reject) => {
            let img = _this.get('imageLoader');
            img.onload = function() {
                resolve(url)
            }
            img.onerror = function() {
                reject(url)
            }
            img.src = url
        });
    },

    didInsertElement() {
        this._super(...arguments);
        let _this = this;

        let src = get(_this, 'src');
        _this.get('getImage')(src, _this).then(success => {
            console.log("success", success);
            set(_this, 'isLoading', false);
        }).catch(error => {
            console.log("fail", error);
            set(_this, 'isLoading', false);
            set(_this, 'isError', true);
        })
    },

    willDestroyElement() {
        this._super(...arguments);
        let img = get(this, 'imageLoader');
        if (img) {
            img = img.onload = img.onerror = null;
            this.set('imageLoader', null);
        }
    }
});
Then in the template you could do something like:
{{#if isLoading}}
 
loading...
{{else}} {{#if isError}} {{else}} {{/if}} {{/if}}
If the component is called "product-render" you could use it in parent template like:
{{product-render src=myImage}}

MySQL select records with timestamp in the previous day, no matter what time of day it is now

WHERE timestamp >= DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 24 HOUR) AND timestamp < DATE_ADD(DATE_SUB(CONCAT(CURDATE(), ' 00:00:00'), INTERVAL 24 HOUR), INTERVAL 24 HOUR)

Sunday, July 2, 2017

Create Self-Signed Cert on OSX and tell the OS to trust it

Not as easy as you might think. Let's say you wanted to create a self-signed cert for a local domain called "my.webtool"... First create a file called v3.ext with these contents:
authorityKeyIdentifier=keyid,issuer
basicConstraints=CA:FALSE
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

[alt_names]
DNS.1 = my.webtool
DNS.2 = localhost
DNS.3 = 127.0.0.1
Then run these commands. This assumes you have openSSL installed:
openssl genrsa -des3 -passout pass:x -out my.webtool.pass.key 2048
openssl rsa -passin pass:x -in my.webtool.pass.key -out my.webtool.key
rm my.webtool.pass.key
openssl req -new -key my.webtool.key -out my.webtool.csr
openssl x509 -req -days 1000 -in my.webtool.csr -signkey my.webtool.key -out my.webtool.crt -extfile v3.ext
Then install the .key and .crt files in whatever server you're running. THEN you have to tell your system to trust the certificate by importing it into your keychain AND change the "trust" settings on it. See http://www.accuweaver.com/2014/09/19/make-chrome-accept-a-self-signed-certificate-on-osx/ update! this appears to be a one-shot deal:
openssl req \
-newkey rsa:2048 \
-x509 \
-nodes \
-keyout yarr.mydev.com.key \
-new \
-out yarr.mydev.com.crt \
-subj /CN=yarr.mydev.com \
-reqexts SAN \
-extensions SAN \
-config <(cat /etc/ssl/openssl.cnf \
    <(printf '[SAN]\nsubjectAltName=DNS:yarr.mydev.com,IP:192.168.56.101')) \
-sha256 \
-days 3650