uploadhandler

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2019 License: GPL-3.0 Imports: 12 Imported by: 0

README

UploadHandler

GoDoc view examples

Purpose

Sometimes a web-server application needs a way to accept file uploads from the remote users. This package does just this: it accepts uploads (up to a certain size) and stores them in a configurable directory.

Installation

You can use Go to install this package for you:

go get -u github.com/mwat56/go-uploadhandler

Usage

The main function to call is

// Wrap returns a handler function that includes error page handling,
// wrapping the given `aHandler` and calling it internally.
//
func Wrap(aHandler http.Handler,
    aDestDir, aFieldName, anUpURL, aNextURL string,
    aMaxSize int64, aPager errorhandler.TErrorPager) http.Handler {…}

While at first glance the number of arguments seems to be overwhelming they allow you to fully configure the package's behaviour when an uploaded file arrives. Let's look at the arguments one by one:

  • aHandler is the handler function which you're already using for your web-server. It will continue to work as it used to before except that a certain URL (configred by anUpURL, see below) will be intercepted if something gets POSTed to it.
  • aDestDir is the directory where the incoming file is finally stored after processing it.
  • aFieldName is the name/ID of the form/field your web-page uses to accept the file-upload.
  • anUpURL is the URL your web-page's FORM element POSTs its data to. This URL will be intercepted (if it's accessed by the POST HTTP method) and its data will be proccessed.
  • aNextURL is the URL the user get's forwarded to after the file upload was successfully processed.
  • aMaxSize defines the max. accepted size of uploaded files. Files bigger than that value will be rejected. Think carefully about which size will suit your actual needs.
  • aPager is an optional provider of error message pages (or nil if not needed). – See github.com/mwat56/go-errorhandler for details about that package.

Here is a very simple example using this package:

func testHandler(aWriter http.ResponseWriter, aRequest *http.Request) {
    // the upload form to show
    page := `<!DOCTYPE html><html><head><title>Go Upload</title></head><body>
    <form action="/up" method="post" enctype="multipart/form-data">
    <p><label for="uploadFile">Filename:</label>
    <input type="file" name="uploadFile" id="uploadFile"></p>
    <p><input type="submit" name="submit" value="Submit"></p>
    </form></body></html>`

    // send it to the remote user:
    aWriter.WriteHeader(200)
    aWriter.Write([]byte(page))
    // POST is handled by the UploadHandler
} // testHandler()

func main() {
    // let the upload handler wrap our own page handler:
    handler := uploadhandler.Wrap(http.HandlerFunc(testHandler),
        "./static", "uploadFile", "up", "/", 10*1024*1024, nil)

    if err := http.ListenAndServe("127.0.0.1:8080", handler); nil != err {
        log.Fatalf("%s Problem: %v", os.Args[0], err)
    }
} // main()

You'll probably save the required values for e.g. aDestDir and aMaxSize in some kind of config-file, reading them at start of your web-server, and passing them along to the final Wrap() call instead of hard-coding them like in the example above. And the values of anUpURL and aFieldName must, obviously, correspond with those you're actually using in your own application. If you don't use customised error pages you can pass nil for the aPager argument as done in the example.

So, to add the file-upload functionality to your web-server application all that's needed is a single Wrap() function call. That's it.

Licence

    Copyright © 2019 M.Watermann, 10247 Berlin, Germany
                    All rights reserved
                EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the GNU General Public License for details.

Documentation

Overview

Package uploadhandler implements a simple handler for HTTP file uploads.

Copyright © 2019 M.Watermann, 10247 Berlin, Germany
                All rights reserved
            EMail : <support@mwat.de>

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

You should have received a copy of the GNU General Public License along with this program. If not, see the [GNU General Public License](http://www.gnu.org/licenses/gpl.html) for details.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Wrap

func Wrap(aHandler http.Handler,
	aDestDir, aFieldName, anUpURL, aNextURL string,
	aMaxSize int64, aPager errorhandler.TErrorPager) http.Handler

Wrap returns a handler function that includes error page handling, wrapping the given `aHandler` and calling it internally.

`aHandler` the previous handler responding to the HTTP request.

`aDestDir` is the directory to place the uploaded files.

`aFieldName` the name/ID of the form/input holding the uploaded file.

`anUpURL` the URL uploads are POSTed to.

`aNextURL` the URL to redirect the user after a asuccessful upload.

`aMaxSize` the max. accepted size of uploaded files.

`aPager` optional provider of error message pages (or `nil` if not needed).

Types

type TUploadHandler

type TUploadHandler struct {
	// contains filtered or unexported fields
}

TUploadHandler embeds a `TErrorPager` which provides error page handling.

func NewHandler

func NewHandler(aDestDir, aFieldName, anUpURL, aNextURL string,
	aMaxSize int64, aPager errorhandler.TErrorPager) *TUploadHandler

NewHandler returns a new `tUploadHandler` instance.

`aDestDir` is the directory to place the uploaded files.

`aFieldName` the name/ID of the form/input holding the uploaded file.

`anUpURL` the URL uploads are POSTed to.

`aNextURL` the URL to redirect the user after a asuccessful upload.

`aMaxSize` the max. accepted size of uploaded files.

`aPager` optional provider of error message pages (or `nil` if not needed).

func (TUploadHandler) ServeHTTP

func (uh TUploadHandler) ServeHTTP(aWriter http.ResponseWriter,
	aRequest *http.Request) (string, int)

ServeHTTP handles the incoming file upload.

`aWriter` writes the response to the remote user.

`aRequest` is the incoming upload request.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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