Skip to content

Commit

Permalink
fixed bugs and issues
Browse files Browse the repository at this point in the history
  • Loading branch information
elblogbruno committed Aug 22, 2021
1 parent dc6a2c5 commit 7e18cae
Show file tree
Hide file tree
Showing 24 changed files with 505 additions and 305 deletions.
3 changes: 2 additions & 1 deletion controller/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def create_app(config_type):
app.register_blueprint(files_blu)
from controller.modules.files_utils import files_utils_blu
app.register_blueprint(files_utils_blu)
setup_log(config_class.LOG_LEVEL)

#setup_log(config_class.LOG_LEVEL)

socketio.init_app(app)

Expand Down
7 changes: 5 additions & 2 deletions controller/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,11 @@ def __repr__(self):
def delete(self, path=None):
db.session.query(Stop).filter(Stop.id == self.id).delete()
db.session.commit()
# if path and os.path.exists(path):
# os.path.removedir(path)
if path and os.path.exists(path):
print("Deleting stop folder")
shutil.rmtree(path, ignore_errors=True)
else:
print("Can't delete stop folder")
return True

def save(self):
Expand Down
10 changes: 6 additions & 4 deletions controller/modules/files/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def get_image_tags(path_name, filename):
print(tags)
std_fmt = '%Y:%m:%d %H:%M:%S+%M:%S'
is_360 = 'XMP:ProjectionType' in tags
print("Image is 360? " + str(is_360))
lat = 0
long = 0
creation_datetime = 0
Expand All @@ -64,10 +65,10 @@ def get_image_tags(path_name, filename):
lat = tags['Composite:GPSLatitude']
long = tags['Composite:GPSLongitude']

create_date = str(tags['File:FileCreateDate'])
creation_datetime = parser.parse(create_date)

if creation_datetime == 0:
if 'File:FileCreateDate' in tags:
create_date = str(tags['File:FileCreateDate'])
creation_datetime = parser.parse(create_date)
else:
match_str = re.search(r'\d{4}-\d{2}-\d{2}', filename)
if match_str:
creation_datetime = datetime.strptime(match_str.group(), '%Y-%m-%d').date()
Expand All @@ -77,6 +78,7 @@ def get_image_tags(path_name, filename):

return lat, long, creation_datetime, is_360
except:
print("Error")
return 0, 0, datetime.today(), False

def get_video_tags(path_name, filename):
Expand Down
76 changes: 41 additions & 35 deletions controller/modules/files/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def save_file_to_database(is_image, travel_id, stop_id, name, lat_coord, long_co
time_duration = video_duration
else:
time_duration = VideoUtils.get_video_info(video_file_path)

address = GeoCodeUtils.reverse_latlong(lat_coord, long_coord)
video = Wanderpi(id=file_id, name=name, lat=lat_coord, long=long_coord, file_thumbnail_path=file_thumbnail_path, travel_id=travel_id, stop_id=stop_id, address=address, time_duration=time_duration, file_path=file_path, is_image=is_image, has_been_edited=edit_video, created_date=created_date, is_360=is_360)
video.save()
Expand Down Expand Up @@ -335,41 +335,47 @@ def process_upload(stop_id):
CUSTOM_STATIC_FOLDER, VIDEOS_FOLDER, UPLOAD_FOLDER = load_custom_video_folder()

final_folder_path = UPLOAD_FOLDER + stop_name + "/"
upload_files = [f for f in listdir(final_folder_path) if isfile(join(final_folder_path, f))]

if len(upload_files) > 0:
for file in upload_files:
emit('process_upload_folder_update', 'Uploading file {0}'.format(file))
path_to_move_file = final_folder_path+file
if (get_file_extension(file) in IMAGE_EXTENSIONS):
#move file to images folder
file_type = 'images'
elif (get_file_extension(file) in VIDEO_EXTENSIONS):
#move file to videos folder
file_type = 'videos'
else:
print("File not allowed")

path = get_travel_folder_path(travel_id=travel_id, filename_or_file_id=file, file_type=file_type)
static_path = get_travel_folder_path_static(travel_id=travel_id, filename_or_file_id=file, file_type=file_type)

if not os.path.isfile(path):
shutil.move(path_to_move_file, path)
upload_file_to_database(path, static_path, file, travel_id,stop_id)
emit('process_upload_folder_update', 'File {0} successfully uploaded'.format(file.encode('utf-8')))
else:
print("File already exists")
os.remove(join(final_folder_path, file))
emit('process_upload_folder_update', 'File {0} already exists'.format(file.encode('utf-8')))

sleep(0.1)
emit('process_upload_folder_update', "200")
else:
emit('process_upload_folder_update', "No files on the uploads folder")
try:

upload_files = [f for f in listdir(final_folder_path) if isfile(join(final_folder_path, f))]

if len(upload_files) > 0:
for file in upload_files:
emit('process_upload_folder_update', 'Uploading file {0}'.format(file))
path_to_move_file = final_folder_path+file
if (get_file_extension(file) in IMAGE_EXTENSIONS):
#move file to images folder
file_type = 'images'
elif (get_file_extension(file) in VIDEO_EXTENSIONS):
#move file to videos folder
file_type = 'videos'
else:
print("File not allowed")

path = get_travel_folder_path(travel_id=travel_id, filename_or_file_id=file, file_type=file_type)
static_path = get_travel_folder_path_static(travel_id=travel_id, filename_or_file_id=file, file_type=file_type)

if not os.path.isfile(path):
shutil.move(path_to_move_file, path)
upload_file_to_database(path, static_path, file, travel_id,stop_id)
emit('process_upload_folder_update', 'File {0} successfully uploaded'.format(file.encode('utf-8')))
else:
print("File already exists")
os.remove(join(final_folder_path, file))
emit('process_upload_folder_update', 'File {0} already exists'.format(file.encode('utf-8')))

sleep(0.1)
emit('process_upload_folder_update', "200")
else:
emit('process_upload_folder_update', "No files on the uploads folder")
sleep(2)
emit('process_upload_folder_update', "")
emit('process_upload_folder_update', "200")

return redirect("/stop/"+stop_id)
except OSError as e:
emit('process_upload_folder_update', str(e))
sleep(2)
emit('process_upload_folder_update', "")
emit('process_upload_folder_update', "200")

return redirect("/stop/"+stop_id)


10 changes: 7 additions & 3 deletions controller/modules/files_utils/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
from controller.utils.video_editor import VideoEditor
from controller.utils.image_editor import ImageEditor

@files_utils_blu.route('/uploads/<string:travel_id>/<string:filename>', methods=['GET', 'POST'])
@files_utils_blu.route('/download_file/<string:travel_id>/<string:filename>', methods=['GET'])
def download_file(travel_id, filename):
print(travel_id, filename)
file = Wanderpi.get_by_id(filename)
file_type = 'images' if file.is_image else 'videos'
abs_file_path = get_travel_folder_path(travel_id=travel_id, file_type=file_type)
Expand All @@ -18,8 +19,11 @@ def download_file(travel_id, filename):
if '.' not in filename:
filename = file.file_path.split('/')[-1]

return send_from_directory(abs_file_path, filename, as_attachment=True)

try:
return send_from_directory(abs_file_path, filename, as_attachment=True)
except:
print("Error sending from directory")
return "File not available"

@files_utils_blu.route('/get_share_image/<string:travel_id>/<string:filename>', methods=['GET', 'POST'])
def get_share_image(travel_id, filename):
Expand Down
5 changes: 5 additions & 0 deletions controller/modules/home/geocode_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ def reverse_latlong(lat,long):
"""Function that having a lat and long translates it to a posiblea ddress"
"""
#check if user has internet connection
if lat == 0 and long == 0:
return "Location unknown"

if not GeoCodeUtils.has_internet_connection():
return GeoCodeUtils.reverse_search_offline(lat, long)
else:
Expand Down Expand Up @@ -64,4 +67,6 @@ def has_internet_connection():
urllib.request.urlopen('https://www.google.com/', timeout=1)
return True
except urllib.error.URLError as err:
return False
except socket.timeout as err:
return False
71 changes: 71 additions & 0 deletions controller/modules/home/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
from math import ceil

class Pagination:
def __init__(self, page, per_page, total_count):
self.page = page
self.per_page = per_page
self.total_count = total_count

@property
def pages(self):
return int(ceil(self.total_count / float(self.per_page)))

@property
def has_prev(self):
return self.page > 1

@property
def has_next(self):
return self.page < self.pages


@property
def next_num(self):
return self.page + 1

@property
def prev_num(self):
return self.page - 1


def iter_pages(self, left_edge=2, left_current=2,
right_current=5, right_edge=2):
last = 0
for num in range(1, self.pages + 1):
if num <= left_edge or \
(num > self.page - left_current - 1 and \
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num

def iter_pages_back(self, left_edge=2, left_current=2,
right_current=5, right_edge=2):
last = 0
for num in range(self.pages, 0, -1):
if num <= left_edge or \
(num > self.page - left_current - 1 and \
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num

def iter_pages_back_with_first(self, left_edge=2, left_current=2,
right_current=5, right_edge=2):
last = 0
for num in range(self.pages, 0, -1):
if num <= left_edge or \
(num > self.page - left_current - 1 and \
num < self.page + right_current) or \
num > self.pages - right_edge:
if last + 1 != num:
yield None
yield num
last = num
yield 1


Loading

0 comments on commit 7e18cae

Please sign in to comment.