blob: 55bcf784ad5dd19c29ef7ce1c581a737b12dbe41 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package utils
import (
"io/ioutil"
"net/http"
"strings"
)
// agnostic util reader to fetch content from web or local path or potentially other places.
type ContentReader struct {
URLReader
LocalReader
}
type URLReader struct {
}
func (urlReader *URLReader) ReadUrl(url string) (content []byte, err error) {
resp, err := http.Get(url)
Check(err)
b, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
Check(err)
return b, nil
}
type LocalReader struct {
}
func (localReader *LocalReader) ReadLocal(path string) (content []byte, err error) {
cont, err := ioutil.ReadFile(path)
Check(err)
return cont, nil
}
func Read(url string) (content []byte, err error) {
if strings.HasPrefix(url, "http") {
return new(ContentReader).URLReader.ReadUrl(url)
} else {
return new(ContentReader).LocalReader.ReadLocal(url)
}
}