Categorygithub.com/5HT2/srp-go
modulepackage
0.0.0-20240320070857-a4b22abf751a
Repository: https://github.com/5ht2/srp-go.git
Documentation: pkg.go.dev

# README

srp-go

time tracker Docker Pulls Docker Build CodeFactor

Serve a random image on <main page> with a dynamic background color, let users upload on /upload and browse a gallery on /browse. Has (optional) image resizing and compression.

Contributions are welcome and appreciated.

Contributing

To build:

git clone [email protected]:5HT2/srp-go.git
cd srp-go
make

To run:

./srp-go -h # for a full list of parameters (none required)
./srp-go -addr=localhost:6060 -debug=true

I recommend deleting sample.jpg after you have uploaded a few pictures. The sample file is there to prevent issues when first testing (needing an image to serve on /, to generate a sample color for, etc).

You can also add your own favicon.ico inside the config/ directory, and it will overwrite the default icon.

OAuth

In order to set up OAuth, follow these instructions to create a GitHub OAuth app.

You will want to

  • Set LIVE_URL to the accessible url of your site in (.env)
    • Eg: https://frog.pics
    • Or: http://localhost:6060 for a testing environment
  • Set the Callback URL to your LIVE_URL + /api/auth/callback
  • Enable "Request user authorization (OAuth)"
  • You do not need to enable permissions for anything

Create a .env file inside your config folder, with the following format:

OAUTH_CLIENT_ID=Iv1.some_client_id
OAUTH_CLIENT_SECRET=your_client_secret
LIVE_URL=http://localhost:6060
BROWSE_PAGE_COLOR= # optional color code to use for the /browse page
WEBHOOK_URL= # optional discord webhook url for posting specific events

API

The full list of accessible API endpoints can be found inside api.go.

The /api/all endpoint will give you a list of all available images, in the below json format.

The /api/random endpoint will return the properties of a randomly-selected image in json (by default), like so:

{
    "image_name": "sample.jpg",
    "image_url": "http://localhost:6060/images/sample.jpg",
    "median_color": "868232"
}

You can also add ?format=css to get the css version if you really want:

body {
    background-color: #868232;
}

div.img {
    content: url('/images/sample.jpg');
}

TODO:

  • Add authentication for uploading
  • Add Discord embeds
  • Switch to a different prominent color library, because the current one doesn't support webp
    • Switch to webp for saving images
  • Finalize webhook support
  • Image attribution support
  • Sign In button broke

License

This project is licensed under ISC.

The sample.jpg file is licensed under Creative Commons Attribution-Share Alike, you can find the original file here.

# Functions

AppendIfMissing will append a string to slice if the slice does not already have that string.
No description provided by the author
ConvertAndCompress will convert the image to jpg if it's non-transparent, and compress if it meets the requirements for being compressed.
GetAllImages TODO: Maybe we could also cache the creation of ImageData? GetAllImages returns a slice of all images in the format of ImageData.
GetCachedContent returns content found inside the appropriate cache.
No description provided by the author
No description provided by the author
GetNewImageSize will calculate a new ImageSize with a ratio as similar as possible to the original with the longest side set to *maxImgLength.
GetNewImageType will take path and convert the image to a png.
GetRandomImage TODO: Maybe we could also cache the creation of ImageData? As it is used for each request GetRandomImage chooses a random image name from the image cache.
GetUser will get a User with the provided state.
No description provided by the author
HandleCachedFavicon will return the favicon bytes to the client.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ImageHandler is our own RequestHandler with a CacheDuration of 0.
InsertUser will insert a new User, or overwrite an existing user with a matching id.
LoadAllCaches will read all the files in dir and return the map of path:content.
LoadFaviconCache tries to load config/favicon.ico and defaults to www/ico/favicon.ico.
LoadGalleryCache will format the list of imageCache into a list of gallery html for each image we have.
LoadImage returns an image object for path.
LoadImageCache opens the www/images directory and caches a list of FileInfos.
MainImageColor calculates the median color of cropped image.
No description provided by the author
No description provided by the author
No description provided by the author
SaveFinal will compress and convert the image from path inside the config/tmp/ folder, and save the final result inside the config/images/ folder.
ToInt will return an int from a float rounded to the nearest whole.
UpdateUserWhitelist will update the whitelisted status of a User matching id.

# Structs

ImageData TODO: image attribution and author.
No description provided by the author