Skip to content

Instantly share code, notes, and snippets.

@maruware
maruware / convert_jpeg.go
Last active August 7, 2019 05:48
[Go] Convert JPEG
func ConvertToJpeg(src io.Reader, dst io.Writer, quality int) error {
img, _, err := image.Decode(src)
if err != nil {
return err
}
opts := &jpeg.Options{Quality: quality}
err = jpeg.Encode(dst, img, opts)
if err != nil {
return err
@maruware
maruware / base64.go
Last active August 2, 2019 12:16
[Go] Base64 to io.Reader
func DecodeBase64(b64Str string) io.Reader {
r := strings.NewReader(b64Str)
img := base64.NewDecoder(base64.StdEncoding, r)
}
@maruware
maruware / svg_to_png.ts
Created July 23, 2019 09:29
SVG to PNG through Canvas
export const svgToPng = async (svg: SVGSVGElement) => {
const w = svg.getAttribute('width')
const h = svg.getAttribute('height')
if (!w || !h) {
throw new Error('Undefined svg size')
}
const width = parseInt(w)
const height = parseInt(h)
@maruware
maruware / koa-socketio.ts
Last active April 18, 2019 09:23
koa + socket.io
import { BaseContext } from 'koa'
declare module 'koa' {
interface BaseContext {
io: SocketIO.Server
}
}
export default (io: SocketIO.Server) => async (
ctx: BaseContext,
@maruware
maruware / basic-auth-s3-cloudfront-lambda-allinone.yaml
Last active April 17, 2019 11:02
Basic auth ss3+cloudfront
AWSTemplateFormatVersion: '2010-09-09'
Description: Static contents distribution using S3 and CloudFront with basic authentication by lambda@Edge
Parameters:
AuthUser:
Description: ID for basic authentication
Type: String
Default: user2018
AuthPass:
Description: Password for basic authentication
Type: String
@maruware
maruware / Link.tsx
Last active June 14, 2023 02:51
custom Link component with material-ui + react-router-dom on TypeScript
@maruware
maruware / array-util.ts
Created November 20, 2018 04:02
TypeScript: find map value with promise
const findMapPromises = async <T, S> (arr: T[], predicate: (value: T, index: number, obj: T[]) => Promise<S>): Promise<S> => {
for (let i = 0; i < arr.length; i++) {
try {
const r = await predicate(arr[i], i, arr)
return r
} catch (e) {
// next.
}
}
return null
@maruware
maruware / provision.sh
Created August 13, 2018 10:46
Setup SSH with MFA
sudo apt-get update
sudo apt-get install -y libpam-google-authenticator
ga_profile=$(cat << EOS
#!/bin/sh
if [ "$USER" != "root" ]; then
if [ ! -f "$HOME/.google_authenticator" ]; then
trap 'exit' SIGINT
echo "Initialize google-authenticator"
@maruware
maruware / cors.js
Created April 27, 2018 03:11
Attach cors header on AWS Lambda (nodejs8.10)
module.exports = (response) => {
const {headers, ...rest} = response
return {
...rest,
headers: {
'Access-Control-Allow-Origin': '*',
...headers
}
}
}
@maruware
maruware / SpeedDial.jsx
Created February 7, 2018 03:54
Material-UI SpeedDial Example
import React, {Component} from 'react'
import classNames from 'classnames'
import Button from 'material-ui/Button'
import Tooltip from 'material-ui/Tooltip'
import { withStyles } from 'material-ui/styles'
const actionMargin = 20
const actionSize = 40