Skip to content

Commit 9e130d4

Browse files
committed
chore: carrierwave rails implemented
1 parent dda02cd commit 9e130d4

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

lib/carrierwave/storage/ik_file.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module CarrierWave
2+
module Storage
3+
class IKFile
4+
# Initialize as required.
5+
6+
def initialize(identifier)
7+
@identifier=JSON.parse(identifier)
8+
ik_config=Rails.application.config.imagekit
9+
@imagekit=ImageKit::ImageKitClient.new(ik_config[:private_key],ik_config[:public_key],ik_config[:url_endpoint])
10+
end
11+
12+
# Duck-type methods for CarrierWave::SanitizedFile.
13+
def content_type
14+
"image/jpg"
15+
end
16+
def public_url
17+
@identifier['url']
18+
end
19+
def url(options = {})
20+
@identifier['url']
21+
end
22+
23+
def fileId
24+
@identifier['fileId']
25+
end
26+
def filename(options = {})
27+
@identifier['name']
28+
end
29+
def read
30+
end
31+
def size
32+
end
33+
def delete
34+
# file_id=@identifier['fileId']
35+
begin
36+
@imagekit.delete_file(fileId)
37+
rescue
38+
fileId
39+
end
40+
# binding.pry
41+
# return nil
42+
end
43+
def exists?
44+
end
45+
# Others... ?
46+
end
47+
48+
end
49+
50+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'uri'
2+
require 'net/http'
3+
require 'base64'
4+
module CarrierWave
5+
module Storage
6+
class ImageKitStore < Abstract
7+
8+
def initialize(*)
9+
super
10+
@cache_called = nil
11+
end
12+
13+
def store!(file)
14+
file.delete
15+
end
16+
17+
def retrieve!(identifier)
18+
19+
IKFile.new(identifier)
20+
end
21+
22+
def cache!(new_file)
23+
new_file.move_to(::File.expand_path(uploader.cache_path, uploader.root), uploader.permissions, uploader.directory_permissions, true)
24+
rescue Errno::EMLINK, Errno::ENOSPC => e
25+
raise(e) if @cache_called
26+
@cache_called = true
27+
28+
# NOTE: Remove cached files older than 10 minutes
29+
clean_cache!(600)
30+
31+
cache!(new_file)
32+
end
33+
34+
def retrieve_from_cache!(identifier)
35+
CarrierWave::SanitizedFile.new(::File.expand_path(uploader.cache_path(identifier), uploader.root))
36+
resp=@client.get(identifier)
37+
# binding.pry
38+
IKFile.new(resp)
39+
end
40+
41+
def delete_dir!(path)
42+
if path
43+
begin
44+
Dir.rmdir(::File.expand_path(path, uploader.root))
45+
rescue Errno::ENOENT
46+
# Ignore: path does not exist
47+
rescue Errno::ENOTDIR
48+
# Ignore: path is not a dir
49+
rescue Errno::ENOTEMPTY, Errno::EEXIST
50+
# Ignore: dir is not empty
51+
end
52+
end
53+
end
54+
55+
def clean_cache!(seconds)
56+
Dir.glob(::File.expand_path(::File.join(uploader.cache_dir, '*'), CarrierWave.root)).each do |dir|
57+
# generate_cache_id returns key formated TIMEINT-PID(-COUNTER)-RND
58+
time = dir.scan(/(\d+)-\d+-\d+(?:-\d+)?/).first.map(&:to_i)
59+
time = Time.at(*time)
60+
if time < (Time.now.utc - seconds)
61+
FileUtils.rm_rf(dir)
62+
end
63+
end
64+
end
65+
66+
end
67+
end
68+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module CarrierWave
2+
module Support
3+
module UriFilename
4+
def self.filename(url)
5+
path = url.split('?').first
6+
URI.decode(path).gsub(%r{.*/(.*?$)}, '\1')
7+
end
8+
end
9+
end
10+
end

0 commit comments

Comments
 (0)