Skip to content

Instantly share code, notes, and snippets.

View darrenmothersele's full-sized avatar
🏠
Working from home

Darren Mothersele darrenmothersele

🏠
Working from home
View GitHub Profile
const getUsage = async uid => {
const [ files ] = await bucket.getFiles({ prefix: uid });
if (!files || !files.length) {
return 0;
}
const getFileSize = file => +file.metadata.size;
const sum = (a, b) => a + b;
return files.map(getFileSize).reduce(sum, 0);
};
export const getQuotaToken = functions.https.onCall(async ({ id }, context) => {
if (!context.auth) {
throw new functions.https
.HttpsError('failed-precondition', 'The function must be called while authenticated.');
}
const { uid } = context.auth;
const usage = await getUsage(uid);
const quota = context.auth.token.quota || DEFAULT_QUOTA;
const remaining = quota - usage;
@darrenmothersele
darrenmothersele / do-file-upload.ts
Last active December 19, 2018 18:31
Per-User File Upload Quotas with Angular and Firebase Custom Auth Tokens
doFileUpload(uid, id, file) {
return new Observable(subscriber => {
const task = this.storage.upload(`${uid}/${id}`, file);
task.snapshotChanges()
.pipe(
finalize(() => {
subscriber.next({ done: true, id });
subscriber.complete();
})
)
@darrenmothersele
darrenmothersele / docker.sh
Created December 2, 2018 11:49
Serving Angular CLI project over HTTPS / generate SSL cert / configure Angular CLI / optionally use NGINX via Docker
#!/bin/bash
docker run --name fc \
-v $PWD/ssl:/etc/nginx/certs \
-v $PWD/ssl/nginx-default.conf:/etc/nginx/conf.d/default.conf:ro \
-v $PWD/dist/___PROJECT_NAME___:/usr/share/nginx/html:ro -p 8080:443 -d nginx
@darrenmothersele
darrenmothersele / drop-zone-directive.ts
Last active July 12, 2024 13:05
Angular Drag and Drop File Directive
import { Directive, EventEmitter, HostBinding, HostListener, Input, Output } from '@angular/core';
// Angular Drag and Drop File
//
// Add this directive to an element to turn it into a dropzone
// for drag and drop of files.
// Example:
//
// <div (appDropZone)="onDrop($event)"></div>
//
@darrenmothersele
darrenmothersele / youtube-player.service.ts
Last active February 19, 2018 16:21
Angular 2, 4, 5+ Service for controlling a YouTube audio player
import { Injectable, NgZone } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { interval } from 'rxjs/observable/interval';
import { combineLatest, filter, map, merge, startWith, switchMap, takeUntil } from 'rxjs/operators';
import { Observable } from 'rxjs/Observable';
declare const YT: any;
export enum YouTubePlayerState {
Unstarted = -1,
@darrenmothersele
darrenmothersele / duration.pipe.ts
Last active January 9, 2022 09:10
Duration Pipe for Angular 2, 4, 5 - converts ISO 8601 duration (eg, "PT1H30M5S") to 01:30:05
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'duration'
})
export class DurationPipe implements PipeTransform {
transform(value: string): string {
const leftPad = x => String(x).length >= 2 ? x : leftPad(`0${x}`);
const [ _, hours, mins, secs ] = value.match(/PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/);
return [hours || 0, mins || 0, secs || 0].map(leftPad).join(':');
get paths() {
return _.map(this.bands, (v, x) => {
return `M ${x} 128 L ${x} ${128 - (v / 2)}`;
});
}
get w() {
const points = _.map(this.wavePoints, (v, x) => `L ${x} ${128 - (v / 2)}`);
return 'M 0 64 ' + points.join(' ');
}
<svg class="spectrum">
<g *ngIf="!!bands">
<path fill="none" stroke="lightgrey" *ngFor="let p of paths" [attr.d]="p"></path>
</g>
<g *ngIf="!!wavePoints">
<path fill="none" stroke="black" stroke-opacity="0.75" [attr.d]="w"></path>
</g>
</svg>
<br>
<lab-slider label="Slider #1" [min]="0" [max]="10" [(value)]="value"></lab-slider>