-
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.
- Loading branch information
Andrea Rossi
committed
Sep 2, 2016
1 parent
75eeb26
commit c87bca8
Showing
14 changed files
with
196 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,4 @@ | |
$(document).ready(function(){ | ||
var app = require("app"); | ||
app.start(); | ||
}); | ||
}); |
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,62 @@ | ||
_ = require("underscore") | ||
async = require("async") | ||
{EventEmitter} = require("events") | ||
require("whatwg-fetch") | ||
|
||
class ImageStore extends EventEmitter | ||
storeImages: (report, done) => | ||
@storeImagesFor("live", report, (updatedReport) => | ||
@storeImagesFor("dead", updatedReport, done) | ||
) | ||
|
||
deleteImage: (file) -> | ||
return unless file.url | ||
|
||
token = document.getElementsByName("csrf-token")[0].content | ||
fetch(file.url, { | ||
method: "DELETE", | ||
credentials: 'include', | ||
headers: {'X-CSRF-Token': token}, | ||
}).then((response) -> | ||
console.log("deleted, #{response.status}") | ||
) | ||
|
||
|
||
storeImagesFor: (type, report, done) -> | ||
async.map((report.answers?[type] || []), ((ape, next) => | ||
async.map((ape["photo_#{type}"]?.selected || []), @uploadPhotoFor(report), (err, uploadedPhotos) -> | ||
ape["photo_#{type}"].selected = uploadedPhotos if ape["photo_#{type}"]?.selected | ||
next(null, ape) | ||
) | ||
), (err, updatedApes) -> | ||
report.answers["type"] = updatedApes | ||
done(report) | ||
) | ||
|
||
uploadPhotoFor: (report) -> | ||
(photo, done) => | ||
return done(null, photo) if photo.id | ||
|
||
@uploadFile(photo.file, report.id, (response) -> | ||
console.log("called for #{response.headers.get("Location")}") | ||
photo.url = response.headers.get("Location") | ||
photo.id = parseInt(response.headers.get("Image-Id")) | ||
|
||
done(null, photo) | ||
) | ||
|
||
uploadFile: (file, reportId, done) -> | ||
form = new FormData() | ||
form.append('image', file) | ||
|
||
token = document.getElementsByName("csrf-token")[0].content | ||
fetch("/reports/#{reportId}/images", { | ||
method: "POST", | ||
headers: { | ||
'X-CSRF-Token': token | ||
}, | ||
body: form, | ||
credentials: 'include', | ||
}).then(done) | ||
|
||
module.exports = new ImageStore() |
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,31 @@ | ||
class ImagesController < ApplicationController | ||
def create | ||
if image = Image.create(report_id: params[:report_id], file: params[:image]) | ||
head 201, { | ||
location: report_image_path(report_id: image.report_id, id: image.id), | ||
image_id: image.id | ||
} | ||
else | ||
head 422 | ||
end | ||
end | ||
|
||
def show | ||
image = Image.where(report_id: params[:report_id], id: params[:id]).first or raise_404 | ||
if params[:size] | ||
redirect_to image.file.url(params[:size]) | ||
else | ||
redirect_to image.file.url | ||
end | ||
end | ||
|
||
def destroy | ||
image = Image.where(report_id: params[:report_id], id: params[:id]).first or raise_404 | ||
|
||
if image.destroy | ||
head 200 | ||
else | ||
head 422 | ||
end | ||
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,18 @@ | ||
# == Schema Information | ||
# | ||
# Table name: images | ||
# | ||
# id :integer not null, primary key | ||
# file_file_name :string | ||
# file_content_type :string | ||
# file_file_size :integer | ||
# file_updated_at :datetime | ||
# report_id :integer | ||
# | ||
|
||
class Image < ActiveRecord::Base | ||
has_attached_file :file, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png" | ||
validates_attachment_content_type :file, content_type: /\Aimage\/.*\z/ | ||
|
||
belongs_to :report | ||
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,8 @@ | ||
class CreateImagesTable < ActiveRecord::Migration | ||
def change | ||
create_table :images do |t| | ||
t.attachment :file | ||
t.references :report | ||
end | ||
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