Skip to content

Commit 27ab11b

Browse files
committed
removed net:http requests that are too slow for checking if link is img and replace with javascript validation for it
1 parent ea5554b commit 27ab11b

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

app/assets/javascripts/application.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@
1414
//= require activestorage
1515
//= require turbolinks
1616
//= require_tree .
17+
18+
function imgErrorPhoto(image) {
19+
image.onerror = '';
20+
image.src = 'https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png';
21+
return true;
22+
}
23+
24+
function imgErrorCover(image) {
25+
image.onerror = '';
26+
image.src = 'https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/cover_default.jpg';
27+
return true;
28+
}

app/helpers/application_helper.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
require 'resolv-replace'
2-
require 'net/http'
3-
require 'uri'
4-
51
module ApplicationHelper
62
def header_bar
73
cntnt = "<a href='/opinions'>"
@@ -52,27 +48,15 @@ def flash_info
5248
def current_profile(usr)
5349
usr = fill_user_images(usr)
5450
begin
55-
image_tag(usr.photo, class: 'profile p-2', alt: usr.username)
51+
image_tag(usr.photo, class: 'profile p-2', alt: usr.username, onerror: 'imgErrorPhoto(this);')
5652
rescue Sprockets::Rails::Helper::AssetNotFound
5753
image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'profile p-2', alt: usr.username)
5854
end
5955
end
6056

6157
def fill_user_images(usr)
62-
# usr.photo = 'https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png' if usr.photo.blank? || !image_exists?(usr.photo)
63-
# usr.cover_image = 'https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/cover_default.jpg' if usr.cover_image.blank? || !image_exists?(usr.cover_image)
6458
usr.photo = 'https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png' if usr.photo.blank?
6559
usr.cover_image = 'https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/cover_default.jpg' if usr.cover_image.blank?
6660
usr
6761
end
68-
69-
# def image_exists?(url)
70-
# response = {}
71-
# uri = URI(url)
72-
# Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http|
73-
# request = Net::HTTP::Get.new uri
74-
# response = http.request request # Net::HTTPResponse object
75-
# end
76-
# response.content_type.starts_with?('image')
77-
# end
7862
end

app/helpers/opinions_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ def linked_unlinked_comment(opi)
1111
opi.user = fill_user_images(opi.user)
1212
if current_user
1313
begin
14-
link_to image_tag(opi.user.photo, class: 'timeline-photo', alt: opi.user.username), user_path(opi.user)
14+
link_to image_tag(opi.user.photo, class: 'timeline-photo', alt: opi.user.username, onerror: 'imgErrorPhoto(this);'), user_path(opi.user)
1515
rescue Sprockets::Rails::Helper::AssetNotFound
1616
link_to image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'timeline-photo', alt: opi.user.username), user_path(opi.user)
1717
end
1818
else
1919
begin
20-
image_tag(opi.user.photo, class: 'timeline-photo', alt: opi.user.username)
20+
image_tag(opi.user.photo, class: 'timeline-photo', alt: opi.user.username, onerror: 'imgErrorPhoto(this);')
2121
rescue Sprockets::Rails::Helper::AssetNotFound
2222
image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'timeline-photo', alt: opi.user.username)
2323
end
@@ -28,13 +28,13 @@ def linked_unlinked_who(usr)
2828
usr = fill_user_images(usr)
2929
if current_user
3030
begin
31-
link_to image_tag(usr.photo, class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username), user_path(usr), class: 'align-self-center'
31+
link_to image_tag(usr.photo, class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username, onerror: 'imgErrorPhoto(this);'), user_path(usr), class: 'align-self-center'
3232
rescue Sprockets::Rails::Helper::AssetNotFound
3333
link_to image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username), user_path(usr), class: 'align-self-center'
3434
end
3535
else
3636
begin
37-
image_tag(usr.photo, class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username)
37+
image_tag(usr.photo, class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username, onerror: 'imgErrorPhoto(this);')
3838
rescue Sprockets::Rails::Helper::AssetNotFound
3939
image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username)
4040
end

app/helpers/users_helper.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def user_copied_info(opi)
4040
def cover_photo(usr)
4141
usr = fill_user_images(usr)
4242
begin
43-
image_tag(usr.cover_image, class: 'cover-img', alt: 'Cover_Image')
43+
image_tag(usr.cover_image, class: 'cover-img', alt: 'Cover_Image', onerror: 'imgErrorCover(this);')
4444
rescue Sprockets::Rails::Helper::AssetNotFound
4545
image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/cover_default.jpg', class: 'cover-img', alt: 'Cover_Image')
4646
end
@@ -49,7 +49,7 @@ def cover_photo(usr)
4949
def link_user_img(usr)
5050
usr = fill_user_images(usr)
5151
begin
52-
link_to image_tag(usr.photo, class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username), user_path(usr), class: 'align-self-center'
52+
link_to image_tag(usr.photo, class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username, onerror: 'imgErrorPhoto(this);'), user_path(usr), class: 'align-self-center'
5353
rescue Sprockets::Rails::Helper::AssetNotFound
5454
link_to image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'rounded-circle who-photo mx-2 align-self-center', alt: usr.username), user_path(usr), class: 'align-self-center'
5555
end
@@ -58,7 +58,7 @@ def link_user_img(usr)
5858
def user_photo(opi)
5959
opi.user = fill_user_images(opi.user)
6060
begin
61-
image_tag(opi.user.photo, class: 'timeline-photo', alt: 'User')
61+
image_tag(opi.user.photo, class: 'timeline-photo', alt: 'User', onerror: 'imgErrorPhoto(this);')
6262
rescue Sprockets::Rails::Helper::AssetNotFound
6363
image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'timeline-photo', alt: 'User')
6464
end
@@ -67,7 +67,7 @@ def user_photo(opi)
6767
def profile_photo(usr)
6868
usr = fill_user_images(usr)
6969
begin
70-
image_tag(usr.photo, class: 'profile-right p-2', alt: usr.username)
70+
image_tag(usr.photo, class: 'profile-right p-2', alt: usr.username, onerror: 'imgErrorPhoto(this);')
7171
rescue Sprockets::Rails::Helper::AssetNotFound
7272
image_tag('https://raw.githubusercontent.com/Stricks1/cap-twitter/feature/app/assets/images/user_default.png', class: 'profile-right p-2', alt: usr.username)
7373
end

0 commit comments

Comments
 (0)