s3browser

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2023 License: MIT Imports: 21 Imported by: 6

README

Caddy s3browser

Example config

dl.example.com {
	s3browser {
		key ADDKEYHERE
		secret ADDSECRETHERE
		bucket ADDBUCKETHERE
		endpoint nyc3.digitaloceanspaces.com
		region us-west-2
		secure true
		refresh 5m
		debug false
	}
	proxy / https://examplebucket.nyc3.digitaloceanspaces.com {
		header_upstream Host examplebucket.nyc3.digitaloceanspaces.com
	}
}

This will provide directory listing for an S3 bucket (you are able to use minio, or other S3 providers). To serve files via Caddy as well you'll need to use the proxy directive as well. The server must be able to have public access to the files in the bucket.

Note: For performance reasons, the file listing is fetched once every 5minutes to reduce load on S3 (or S3 equivalent).

Prior Art

Documentation

Index

Constants

View Source
const (
	AssetsPrefix    = `/---caddy-s3browser---/`
	MIMEOctetStream = "application/octet-stream"
)

Variables

View Source
var DefaultTemplate = func(c Config) string {
	dependencies := selfEmbed
	if len(c.CSSCDN) > 0 {
		if cdn, ok := Dependencies[c.CSSCDN]; ok {
			dependencies = cdn
		} else {
			dependencies = DepCSS(strings.Split(c.CSSCDN, ","))
		}
	}
	return `<!DOCTYPE html>
<html>
	<head>
		<title>{{ .ReadableName }} | S3 Browser</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		` + dependencies.String() + `
		<style>
			body {
				cursor: default;
			}
			.navbar {
				margin-bottom: 20px;
			}
			.credits {
				padding-left: 15px;
				padding-right: 15px;
			}
			.readme-doc > h1 {
				margin-top: 0;
			}
			.readme-doc img{
				max-width: 100%;
			}
			th .glyphicon {
				font-size: 15px;
			}
			table .icon {
				width: 30px;
			}
		</style>
    <!-- template source from https://raw.githubusercontent.com/dockhippie/caddy/master/rootfs/etc/caddy/browse.tmpl -->
	</head>
	<body>
		<nav class="navbar navbar-inverse navbar-static-top">
			<div class="container-fluid">
				<div class="navbar-header">
					<a class="navbar-brand" href="/">
						S3 Browser
					</a>
				</div>

				<div class="navbar-text navbar-right hidden-xs credits">
					Powered by <a href="https://github.com/admpub/nging">Nging</a>
				</div>
			</div>
		</nav>

		<div class="container-fluid">
			<ol class="breadcrumb">
				<li>
					<a href="/"><span class="glyphicon glyphicon-home" aria-hidden="true"></span></a>
				</li>
				{{- range .Breadcrumbs -}}
					<li>
						<a href="/{{ html .Link }}">
							{{ html .ReadableName }}
						</a>
					</li>
				{{- end -}}
			</ol>

			<div class="panel panel-default">
				<table class="table table-hover table-striped">
					<thead>
						<tr>
							<th class="icon"></th>
							<th class="name">
								Name
							</th>
							<th class="size col-sm-2">
								Size
							</th>
							<th class="modified col-sm-2">
								Modified
							</th>
						</tr>
					</thead>

					<tbody>
						{{- if ne .Path "/" -}}
							<tr>
								<td>
									<span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
								</td>
								<td>
									<a href="..">
										Go up
									</a>
								</td>
								<td>
									&mdash;
								</td>
								<td>
									&mdash;
								</td>
							</tr>
						{{- end -}}
						{{- range .Folders -}}
							<tr>
								<td class="icon">
									<span class="glyphicon glyphicon-folder-close" aria-hidden="true"></span>
								</td>
								<td class="name">
									<a href="{{ html .Name }}">
										{{- .ReadableName -}}
									</a>
								</td>
								<td class="size">
									&mdash;
								</td>
								<td class="modified">
									&mdash;
								</td>
							</tr>
						{{- end -}}
						{{- range .Files -}}
							{{- if ne .Name "" -}}
							<tr>
								<td class="icon">
									<span class="glyphicon glyphicon-file" aria-hidden="true"></span>
								</td>
								<td class="name">
									<a href="{{ $.CDNURL }}{{ .Folder }}{{ html .Name }}">
										{{- .Name -}}
									</a>
								</td>
								<td class="size">
									{{- .HumanSize -}}
								</td>
								<td class="modified">
									{{- .HumanModTime "01/02/2006 03:04:05 PM" -}}
								</td>
							</tr>
							{{- end -}}
						{{- end -}}
					</tbody>
				</table>
			</div>
			{{- if .README -}}
			<div class="panel panel-default phl pvl readme-doc">
			{{- .RenderREADME -}}
			</div>
			{{- end -}}
		</div>
	</body>
</html>
`
}
View Source
var (
	Dependencies = map[string]DepCSS{
		`cloudflare`: cloudflare,
		`bootcdn`:    bootCDN,
		``:           selfEmbed,
	}
)

Functions

func BoolArg

func BoolArg(c *caddy.Controller) (bool, error)

func ContentTypeByExtension added in v0.1.2

func ContentTypeByExtension(name string) (t string)

ContentTypeByExtension returns the MIME type associated with the file based on its extension. It returns `application/octet-stream` incase MIME type is not found.

func StringArg

func StringArg(c *caddy.Controller) (string, error)

StringArg Assert only one arg and return it

Types

type Browse

type Browse struct {
	Next     httpserver.Handler
	Config   Config
	Client   *minio.Client
	Fs       map[string]Directory
	Template *template.Template
}

func (Browse) ServeHTTP

func (b Browse) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error)

type Config

type Config struct {
	Key      string
	Bucket   string
	Secret   string
	Region   string
	Prefix   string
	CDNURL   string
	Endpoint string
	Secure   bool
	Refresh  string
	Debug    bool
	CSSCDN   string
	// contains filtered or unexported fields
}

type DepCSS

type DepCSS []string

func (DepCSS) String

func (d DepCSS) String() string

type Directory

type Directory struct {
	Path    string
	Folders []Folder
	Files   []File
	README  string
}

func (Directory) Breadcrumbs

func (d Directory) Breadcrumbs() []Node

func (Directory) ReadableName

func (d Directory) ReadableName() string

func (Directory) RenderREADME added in v0.0.2

func (d Directory) RenderREADME() template.HTML

type File

type File struct {
	Folder string
	Bytes  int64
	Name   string
	Date   time.Time
}

func (File) HumanModTime

func (f File) HumanModTime(format string) string

HumanModTime returns the modified time of the file as a human-readable string.

func (File) HumanSize

func (f File) HumanSize() string

HumanSize returns the size of the file as a human-readable string in IEC format (i.e. power of 2 or base 1024).

type Folder

type Folder struct {
	Name string
}

func (Folder) ReadableName

func (f Folder) ReadableName() string

type Node

type Node struct {
	Link         string
	ReadableName string
}

type TmplData

type TmplData struct {
	CDNURL string
	Directory
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL