github.com/jlevesy/mattermost-server@v5.3.2-0.20181003190404-7468f35cb0c8+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 StringInSlice(a string, slice []string) bool {
    17  	for _, b := range slice {
    18  		if b == a {
    19  			return true
    20  		}
    21  	}
    22  	return false
    23  }
    24  
    25  func StringArrayIntersection(arr1, arr2 []string) []string {
    26  	arrMap := map[string]bool{}
    27  	result := []string{}
    28  
    29  	for _, value := range arr1 {
    30  		arrMap[value] = true
    31  	}
    32  
    33  	for _, value := range arr2 {
    34  		if arrMap[value] {
    35  			result = append(result, value)
    36  		}
    37  	}
    38  
    39  	return result
    40  }
    41  
    42  func FileExistsInConfigFolder(filename string) bool {
    43  	if len(filename) == 0 {
    44  		return false
    45  	}
    46  
    47  	if _, err := os.Stat(FindConfigFile(filename)); err == nil {
    48  		return true
    49  	}
    50  	return false
    51  }
    52  
    53  func RemoveDuplicatesFromStringArray(arr []string) []string {
    54  	result := make([]string, 0, len(arr))
    55  	seen := make(map[string]bool)
    56  
    57  	for _, item := range arr {
    58  		if !seen[item] {
    59  			result = append(result, item)
    60  			seen[item] = true
    61  		}
    62  	}
    63  
    64  	return result
    65  }
    66  
    67  func GetIpAddress(r *http.Request) string {
    68  	address := ""
    69  
    70  	header := r.Header.Get(model.HEADER_FORWARDED)
    71  	if len(header) > 0 {
    72  		addresses := strings.Fields(header)
    73  		if len(addresses) > 0 {
    74  			address = strings.TrimRight(addresses[0], ",")
    75  		}
    76  	}
    77  
    78  	if len(address) == 0 {
    79  		address = r.Header.Get(model.HEADER_REAL_IP)
    80  	}
    81  
    82  	if len(address) == 0 {
    83  		address, _, _ = net.SplitHostPort(r.RemoteAddr)
    84  	}
    85  
    86  	return address
    87  }
    88  
    89  func GetHostnameFromSiteURL(siteURL string) string {
    90  	u, err := url.Parse(siteURL)
    91  	if err != nil {
    92  		return ""
    93  	}
    94  
    95  	return u.Hostname()
    96  }