Skip to content

Instantly share code, notes, and snippets.

@sda0
sda0 / request2curl.go
Created June 20, 2024 13:35
golang request to curl
fmt.Println(request2curl(req))
func request2curl(req *http.Request) string {
escaped := func(origin string) string {
return `'` + strings.ReplaceAll(origin, `'`, `'\''`) + `'`
}
resource := req.URL.String()
scheme := req.URL.Scheme
if scheme == "" {
@sda0
sda0 / bitrix_personal_photo.php
Last active June 2, 2024 12:53
bitrix удаление одинакового аватара (PERSONAL_PHOTO)
<?php
$res = Bitrix\Main\UserTable::getList(Array(
"select"=>Array("ID", "PERSONAL_PHOTO"),
"filter"=>Array("!PERSONAL_PHOTO" => 0),
));
$avatar = $other = 0;
while ($arUser = $res->fetch()) {
$fileID = (int) $arUser['PERSONAL_PHOTO'];
$arFile = CFile::GetByID($fileID)->Fetch();
@sda0
sda0 / ilike_to_regexp.go
Created January 25, 2022 10:45
convert ILIKE to regexp pattern on golang
// IsMatchedName implements case-insensitive search string value for pattern.
// Pattern supports % and _ wildcards. Escape symbol is \.
func IsMatchedName(pattern, value string) bool {
if len(pattern) == 0 {
return true
}
re := likeToRegexp(pattern)
match, err := regexp.MatchString(strings.ToLower(re), strings.ToLower(value))
if err != nil {
panic(err)
@sda0
sda0 / postgres show indexes on table.sql
Created March 13, 2020 10:23
how to see index details for some table
SELECT
tablename,
indexname,
indexdef
FROM
pg_indexes
WHERE
schemaname = 'public' AND
tablename = 'file_name_attribute_fact'
ORDER BY
type MultipartForm struct {
Body bytes.Buffer
ContentType string
}
type multipartFormParams struct {
File io.Reader
FieldName string
FileName string
Extra map[string]string
@sda0
sda0 / hateoas.go
Last active October 15, 2023 20:39
var urlInjector = &URLInjector{
Rules: map[string]URLGeneratorFunc{
entityUUIDKey: observableEntityURL,
},
}
// URLGeneratorFunc returns URL of entity/artifact/object/task...
// BaseURL doesn't contain an ending slash, so remember to add leading slash at the begin of an URI.
type URLGeneratorFunc func(baseURL string, uuidStr []byte) string
// RealProto is a helper used to determine protocol used on client's side of proxy.
// Ensure the proxy configured correct before use this func in prod env.
func RealProto(r *http.Request) string {
proto := r.Header.Get("X-Forwarded-Proto")
if proto == "" {
if r.TLS == nil {
proto = "http"
} else {
proto = "https"
@sda0
sda0 / file.go
Created September 26, 2018 07:29
uniq slice example
func uniq(source []uuid.UUID) []uuid.UUID {
m := make(map[string]bool)
res := make([]uuid.UUID, 0, len(source))
for i := range source {
if _, ok := m[source[i].String()]; ok {
continue
}
m[source[i].String()] = true
res = append(res, source[i])
}
@sda0
sda0 / email.go
Created July 17, 2018 10:33
golang email vaildation and canonization
var hostFmt = regexp.MustCompile("^[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
func canonize(addr string) (string, error) {
addr = strings.ToLower(strings.Trim(addr, " "))
components := strings.Split(addr, "@")
username, host := components[0], components[1]
if len(addr) > 254 || username == "" || !hostFmt.MatchString(host) {
return "", ErrInvalidEmailAddr
}
@sda0
sda0 / phpversion.sh
Created December 25, 2016 15:12 — forked from vjandrea/phpversion.sh
Change php cli version in plesk
#!/bin/sh
printf "Switching php to version 5.6 with symlinks.\n"
printf "Press any key to continue...\n"
read CONTINUE
printf "Creating backups..."
# Backup original binaries