package main

import (
	"io/ioutil"
	"log"
	"net/http"
	"path/filepath"
)

func ServeStorage(w http.ResponseWriter, r *http.Request, path string) {

	path = filepath.Join(config.Http.Storage, path)
	path, err := filepath.Abs(path)
	if err != nil {
		log.Print(err)
		http.Error(w, http.StatusText(400), 400)
		return
	}

	// TODO check if path is still in storage folder

	http.ServeFile(w, r, path)
}

func SaveStorageFile(data *[]byte, path string) error {
	fullpath := filepath.Join(config.Http.Storage, path)
	return ioutil.WriteFile(fullpath, *data, 0644)
}