-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
271 additions
and
362 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class ErrorsController < ApplicationController | ||
def not_found | ||
render status: 404 | ||
end | ||
|
||
def internal_server_error | ||
render status: 500 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,53 @@ | ||
# frozen_string_literal: true | ||
|
||
class UploadsController < ApplicationController | ||
before_action :set_upload, only: %i[show preview] | ||
|
||
def show; end | ||
|
||
def preview; end | ||
include ActiveStorage::SetCurrent | ||
include ActionController::Live | ||
|
||
def new | ||
@upload = Upload.new | ||
@upload = Upload.new(expiry: 10.minutes.from_now, remaining_uses: 1) | ||
end | ||
|
||
def create | ||
def upload | ||
@upload = Upload.new(upload_params) | ||
@upload.key = Upload.generate_key | ||
|
||
respond_to do |format| | ||
if @upload.save | ||
format.html { redirect_to upload_url(@upload), notice: 'Upload was successfully created.' } | ||
format.json { render :show, status: :created, location: @upload } | ||
format.html { redirect_to preview_url(@upload) } | ||
format.json { render :preview, status: :created, location: @upload } | ||
else | ||
format.html { render :new, status: :unprocessable_entity } | ||
format.json { render json: @upload.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def preview | ||
@upload = upload_scope.where(previewed: false).find_by!(key: params[:id]) | ||
@upload.update!(previewed: true) | ||
respond_to do |format| | ||
format.html { render :preview, status: :not_found } | ||
end | ||
end | ||
|
||
def download | ||
@upload = upload_scope.find_by!(key: params[:id]) | ||
@upload.decrement!(:remaining_uses) | ||
|
||
redirect_to rails_blob_path(@upload.data, disposition: 'attachment') | ||
# send_data @upload.data.download, filename: @upload.data.filename.to_s, content_type: @upload.data.content_type | ||
end | ||
|
||
private | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_upload | ||
@upload = Upload.find(params[:id]) | ||
def upload_scope | ||
Upload.where('expiry > ?', DateTime.now).where('remaining_uses > ?', 0) | ||
end | ||
|
||
# Only allow a list of trusted parameters through. | ||
def upload_params | ||
params.require(:upload).permit(:path, :data, :expiry, :remaining_uses, :previewed) | ||
params | ||
.require(:upload).permit(:data, :expiry, :remaining_uses) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module ErrorsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails | ||
import "@hotwired/turbo-rails" | ||
import "controllers" | ||
import "@hotwired/turbo-rails"; | ||
import "controllers"; | ||
|
||
Turbo.setFormMode("optin"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
# == Schema Information | ||
# | ||
# Table name: uploads | ||
# | ||
# id :integer not null, primary key | ||
# expiry :datetime not null | ||
# key :string not null | ||
# previewed :boolean default(FALSE), not null | ||
# remaining_uses :integer default(1), not null | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
# Indexes | ||
# | ||
# index_uploads_on_key (key) UNIQUE | ||
# | ||
class Upload < ApplicationRecord | ||
has_one_attached :data | ||
|
||
validates :data, attached: true, size: { less_than: 512.kilobytes, message: 'is must be smaller than 512 kb' } | ||
|
||
def to_param | ||
key | ||
end | ||
|
||
def self.generate_key | ||
ids = (1..3).map { (1..5).map { rand(1..6) }.join.to_i } | ||
DiceWord | ||
.find(*ids) | ||
.map(&:words) | ||
.map { |w| w.split(' ') } | ||
.map { |t| t[rand(0..t.length - 1)] }.flatten.join('-') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="w-full h-full grid place-content-center bg-white"> | ||
<h1 class="uppercase tracking-widest font-semibold text-gray-700">500 | Server Error</h1> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<div class="w-full h-full grid place-content-center bg-white"> | ||
<h1 class="uppercase tracking-widest font-semibold text-gray-700">404 | Not Found</h1> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
<section> | ||
<div class="mx-auto md:w-2/3 w-full"> | ||
<section class="px-4 py-32"> | ||
<div class="mx-auto max-w-2xl "> | ||
<h1 class="font-bold text-4xl">New upload</h1> | ||
|
||
<%= render "form", upload: @upload %> | ||
|
||
<%= link_to "Back to uploads", | ||
uploads_path, | ||
class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %> | ||
</div> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<section> | ||
<h2>A Preview</h2> | ||
<div class="mx-auto md:w-2/3 w-full flex"> | ||
<div class="mx-auto"> | ||
<% if notice.present? %> | ||
<p | ||
class=" | ||
py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block | ||
" | ||
id="notice" | ||
><%= notice %></p> | ||
<% end %> | ||
|
||
<%= render @upload %> | ||
|
||
</div> | ||
</div> | ||
</section> |
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.