This application provides an HTTP endpoint to save multiple images from given URLs into a specified directory structure. It is built using Go and the Gin web framework.
The service exposes a POST /save-images
endpoint. You can send a JSON payload containing an array of image information objects. Each object specifies an image source URL and a desired folder path relative to the dist
directory. The application will then download each image and save it into the appropriate folder.
By default, the application listens on port 8080
.
- Concurrent Downloads: The service processes up to 25 images concurrently for faster handling.
- Customizable File Structure: Each request can target different folders and image names.
- Automatic Directory Creation: If a specified folder does not exist, it will be created automatically within the
dist
directory. - Graceful Error Handling: Errors are logged, but the endpoint will continue processing other images.
- Go 1.18+ (or later)
- A working internet connection (to download images from the provided URLs)
- Network permissions to run on the specified port (default:
8080
)
-
Clone the repository:
git clone <REPO_URL>
-
Navigate to the project directory:
cd <project-directory>
-
Download dependencies:
go mod tidy
-
Build the application:
go build -o image-downloader
Once built, simply run the executable:
./image-downloader
This will start the server on http://localhost:8080
.
To save images, send a POST
request to http://localhost:8080/save-images
with a JSON body. For example, using curl
:
curl -X POST http://localhost:8080/save-images \
-H "Content-Type: application/json" \
-d '[
{
"folderPath": "my-images/animals",
"imageURL": "https://example.com/cat.jpg"
},
{
"folderPath": "my-images/landscapes",
"imageURL": "https://example.com/mountain.png",
"imageName": "mountain_view.png"
}
]'
- folderPath (string, required): The relative path from
dist
where the image will be saved. E.g.,my-images/animals
. - imageURL (string, required): The URL of the image to be downloaded.
- imageName (string, optional): The desired filename for the saved image. If not provided, the file name will be extracted from the URL.
After running the service and making requests, the directory structure might look like this:
dist/
├── my-images/
│ ├── animals/
│ │ └── cat.jpg
│ └── landscapes/
│ └── mountain_view.png