This project provides a simple API for uploading and retrieving files using a Roda-based web server. It uses Git for versioning files, ensuring that even deleted files can still be accessed via their SHA1 hash if not pruned.
- Ruby
- Bundler
- Git
- Clone the repository:
git clone your-repository-url
- Navigate to the project directory:
cd your-project-directory
- Install dependencies:
bundle install
To start the server, run:
bundle exec rackup
HOSTNAME
: Sets the hostname for the server. If not specified, defaults tolocalhost:3000
.
To upload a file:
curl -X POST -F "file=@path_to_your_file" http://$HOSTNAME/files/optional_filename_here
If you do not specify a filename in the URL, the original filename will be used. If the file data does not contain a filename, it defaults to anonymous.txt
.
Upon successfully uploading a file, the server will return a JSON object containing the URL to access the uploaded file. Here's an example of what this response might look like:
{
"url": "http://localhost:3000/files/1a2b3c4d5e6f7890123456789abcdef01234567/sample.txt"
}
This URL includes the SHA1 hash of the uploaded file and the filename, allowing you to access the file directly through the API.
To retrieve a file:
curl http://$HOSTNAME/files/{sha1}/{filename}
You can also pipe content directly to curl
for uploading. Here's how to upload content from a command:
echo "Sample text data" | curl -X POST -F "file=@-" http://$HOSTNAME/files/sample.txt
Here's a simple Ruby script to upload a file using Net::HTTP:
require 'net/http'
require 'uri'
uri = URI('http://your-hostname/files/filename.txt')
request = Net::HTTP::Post.new(uri)
request.set_form([['file', File.open('path_to_your_file')]], 'multipart/form-data')
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
end
puts response.body
This project is licensed under the MIT License - see the LICENSE file for details.