-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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 #2 from luilver/xls_files
Xls files
- Loading branch information
Showing
9 changed files
with
123 additions
and
2 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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2008-2013 Michael Dvorkin and contributors. | ||
# | ||
# Fat Free CRM is freely distributable under the terms of MIT license. | ||
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php | ||
#------------------------------------------------------------------------------ | ||
# == Schema Information | ||
# | ||
# Table name: imported_files | ||
# | ||
# id :integer not null, primary key | ||
# filename :string(64) default(""), not null | ||
# md5sum :string(32) default(""), not null | ||
# | ||
|
||
class ImportedFile < ActiveRecord::Base | ||
before_validation :generate_md5sum | ||
|
||
validate :filetype | ||
|
||
validates :filename, presence: true | ||
validates :md5sum, presence: true | ||
validates :md5sum, uniqueness: { message: "file already imported" } | ||
|
||
def generate_md5sum | ||
self.md5sum = Digest::MD5.hexdigest File.open(filename).read unless filename.empty? rescue "" | ||
end | ||
|
||
private | ||
|
||
def filetype | ||
valid = File.open(filename).type_from_file_command == "application/vnd.ms-excel" rescue "" | ||
if valid == "" | ||
errors.add(:filename, "no such file") | ||
end | ||
unless valid | ||
errors.add(:filename, "invalid filetype") | ||
end | ||
end | ||
|
||
ActiveSupport.run_load_hooks(:fat_free_crm_imported_file, self) | ||
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,15 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2008-2013 Michael Dvorkin and contributors. | ||
# | ||
# Fat Free CRM is freely distributable under the terms of MIT license. | ||
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php | ||
#------------------------------------------------------------------------------ | ||
File.class_eval do | ||
def type_from_file_command | ||
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase | ||
mime_type = `file -b --mime-type #{self.path}`.split(':').last.strip rescue "application/x-#{type}" | ||
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/) | ||
mime_type | ||
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,16 @@ | ||
# frozen_string_literal: true | ||
|
||
class CreateImportedFiles < ActiveRecord::Migration[4.2] | ||
def self.up | ||
create_table :imported_files, force: true do |t| | ||
t.string :filename, limit: 64, null: false, default: "" | ||
t.string :md5sum, limit: 32, null: false, default: "" | ||
|
||
t.timestamps | ||
end | ||
end | ||
|
||
def self.down | ||
drop_table :imported_files | ||
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,13 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2008-2013 Michael Dvorkin and contributors. | ||
# | ||
# Fat Free CRM is freely distributable under the terms of MIT license. | ||
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php | ||
#------------------------------------------------------------------------------ | ||
FactoryBot.define do | ||
factory :imported_file do | ||
filename { "MyString" } | ||
md5sum { "MyString" } | ||
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,21 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2008-2013 Michael Dvorkin and contributors. | ||
# | ||
# Fat Free CRM is freely distributable under the terms of MIT license. | ||
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php | ||
#------------------------------------------------------------------------------ | ||
# == Schema Information | ||
# | ||
# Table name: imported_files | ||
# | ||
# id :integer not null, primary key | ||
# filename :string(64) default(""), not null | ||
# md5sum :string(32) default(""), not null | ||
# | ||
|
||
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') | ||
|
||
RSpec.describe ImportedFile, type: :model do | ||
pending "add some examples to (or delete) #{__FILE__}" | ||
end |