github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/utils/utils.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"net"
     8  	"net/http"
     9  	"net/url"
    10  	"os"
    11  	"strings"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  )
    15  
    16  func StringArrayIntersection(arr1, arr2 []string) []string {
    17  	arrMap := map[string]bool{}
    18  	result := []string{}
    19  
    20  	for _, value := range arr1 {
    21  		arrMap[value] = true
    22  	}
    23  
    24  	for _, value := range arr2 {
    25  		if arrMap[value] {
    26  			result = append(result, value)
    27  		}
    28  	}
    29  
    30  	return result
    31  }
    32  
    33  func FileExistsInConfigFolder(filename string) bool {
    34  	if len(filename) == 0 {
    35  		return false
    36  	}
    37  
    38  	if _, err := os.Stat(FindConfigFile(filename)); err == nil {
    39  		return true
    40  	}
    41  	return false
    42  }
    43  
    44  func RemoveDuplicatesFromStringArray(arr []string) []string {
    45  	result := make([]string, 0, len(arr))
    46  	seen := make(map[string]bool)
    47  
    48  	for _, item := range arr {
    49  		if !seen[item] {
    50  			result = append(result, item)
    51  			seen[item] = true
    52  		}
    53  	}
    54  
    55  	return result
    56  }
    57  
    58  func GetIpAddress(r *http.Request) string {
    59  	address := ""
    60  
    61  	header := r.Header.Get(model.HEADER_FORWARDED)
    62  	if len(header) > 0 {
    63  		addresses := strings.Fields(header)
    64  		if len(addresses) > 0 {
    65  			address = strings.TrimRight(addresses[0], ",")
    66  		}
    67  	}
    68  
    69  	if len(address) == 0 {
    70  		address = r.Header.Get(model.HEADER_REAL_IP)
    71  	}
    72  
    73  	if len(address) == 0 {
    74  		address, _, _ = net.SplitHostPort(r.RemoteAddr)
    75  	}
    76  
    77  	return address
    78  }
    79  
    80  func GetHostnameFromSiteURL(siteURL string) string {
    81  	u, err := url.Parse(siteURL)
    82  	if err != nil {
    83  		return ""
    84  	}
    85  
    86  	return u.Hostname()
    87  }