forked from smart-village-solutions/smart-village-app-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request smart-village-solutions#159 from ikuseiGmbH/featur…
…e/SVA-226-cms-fileupload Feature: CMS File upload
- Loading branch information
Showing
14 changed files
with
283 additions
and
10 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 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,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "securerandom" | ||
|
||
# This controller handles file uploads to minio buckets | ||
# specifically for uploading images for e.g. POIs | ||
class MinioController < ApplicationController | ||
|
||
before_action :verify_current_user | ||
before_action :minio_setup | ||
|
||
def signed_url | ||
bucket = @minio_config["bucket"] | ||
headers = {} | ||
options = { path_style: true } | ||
|
||
url = @storage.put_object_url( | ||
bucket, | ||
generate_filename, | ||
15.minutes.from_now.to_time.to_i, | ||
headers, | ||
options | ||
) | ||
|
||
respond_to do |format| | ||
format.json { render json: { signedUrl: url } } | ||
end | ||
end | ||
|
||
private | ||
|
||
# We overwrite ApplicationController here, because we do not | ||
# need a redirect but just a http response instead | ||
def verify_current_user | ||
head :unauthorized unless session["current_user"] | ||
end | ||
|
||
def minio_setup | ||
@minio_config = session["current_user"]["minio"] | ||
@storage = Fog::Storage.new( | ||
provider: "AWS", | ||
endpoint: @minio_config["endpoint"], | ||
aws_access_key_id: @minio_config["access_key_id"], | ||
aws_secret_access_key: @minio_config["secret_access_key"], | ||
region: @minio_config["region"] | ||
) | ||
end | ||
|
||
def generate_filename | ||
extension = File.extname(params[:filename]) | ||
filename = File.basename(params[:filename], extension).gsub(" ", "-") | ||
|
||
"cms_uploads/#{filename}-#{SecureRandom.hex}#{extension}" | ||
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
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,61 @@ | ||
const axios = require('axios') | ||
|
||
async function getSignedUrl(filename) { | ||
let response = await axios.get('/minio/signed_url.json?filename=' + filename); | ||
return response.data.signedUrl; | ||
} | ||
|
||
async function upload(file, signedUrl, formIndex) { | ||
|
||
$('.upload-progress-' + formIndex).show('slow'); | ||
|
||
await axios.put(signedUrl, file, { | ||
headers: { | ||
'Content-Type': file.type | ||
}, | ||
onUploadProgress: (e) => { | ||
let percentCompleted = Math.round((e.loaded * 100) / e.total); | ||
$('.upload-progress-bar-' + formIndex) | ||
.attr('aria-valuenow', percentCompleted) | ||
.css('width', percentCompleted + '%'); | ||
}, | ||
}); | ||
} | ||
|
||
async function handleFileChange(e) { | ||
try { | ||
// Get upload URL | ||
const file = e.target.files[0]; | ||
// There might be multiple forms on the page | ||
const formIndex = e.target.dataset.index; | ||
const signedUrl = await getSignedUrl(file.name); | ||
const fileUrl = signedUrl.split('?')[0]; | ||
|
||
// Upload the file | ||
await upload(file, signedUrl, formIndex); | ||
|
||
$('.image-preview-' + formIndex).attr('src', fileUrl); | ||
$('.image-preview-wrapper-' + formIndex).show('slow'); | ||
|
||
// Put the url of the uploaded file into the url input and disable it | ||
$(e.target).closest('.nested-medium-form').find('.media-content-url') | ||
.val(fileUrl) | ||
.attr('disabled', true); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
} | ||
|
||
// Saving this globally, because we need to rebind events | ||
// when a new nested form is added | ||
window.bindFileUploadEvents = () => { | ||
$('.upload-toggle').click((e) => { | ||
const formIndex = e.target.dataset.index; | ||
$('.file-input-' + formIndex).click(); | ||
}); | ||
$('.file-input').change(handleFileChange); | ||
} | ||
|
||
$(() => { | ||
window.bindFileUploadEvents(); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<div class=file-upload-wrapper"> | ||
<p> | ||
<button | ||
class="btn btn-primary upload-toggle" | ||
type="button" | ||
data-toggle="collapse" | ||
data-target=".file-upload-collapse-<%= index %>" | ||
data-index="<%= index %>" | ||
aria-expanded="false" | ||
aria-controls="collapseExample"> | ||
Bild vom Computer hochladen | ||
</button> | ||
</p> | ||
<div class="collapse file-upload-collapse file-upload-collapse-<%= index %>"> | ||
<div class="card card-body"> | ||
<input class="form-control-file file-input file-input-<%= index %>" type="file" data-index="<%= index %>"> | ||
<div class="upload-progress upload-progress-<%= index %>"> | ||
<div class="progress"> | ||
<div | ||
class="progress-bar upload-progress-bar upload-progress-bar-<%= index %>" | ||
role="progressbar" | ||
aria-valuenow="0" | ||
aria-valuemin="0" | ||
aria-valuemax="100"> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="image-preview-wrapper image-preview-wrapper-<%= index %>"> | ||
<label>Bildvorschau:</label> | ||
<img src="" class="image-preview image-preview-<%= index %>" /> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
Oops, something went wrong.