2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
|
2016-05-27 08:35:55 -07:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package utils
|
|
|
|
|
|
2016-07-20 17:01:10 -04:00
|
|
|
import (
|
2017-01-13 13:53:37 -05:00
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2017-05-09 15:34:30 +02:00
|
|
|
"net/url"
|
2016-07-20 17:01:10 -04:00
|
|
|
"os"
|
2017-01-13 13:53:37 -05:00
|
|
|
|
|
|
|
|
"github.com/mattermost/platform/model"
|
2016-07-20 17:01:10 -04:00
|
|
|
)
|
|
|
|
|
|
2016-05-27 08:35:55 -07:00
|
|
|
func StringArrayIntersection(arr1, arr2 []string) []string {
|
|
|
|
|
arrMap := map[string]bool{}
|
|
|
|
|
result := []string{}
|
|
|
|
|
|
|
|
|
|
for _, value := range arr1 {
|
|
|
|
|
arrMap[value] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, value := range arr2 {
|
|
|
|
|
if arrMap[value] {
|
|
|
|
|
result = append(result, value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
2016-07-20 17:01:10 -04:00
|
|
|
|
|
|
|
|
func FileExistsInConfigFolder(filename string) bool {
|
|
|
|
|
if len(filename) == 0 {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(FindConfigFile(filename)); err == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2016-09-30 11:06:30 -04:00
|
|
|
|
|
|
|
|
func RemoveDuplicatesFromStringArray(arr []string) []string {
|
|
|
|
|
result := make([]string, 0, len(arr))
|
|
|
|
|
seen := make(map[string]bool)
|
|
|
|
|
|
|
|
|
|
for _, item := range arr {
|
|
|
|
|
if !seen[item] {
|
|
|
|
|
result = append(result, item)
|
|
|
|
|
seen[item] = true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
}
|
2017-01-13 13:53:37 -05:00
|
|
|
|
|
|
|
|
func GetIpAddress(r *http.Request) string {
|
|
|
|
|
address := r.Header.Get(model.HEADER_FORWARDED)
|
|
|
|
|
|
|
|
|
|
if len(address) == 0 {
|
|
|
|
|
address = r.Header.Get(model.HEADER_REAL_IP)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(address) == 0 {
|
|
|
|
|
address, _, _ = net.SplitHostPort(r.RemoteAddr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return address
|
|
|
|
|
}
|
2017-05-09 15:34:30 +02:00
|
|
|
|
2017-05-19 16:57:36 -04:00
|
|
|
func GetHostnameFromSiteURL(siteURL string) string {
|
2017-05-09 15:34:30 +02:00
|
|
|
u, err := url.Parse(siteURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2017-05-17 17:30:21 +02:00
|
|
|
|
|
|
|
|
return u.Hostname()
|
2017-05-09 15:34:30 +02:00
|
|
|
}
|