Skip to content

Instantly share code, notes, and snippets.

View wjkoh's full-sized avatar
🎯
Focusing

Woojong Koh wjkoh

🎯
Focusing
View GitHub Profile
@wjkoh
wjkoh / truncate_pdf.go
Last active June 26, 2024 16:42
Go: How to split or truncate a large PDF file with QPDF
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
)
package main
import(
"regexp"
)
var(
pageRe *regexp.Regexp = regexp.MustCompile(`\bp.\s*(\d+)\b`)
pagesRe *regexp.Regexp = regexp.MustCompile(`\bpp.\s*(\d+)-(\d+)\b`)
)
@wjkoh
wjkoh / golang-tls-handshake-failure.md
Last active May 30, 2024 05:02
Go: remote error: tls: handshake failure

Have you encountered the following error while using Go's net/http package?

Get "https://host-with-tls-problem.com": remote error: tls: handshake failure

Here is a solution that works for me.

  1. Install goTLSScan:
$ go install github.com/jbardin/gotlsscan@latest
@wjkoh
wjkoh / form.html
Created May 4, 2024 14:20
Alpine.js + Pico CSS: Displaying a Loading Indicator on the HTML Form Submit Button
<div x-data="{ inProgress: false }">
<form x-on:submit="inProgress = true" ...>
...
<button type="submit" x-bind:aria-busy="inProgress" x-text="inProgress ? 'Please wait...' : 'Generate'"></button>
</form>
</div>
@wjkoh
wjkoh / post_file_as_base64.sh
Last active April 22, 2024 02:37
cURL: How to convert a file to a Base64 encoded string and send it as JSON to HTTP
#!/usr/bin/env bash
BASE64=$(base64 -i yann-lecun_resize.jpg)
curl --request POST \
--url http://localhost:8080/v1/images \
--header 'Content-Type: application/json' \
--data '{ "image": "'${BASE64}'" }'
@wjkoh
wjkoh / main.go
Last active January 30, 2024 06:43
Google Cloud: Get Cloud Run URL in Go
// $ gcloud run deploy --source .
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strings"
@wjkoh
wjkoh / future.dart
Last active January 18, 2024 09:31
Dart/Flutter: StatefulWidget + TextEditingController + FutureBuilder
class EditCaptionView extends StatefulWidget {
final String postId;
const EditCaptionView({super.key, required this.postId});
@override
State<EditCaptionView> createState() => _EditCaptionViewState();
}
class _EditCaptionViewState extends State<EditCaptionView> {
final _controller = TextEditingController();
@wjkoh
wjkoh / fnv1a.dart
Last active January 9, 2024 08:49
Dart/Flutter: FNV-1a 64-bit hash function (Web compatible)
import 'dart:convert';
final fnv1A64Init = BigInt.parse("0xcbf29ce484222325");
final fnv1A64Prime = BigInt.from(0x100000001b3);
final uint64Mask = BigInt.parse("0xffffffffffffffff");
// FNV-1a 64-bit hash function (JavaScript compatible)
// This function is for you if you have seen errors like "Error: The integer literal 0xcbf29ce484222325 can't be represented exactly in JavaScript."
String fnv1A64(String string) {
var hash = fnv1A64Init;
@wjkoh
wjkoh / hsv.go
Last active November 3, 2023 07:08
Go: HSV to RGB
package bake
import (
"errors"
"image/color"
"math"
)
// https://en.wikipedia.org/wiki/HSL_and_HSV#Converting_to_RGB
func HSVToRGBA(h float64, s float64, v float64) (*color.RGBA, error) {
@wjkoh
wjkoh / blur.go
Last active February 1, 2024 10:52
Go: Box blur
package bake
import (
"errors"
"image"
"image/draw"
)
// Try radius: 8 and numPasses: 2 with
// https://cdn.pixabay.com/photo/2015/09/21/14/24/zombie-949916_960_720.jpg