Beispiel #1
0
// If redirect is nil, the redirect will go to "/"
func LogoutView(redirect view.URL) view.View {
	return view.RenderView(
		func(context *view.Context, writer *utils.XMLWriter) (err error) {
			Logout(context)
			if redirect != nil {
				return view.Redirect(redirect.URL(context.PathArgs...))
			}
			return view.Redirect("/")
		},
	)
}
Beispiel #2
0
// If redirect is nil, the redirect will go to "/"
func LogoutView(redirect view.URL) view.View {
	return view.RenderView(
		func(ctx *view.Context) (err error) {
			Logout(ctx.Session)
			if redirect != nil {
				return view.Redirect(redirect.URL(ctx))
			}
			return view.Redirect("/")
		},
	)
}
Beispiel #3
0
// Uses https://github.com/valums/file-uploader
func RequireUploadBlobButtonScript(parentSelector, dropZoneSelector, listSelector, onComplete string) view.View {
	return view.RenderView(
		func(ctx *view.Context) error {
			script := uploadBlobButtonScript(ctx, parentSelector, dropZoneSelector, listSelector, onComplete)
			ctx.Response.RequireScript(script, 20)
			if !Config.NoDynamicStyleAndScript {
				ctx.Response.RequireStyleURL("/media/fileuploader.css", 0)
				ctx.Response.RequireScriptURL("/media/fileuploader.js", 0)
			}
			return nil
		},
	)
}
Beispiel #4
0
// Uses https://github.com/valums/file-uploader
func UploadImageButton(parentSelector, dropZoneSelector, listSelector string, thumbnailSize int, onComplete string) view.View {
	extraDropzones := "[]"
	if dropZoneSelector != "" {
		dropZoneSelector = fmt.Sprintf(`[jQuery("%s")[0]]`, dropZoneSelector)
	}
	listElement := "null"
	if listSelector != "" {
		listElement = fmt.Sprintf(`jQuery("%s")[0]`, listSelector)
	}
	if onComplete == "" {
		onComplete = "function(){}"
	}

	uploaderScript := view.RenderView(
		func(ctx *view.Context) error {
			uploadURL := UploadImage.URL(ctx.ForURLArgsConvert(thumbnailSize))
			script := fmt.Sprintf(
				`jQuery(function() {
					var uploader = new qq.FileUploader({
						debug: true,
					    element: jQuery("%s")[0],
					    extraDropzones: %s,
					    listElement: %s,
					    action: "%s",
					    allowedExtensions: ["png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff"],
					    acceptFiles: ["image/png", "image/jpeg", "image/gif", "image/bmp", "image/tiff"],
					    sizeLimit: 1024*1024*64,
					    multiple: false,
					    onComplete: %s
					});
				});`,
				parentSelector,
				extraDropzones,
				listElement,
				uploadURL,
				onComplete,
			)
			ctx.Response.RequireScript(script, 20)
			if !Config.NoDynamicStyleAndScript {
				ctx.Response.RequireStyleURL("/media/fileuploader.css", 0)
				ctx.Response.RequireScriptURL("/media/fileuploader.js", 0)
			}
			return nil
		},
	)
	return uploaderScript
}
Beispiel #5
0
// Uses https://github.com/valums/file-uploader
func UploadImageButton(thumbnailSize int, onComplete string) view.View {
	if onComplete == "" {
		onComplete = "function(){}"
	}

	const registerUploaderScript = `jQuery(function() {
		var uploader = new qq.FileUploader({
			debug: true,
		    element: jQuery("#%s")[0],
		    action: "%s",
		    allowedExtensions: ["png", "jpg", "jpeg", "gif", "bmp", "tif", "tiff"],
		    acceptFiles: ["image/png", "image/jpeg", "image/gif", "image/bmp", "image/tiff"],
		    multiple: false,
		    onComplete: %s
		});
	});`

	uploader := view.DIV("uploader")
	uploaderID := uploader.ID()
	uploaderScript := view.RenderView(
		func(ctx *view.Context) error {
			uploadURL := UploadImage.URL(ctx.ForURLArgsConvert(thumbnailSize))
			script := fmt.Sprintf(registerUploaderScript, uploaderID, uploadURL, onComplete)
			ctx.Response.RequireScript(script, 20)
			return nil
		},
	)
	return view.Views{
		&view.If{
			Condition: !Config.NoDynamicStyleAndScript,
			Content: view.Views{
				view.RequireStyleURL("/media/fileuploader.css", 0),
				view.RequireScriptURL("/media/fileuploader.js", 0),
				// view.RequireScriptURL("/media/media.js", 10),
			},
		},
		uploader,
		uploaderScript,
	}
}
Beispiel #6
0
	"github.com/ungerik/go-start/utils"
	"github.com/ungerik/go-start/view"
	"io"
)

var View view.ViewWithURL = view.NewViewURLWrapper(view.RenderView(
	func(context *view.Context, writer *utils.XMLWriter) error {
		reader, contentType, err := Config.Backend.ImageVersionReader(context.PathArgs[0])
		if err != nil {
			if _, ok := err.(ErrInvalidImageID); ok {
				return view.NotFound(context.PathArgs[0] + "/" + context.PathArgs[1] + " not found")
			}
			return err
		}
		_, err = io.Copy(writer, reader)
		if err != nil {
			return err
		}
		err = reader.Close()
		if err != nil {
			return err
		}
		context.Header().Set("Content-Type", contentType)
		return nil
	},
))

func ViewPath(name string) view.ViewPath {
	return view.ViewPath{Name: name, Args: 2, View: View}
}
Beispiel #7
0
	"io"

	"github.com/ungerik/go-start/view"
)

var View = view.NewViewURLWrapper(view.RenderView(
	func(ctx *view.Context) error {
		reader, contentType, err := Config.Backend.ImageVersionReader(ctx.URLArgs[0])
		if err != nil {
			if _, ok := err.(ErrInvalidImageID); ok {
				return view.NotFound(ctx.URLArgs[0] + "/" + ctx.URLArgs[1] + " not found")
			}
			return err
		}
		_, err = io.Copy(ctx.Response, reader)
		if err != nil {
			return err
		}
		err = reader.Close()
		if err != nil {
			return err
		}
		ctx.Response.Header().Set("Content-Type", contentType)
		return nil
	},
))

func ViewPath(name string) view.ViewPath {
	return view.ViewPath{Name: name, Args: 2, View: View}
}
Beispiel #8
0
package media

import (
	"io"

	"github.com/ungerik/go-start/config"
	"github.com/ungerik/go-start/view"
)

var FileView = view.NewViewURLWrapper(view.RenderView(
	func(ctx *view.Context) error {
		reader, _, contentType, err := Config.Backend.FileReader(ctx.URLArgs[0])
		if err != nil {
			if _, ok := err.(ErrNotFound); ok {
				err = view.NotFound(ctx.URLArgs[0] + "/" + ctx.URLArgs[1] + " not found")
				config.Logger.Println("FileView:", err)
				return err
			}
			return err
		}
		defer reader.Close()
		_, err = io.Copy(ctx.Response, reader)
		if err != nil {
			config.Logger.Println("FileView:", err)
			return err
		}
		ctx.Response.Header().Set("Content-Type", contentType)
		return nil
	},
))