toolkit

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: MIT Imports: 9 Imported by: 0

README

Go Toolkit Library

This is a simple proof-of-concept (POC) project demonstrating how to write libraries in Go that can be imported into other projects.

Overview

The library provides a Tools struct with utility functions. Currently, it includes a function to generate a random string of a given length.

Installation

To install this library, run the following command:

go get github.com/caricciy/toolkit

Usage

First, import the toolkit package into your Go file:

import "github.com/caricciy/toolkit"

Instantiate the Tools struct:

tools := toolkit.NewTools()
RandonString

Use the RandomString function to generate a random string of a given length:

randomString := tools.RandomString(10)
fmt.Println(randomString)
UploadFiles

The UploadFiles function is used to handle file uploads. It takes in an http.Request object, a string representing the upload directory, and an optional boolean to determine if the file should be renamed.

t := toolkit.Tools{
		MaxFileSize: 1024 * 1024 * 1024,
		AllowedFileTypes: []string{"image/jpeg", "image/png", "image/gif"},
	}
uploadedFiles, err := t.UploadFiles(request, "/path/to/upload/directory", false)

The UploadFiles function returns an UploadedFile struct used to save information about the uploaded file:

type UploadedFile struct {
    NewFileName      string
    OriginalFileName string
    FileSize         int64
}
CreateDirIfNotExist

Use the CreateDirIfNotExist function to create a directory, and all necessary parents, if it does not exist.

err := tools.CreateDirIfNotExist("/path/to/directory")
if err != nil {
    fmt.Println(err)
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Tools

type Tools struct {
	MaxFileSize      int
	AllowedFileTypes []string
}

Tools is the type used to instantiate the toolkit module.

func NewTools

func NewTools() *Tools

NewTools returns a new Tools instance.

func (*Tools) CreateDirIfNotExist

func (t *Tools) CreateDirIfNotExist(path string) error

CreateDirIfNotExist creates a directory, and all necessary parents, if it does not exist.

func (*Tools) RandomString

func (t *Tools) RandomString(length int) string

RandomString returns a random string of the given length.

func (*Tools) UploadFiles

func (t *Tools) UploadFiles(r *http.Request, uploadDir string, rename ...bool) ([]*UploadedFile, error)

UploadFiles uploads files from a multipart form request to the given directory.

func (*Tools) UploadOneFile

func (t *Tools) UploadOneFile(r *http.Request, uploadDir string, rename ...bool) (*UploadedFile, error)

UploadOneFile uploads a single file from a multipart form request to the given directory.

type UploadedFile

type UploadedFile struct {
	NewFileName      string
	OriginalFileName string
	FileSize         int64
}

UploadedFile is a struct used to save file upload information.

Jump to

Keyboard shortcuts

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