cliz 0.0.6 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/cliz +25 -10
- data/lib/functions.rb +94 -44
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: de4a5cd7ac9ce9598f60e1674053920a3362d5cf93a9fc19c1b879845fb06567
|
4
|
+
data.tar.gz: 23e5e440771e05f9e15a8b8a508de4747fccf613dc1af6e459cce7c9bad85c5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7e8d8bde06fc90d008c29ccfa234157a9d26d160557ecc6cef78cd201db533c5ec13cedfd1f4a3d05b2b5b33bef7b4dbe43cb4e9611b3025070f3594a0232892
|
7
|
+
data.tar.gz: 504e21a63bd2359ce50ef4e567fbb3b894c2d92aed4a1c37e2d40f105b4661ba11e698706891a96e564b6ef2cb799c6f0d5ac8cefb527083dab1eeeeab011f1d
|
data/bin/cliz
CHANGED
@@ -3,25 +3,40 @@
|
|
3
3
|
require 'securerandom'
|
4
4
|
require 'fileutils'
|
5
5
|
require 'yaml'
|
6
|
+
require 'fileutils'
|
6
7
|
require_relative '../lib/functions.rb'
|
7
8
|
|
9
|
+
include Cliz
|
10
|
+
|
11
|
+
|
8
12
|
|
9
13
|
config = YAML.load(File.read('config.yml'))
|
14
|
+
output_dir = config['output_dir']
|
10
15
|
|
11
|
-
|
12
|
-
BASE_DOMAIN = config['base_domain']
|
13
|
-
DEPLOY_SCRIPT = config['deploy_script']
|
14
|
-
PRE_SCRIPT = config['pre_script']
|
16
|
+
FileUtils.mkdir_p File.join output_dir, 'reverse-index'
|
15
17
|
|
16
18
|
url = ARGV[0]
|
17
19
|
short_suggestion = ARGV[1]
|
18
20
|
|
19
|
-
|
21
|
+
unless short_suggestion
|
22
|
+
# check for reverse index
|
23
|
+
reverse_index_file = File.join output_dir, 'reverse-index', "#{Digest::SHA2.hexdigest url}"
|
24
|
+
|
25
|
+
if File.exist? reverse_index_file
|
26
|
+
base_domain = config['base_domain']
|
27
|
+
paths = File.open(reverse_index_file).readlines
|
28
|
+
for path in paths
|
29
|
+
puts File.join base_domain, path
|
30
|
+
end
|
31
|
+
exit
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
short_url = short config, url, short_suggestion
|
20
37
|
|
21
|
-
|
22
|
-
folder_path = get_a_folder_path OUTPUT_DIR, short_suggestion
|
23
|
-
create_index_file(url, folder_path)
|
38
|
+
# do reverse index
|
24
39
|
|
25
|
-
|
40
|
+
reverse_index config, short_url
|
26
41
|
|
27
|
-
puts
|
42
|
+
puts short_url
|
data/lib/functions.rb
CHANGED
@@ -1,57 +1,107 @@
|
|
1
1
|
require 'securerandom'
|
2
2
|
require 'fileutils'
|
3
|
+
require 'uri'
|
4
|
+
require 'digest'
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
module Cliz
|
7
|
+
##
|
8
|
+
# Gets a folder path for the shortened url
|
9
|
+
def get_a_folder_path output_directory, short_suggestion = nil
|
10
|
+
folder_path = nil
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
short_suggestion ||= generate_short_suggestion output_directory
|
13
|
+
folder_path = File.join(output_directory, short_suggestion, '/')
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
if File.directory?(folder_path)
|
16
|
+
raise ("This short URL is already taken.")
|
17
|
+
end
|
18
|
+
|
19
|
+
folder_path
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_short_suggestion output_directory
|
23
|
+
counter = 0
|
24
|
+
random_string = nil
|
25
|
+
|
26
|
+
loop do
|
27
|
+
random_string = SecureRandom.alphanumeric[..counter]
|
28
|
+
folder_path = File.join(output_directory, random_string, '/')
|
29
|
+
break unless File.directory?(folder_path)
|
30
|
+
counter += 1
|
31
|
+
end
|
32
|
+
|
33
|
+
random_string
|
14
34
|
end
|
15
35
|
|
16
|
-
folder_path
|
17
|
-
|
36
|
+
def create_index_file url, folder_path
|
37
|
+
html = %Q{
|
38
|
+
<!DOCTYPE html>
|
39
|
+
<html>
|
40
|
+
<head>
|
41
|
+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
|
42
|
+
<meta content="utf-8" http-equiv="encoding">
|
43
|
+
<script type="text/javascript">
|
44
|
+
window.location.replace("https://cliz.in");
|
45
|
+
</script>
|
46
|
+
<meta http-equiv="refresh" content="0; url='https://cliz.in'" />
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<p>Please follow <a href="https://cliz.in">this link</a>.</p>
|
50
|
+
</body>
|
51
|
+
</html>
|
52
|
+
}
|
18
53
|
|
19
|
-
|
20
|
-
|
21
|
-
random_string = nil
|
54
|
+
redirect_content = html.gsub 'https://cliz.in', url
|
55
|
+
FileUtils.mkdir_p(folder_path)
|
22
56
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
break unless File.directory?(folder_path)
|
27
|
-
counter += 1
|
57
|
+
File.open "#{folder_path}/index.html", 'w' do |f|
|
58
|
+
f.puts redirect_content
|
59
|
+
end
|
28
60
|
end
|
29
61
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
62
|
+
def short config, url, short_suggestion = nil
|
63
|
+
output_dir = config['output_dir']
|
64
|
+
base_domain = config['base_domain']
|
65
|
+
deploy_script = config['deploy_script']
|
66
|
+
pre_script = config['pre_script']
|
67
|
+
|
68
|
+
system("sh #{pre_script}") if pre_script
|
69
|
+
|
70
|
+
short_suggestion ||= generate_short_suggestion output_dir
|
71
|
+
folder_path = get_a_folder_path output_dir, short_suggestion
|
72
|
+
create_index_file(url, folder_path)
|
73
|
+
|
74
|
+
system("sh #{deploy_script}") if deploy_script
|
75
|
+
|
76
|
+
File.join base_domain, short_suggestion
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Gets the path of short_url
|
81
|
+
# Say its https://cliz.in/abc/d, it returns abc/d
|
82
|
+
def path config, short_url
|
83
|
+
base_domain = config['base_domain']
|
84
|
+
short_url.gsub base_domain, ""
|
85
|
+
end
|
86
|
+
|
87
|
+
def long_url config, short_url
|
88
|
+
output_dir = config['output_dir']
|
89
|
+
|
90
|
+
path = path config, short_url
|
91
|
+
file_contents = File.open(File.join(output_dir, path, 'index.html')).readlines.join '\n'
|
92
|
+
URI.extract(file_contents).first
|
93
|
+
end
|
94
|
+
|
95
|
+
def reverse_index config, short_url
|
96
|
+
output_dir = config['output_dir']
|
97
|
+
|
98
|
+
long_url = long_url config, short_url
|
99
|
+
|
100
|
+
reverse_index_file = File.join output_dir, 'reverse-index', "#{Digest::SHA2.hexdigest long_url}"
|
101
|
+
|
102
|
+
|
103
|
+
File.open reverse_index_file, "a" do |f|
|
104
|
+
f.puts path(config, short_url)
|
105
|
+
end
|
56
106
|
end
|
57
107
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cliz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karthikeyan A K
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Cliz - The static URL shortner
|
14
14
|
email: mindaslab@protonmail.com
|
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
|
-
rubygems_version: 3.2.
|
41
|
+
rubygems_version: 3.2.22
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: Cliz - The static URL shortner
|