Skip to content

Instantly share code, notes, and snippets.

Avatar

Joël Harkes joelharkes

  • NS mobiliteitsdiensten
  • Den Haag, Netherlands
View GitHub Profile
@joelharkes
joelharkes / LaravelUsage.php
Last active Nov 25, 2021
Making streams in PHP/Laravel
View LaravelUsage.php
<?php
public function writeQueryAsCsvFileToStorage() {
$disk = Storage::disk('yourDiskNameHer');
// optional: ensure file doesn't exist:
// $disk->delete($filePath);
$cursor = $this->getQuery()
->toBase() // +-10x speed improvement, skip making laravel models
->cursor() // avoid loading whole result set into memory.
->map(fn ($item) => $this->map((array) $item));
@joelharkes
joelharkes / sftp.php
Created Jun 4, 2020
Upload file in vanilla php
View sftp.php
protected function putfile(string $path, string $content)
{
$host = config('services.sftp.host');
$port = config('services.sftp.port');
$con = ssh2_connect($host, $port, [], ['disconnect' => fn () => Log::debug('sftp disconeccted')]);
if (false === $con) {
throw new HttpException(503, 'sftp connection failed', ['sftpHost' => $host, 'sftpPort' => $port]);
}
if (false === ssh2_auth_password($con, config('services.sftp.username'), config('services.sftp.password'))) {\
ssh2_disconnect($con);
@joelharkes
joelharkes / pgp-keys.ts
Last active Sep 23, 2019
Generate Pgp keys with node
View pgp-keys.ts
import * as openpgp from "openpgp";
import { writeFileSync } from "fs";
import { join } from "path";
var phrase = "test12";
generateRsaKeys('test', phrase);
async function generateRsaKeys(name: string, passphrase: string) {
var options: openpgp.KeyOptions = {
userIds: [{ name: "Test user", email: "test@user.email" }], // multiple user IDs
@joelharkes
joelharkes / CrockfordBase32.php
Created Aug 5, 2019
Crockford base 32 implementation (lower case version)
View CrockfordBase32.php
/**
* Crockford base32 encoding implementation (useable with random_bytes())
* see: https://www.crockford.com/base32.html.
*/
class CrockfordBase32
{
const BITS_5_RIGHT = 31;
const CHARS = '0123456789abcdefghjkmnpqrstvwxyz'; // lower-case
@joelharkes
joelharkes / EventServiceProvider.php
Last active Apr 30, 2021
Sqreen implementation for laravel, SQREEN_ID configurable in environment variable
View EventServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
@joelharkes
joelharkes / MapMap.tsx
Last active Dec 8, 2017
Map function for the Map Class
View MapMap.tsx
// generic map function for the Map class for typescript
function MapMap<TKey, TValue, TReturn>(map: Map<TKey, TValue>, mapper: (key: TKey, value: TValue) => TReturn): TReturn[] {
var t = [] as TReturn[];
map.forEach((val, key) => t.push(mapper(key, val)));
return t;
}
// USAGE EXAMPLE
interface SimpleSelectProps extends BaseFieldProps<string> {
@joelharkes
joelharkes / launch.json
Last active Sep 18, 2022
Debug ts-mocha in vs code (visual studio code)
View launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Run ts-mocha File",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-mocha",
"runtimeArgs": [
"${file}"
@joelharkes
joelharkes / test-with-ts-mocha.js
Created Nov 21, 2017
Run ts-mocha programmatically (in a script)
View test-with-ts-mocha.js
require('ts-mocha');
const Mocha = require('mocha');
const fs = require('fs');
let testSubject = null;
if (process.argv.length === 3) {
testSubject = process.argv[2];
}
const mocha = new Mocha({ timeout: 50000 });
@joelharkes
joelharkes / launch.json
Created Nov 21, 2017
Debug IronPython in visual studio code (vscode)
View launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "IronPython",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "C:\\Program Files (x86)\\IronPython 2.7\\ipy.exe",
"program": "${file}",