Skip to content

Commit

Permalink
Rename uses
Browse files Browse the repository at this point in the history
  • Loading branch information
hschne committed Dec 23, 2023
1 parent 2153163 commit 703e43b
Show file tree
Hide file tree
Showing 16 changed files with 280 additions and 155 deletions.
10 changes: 8 additions & 2 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
class ErrorsController < ApplicationController
def not_found
render status: :not_found
respond_to do |format|
format.html { render status: :not_found }
format.json { render json: { error: 'not_found' }, status: :not_found }
end
end

def internal_server_error
render status: :internal_server_error
respond_to do |format|
format.html { render status: :internal_server_error }
format.json { render json: { error: 'server_error' }, status: :internal_server_error }
end
end
end
12 changes: 6 additions & 6 deletions app/controllers/uploads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ class UploadsController < ApplicationController
skip_before_action :verify_authenticity_token, only: %i[upload preview]

def new
@upload = Upload.new(expiry: 10.minutes.from_now, remaining_uses: 1)
@upload = Upload.new(expiry: 10, uses: 1)
end

def upload
@upload = Upload.new(upload_params)
@upload.expiry = upload_params[:expiry].to_i.minutes.from_now
@upload.key = Upload.generate_key

respond_to do |format|
Expand All @@ -32,7 +33,7 @@ def preview

def download
@upload = upload_scope.find_by!(key: params[:id])
@upload.decrement!(:remaining_uses)
@upload.decrement!(:uses)

redirect_to(@upload.data.url(disposition: 'attachment', filename: @upload.data.filename.to_s),
allow_other_host: true)
Expand All @@ -41,14 +42,13 @@ def download
private

def upload_scope
Upload
# Upload.where('expiry > ?', DateTime.now).where('remaining_uses > ?', 0)
Upload.where('expiry > ?', DateTime.now).where(uses: (1..))
end

# Only allow a list of trusted parameters through.
def upload_params
params
.require(:upload).permit(:data, :expiry, :remaining_uses)
.with_defaults(expiry: 10.minutes.from_now, remaining_uses: 1)
.require(:upload).permit(:data, :expiry, :uses)
.with_defaults(expiry: 10.minutes.from_now, uses: 1)
end
end
6 changes: 4 additions & 2 deletions app/javascript/controllers/clipboard_controller.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Controller } from "@hotwired/stimulus";

export default class extends Controller {
static targets = ["source"];
static targets = ["source", "trigger"];

copy(event) {
event.preventDefault();
navigator.clipboard.writeText(this.sourceTarget.value);
navigator.clipboard.writeText(
this.sourceTarget.innerText || this.sourceTarget,
);

this.sourceTarget.focus();
var triggerElement = this.triggerTarget;
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/cleanup_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class CleanupJob < ApplicationJob

def perform
Upload.where(expiry: (..Time.now))
.or(Upload.where('remaining_uses < ?', 1))
.or(Upload.where('uses < ?', 1))
.destroy_all
end
end
16 changes: 8 additions & 8 deletions app/models/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
#
# 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
# id :integer not null, primary key
# expiry :datetime not null
# key :string not null
# previewed :boolean default(FALSE), not null
# uses :integer default(1), not null
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
Expand All @@ -21,7 +21,7 @@ class Upload < ApplicationRecord

validates :expiry, presence: true
validates :key, presence: true
validates :remaining_uses, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
validates :uses, presence: true, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
validates :data, attached: true, size: { less_than: 1024.kilobytes, message: 'must be smaller than 1 megabyte' }

validate :expires_range
Expand Down
34 changes: 34 additions & 0 deletions app/views/components/_code.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<code
data-controller="clipboard"
class="
w-full text-sm sm:text-base inline-flex text-left items-center space-x-4
bg-gray-800 text-white rounded-lg p-4 pl-6
"
>
<p class="w-full" data-clipboard-target="source">
<%= yield %>
</p>

<button
data-action="clipboard#copy"
class="
border border-gray-500 rounded-full text-gray-500 hover:text-white transition
py-1 px-4 gap-1 flex flex-row items-center justify-center self-start
"
>
<svg
class="shrink-0 h-5 w-5"
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
>
<path d="M8 2a1 1 0 000 2h2a1 1 0 100-2H8z"></path>
<path
d="M3 5a2 2 0 012-2 3 3 0 003 3h2a3 3 0 003-3 2 2 0 012 2v6h-4.586l1.293-1.293a1 1 0 00-1.414-1.414l-3 3a1 1 0 000 1.414l3 3a1 1 0 001.414-1.414L10.414 13H15v3a2 2 0 01-2 2H5a2 2 0 01-2-2V5zM15 11h2a1 1 0 110 2h-2v-2z"
>
</path>
</svg>
<span data-clipboard-target="trigger">Copy</span>
</button>
</code>
23 changes: 10 additions & 13 deletions app/views/uploads/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= form_with(model: upload, class: "flex flex-col gap-4 py-4", url: upload_path) do |form| %>
<%= form_with(model: upload, class: "flex flex-col gap-4 py-4 justify-center items-center", url: upload_path) do |form| %>
<% if upload.errors.any? %>
<div
id="error_explanation"
Expand Down Expand Up @@ -40,37 +40,34 @@
<%= form.file_field :data, class: "hidden", data: { action: "change->drop#display" } %>
<% end %>

<div class="flex flex-col sm:flex-row align-center grow gap-4">
<div class="flex flex-col w-full sm:flex-row align-center grow gap-4">
<div class="w-full">
<%= form.label :expiry %>
<%= form.select(
:expiry,
[
["1 Minutes", 1.minute.from_now],
["10 Minutes", 10.minutes.from_now],
["30 Minutes", 30.minutes.from_now],
["60 Minutes", 60.minutes.from_now],
],
{},
[["1 Minute", 1], ["10 Minutes", 10], ["30 Minutes", 30], ["60 Minutes", 60]],
{ selected: 10 },
class:
"block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full",
) %>

</div>

<div class="w-full">
<%= form.label :remaining_uses, "Uses" %>
<%= form.number_field :remaining_uses,
<%= form.label :uses, "Uses" %>
<%= form.number_field :uses,
value: 1,
min: "1",
max: "5",
class:
"block shadow rounded-md border border-gray-200 outline-none px-3 py-2 mt-2 w-full" %>
</div>
</div>

<div class="flex flex-row justify-center align-center w-full py-8">
<div class="flex flex-row justify-center align-center w-full sm:w-1/2 py-8">
<%= form.submit "Upload",
class:
"block text-white font-semibold outline-none bg-gray-800 w-full rounded-full grow px-12 py-3 text-sm focus:outline-none focus:ring sm:w-auto
" %>
" %>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/uploads/_upload.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@

<p class="my-5">
<strong class="block font-medium mb-1">Remaining uses:</strong>
<%= upload.remaining_uses %>
<%= upload.uses %>
</p>
</div>
2 changes: 1 addition & 1 deletion app/views/uploads/_upload.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

json.extract! upload, :key, :expiry, :remaining_uses, :created_at
json.extract! upload, :key, :expiry, :uses, :created_at
json.url Rails.application.routes.url_helpers.download_url(upload)
json.name upload.data.filename.to_s
Loading

0 comments on commit 703e43b

Please sign in to comment.