Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 5fa4dbd

Browse files
authored
Merge pull request #14 from andrey18106/dev
MediaDC v0.1.3
2 parents 59bc7b5 + 51a03e8 commit 5fa4dbd

File tree

8 files changed

+120
-33
lines changed

8 files changed

+120
-33
lines changed

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ If applicable, add screenshots to help explain your problem.
2323
- OS: [e.g. iOS]
2424
- Browser [e.g. chrome, safari]
2525
- Nextcloud version [e.g. 22]
26+
- Database configuration (without sensitive information)
27+
- Python version [e.g. 3.9.1]
2628
- MediaDC version [e.g. 0.1.0]
2729

2830
**Smartphone (please complete the following information):**

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.1.3 - 2021-10-04]
6+
7+
### Fixed
8+
9+
- Now properly parse configs for database connect.
10+
511
## [0.1.2 - 2021-09-30]
612

713
### Added

appinfo/info.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This app allows to find duplicate or similar 📸📹 photos and videos
1717
Quick start guide and further information in our [Wiki](https://github.com/andrey18106/mediadc/wiki).
1818
]]>
1919
</description>
20-
<version>0.1.2</version>
20+
<version>0.1.3</version>
2121
<licence>agpl</licence>
2222
<author mail="[email protected]" homepage="https://github.com/andrey18106">Andrey Borysenko</author>
2323
<author mail="[email protected]" homepage="https://github.com/bigcat88">Alexander Piskun</author>
@@ -36,6 +36,7 @@ Quick start guide and further information in our [Wiki](https://github.com/andre
3636
<screenshot>https://raw.githubusercontent.com/andrey18106/mediadc/main/screenshots/admin-config.png</screenshot>
3737
<dependencies>
3838
<nextcloud min-version="21" max-version="22" />
39+
<php min-version="7.4" />
3940
<python min-version="3.6.8" />
4041
</dependencies>
4142
<settings>

lib/Service/python/db/connectors.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2929

3030

31-
def create_connection(config: dict, provider: str, error_out: list):
31+
def create_connection(config: dict, provider: str, error_out: list = None):
3232
connection = None
3333
try:
3434
if provider == 'pymysql':
@@ -41,7 +41,8 @@ def create_connection(config: dict, provider: str, error_out: list):
4141
charset='utf8mb4')
4242
else:
4343
port = int(config['dbport']) if len(config['dbport']) else 3306
44-
connection = pymysql.connect(host=config['dbhost'],
44+
host = config['dbhost'] if len(config['dbhost']) else "localhost"
45+
connection = pymysql.connect(host=host,
4546
port=port,
4647
user=config['dbuser'],
4748
password=config['dbpassword'],
@@ -56,7 +57,8 @@ def create_connection(config: dict, provider: str, error_out: list):
5657
database=config['dbname'])
5758
else:
5859
port = int(config['dbport']) if len(config['dbport']) else 5432
59-
connection = dbapi.connect(host=config['dbhost'],
60+
host = config['dbhost'] if len(config['dbhost']) else "localhost"
61+
connection = dbapi.connect(host=host,
6062
port=port,
6163
user=config['dbuser'],
6264
password=config['dbpassword'],
@@ -81,3 +83,18 @@ def internal_handle_db_connect_exception(exception_info, error_out: list = None)
8183
time.sleep(0.5)
8284
else:
8385
raise exception_info from None
86+
87+
88+
def test_connection(config: dict, provider: str, print_errors: bool = False) -> bool:
89+
errors_list = []
90+
try:
91+
connection = create_connection(config, provider, errors_list)
92+
if connection is not None:
93+
connection.close()
94+
return True
95+
except Exception as exception_info:
96+
if print_errors:
97+
print(exception_info)
98+
if print_errors:
99+
print(errors_list)
100+
return False

lib/Service/python/db/manager.py

Lines changed: 86 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import os
99
import re
1010
from . import occ_cloud
11-
from .connectors import create_connection, internal_handle_db_connect_exception
11+
from .connectors import create_connection, internal_handle_db_connect_exception, test_connection
1212

1313

1414
# @copyright Copyright (c) 2021 Andrey Borysenko <[email protected]>
@@ -61,52 +61,111 @@ def init_cloud_config(map_schema: dict) -> bool:
6161
return result
6262

6363

64-
def detect_msql_socket(config: dict, _php_info: str) -> None:
65-
global Warnings
66-
socket_path = config['dbhost'].split(":")[-1]
67-
if os.path.exists(socket_path):
68-
config['usock'] = socket_path
64+
def get_basic_db_provider(dbtype: str) -> str:
65+
if dbtype == 'mysql':
66+
return 'pymysql'
67+
if dbtype == 'pgsql':
68+
return 'pg8000'
69+
return ''
70+
71+
72+
def check_db_config(config: dict) -> None:
73+
"""Finding way to connect to database."""
74+
if test_connection(config, get_basic_db_provider(config['dbtype'])):
6975
return
70-
if not config['dbport'] and _php_info:
71-
m_groups = re.search(r'pdo_mysql\.default_socket\s*=>\s*(.*)\s*=>\s*(.*)',
72-
_php_info, flags=re.MULTILINE + re.IGNORECASE)
73-
if m_groups is None:
74-
Warnings.append("Cant parse php info.")
76+
# lets try without socket.
77+
if config['usock']:
78+
usock = config['usock']
79+
config['usock'] = ''
80+
if test_connection(config, get_basic_db_provider(config['dbtype'])):
7581
return
76-
socket_path = m_groups.groups()[-1].strip()
77-
if os.path.exists(socket_path):
78-
config['usock'] = socket_path
82+
config['usock'] = usock
7983

8084

81-
def detect_pgsql_socket(config: dict, _php_info: str) -> None:
82-
socket_path = config['dbhost'].split(":")[-1]
83-
if os.path.exists(socket_path):
84-
config['usock'] = socket_path
85+
def postprocess_config(config: dict) -> None:
86+
"""Parses 'dbhost' value. Possible values: host:port, host:socket, host, socket, nil"""
87+
global Warnings
88+
host_port_socket = config['dbhost'].split(":", maxsplit=1)
89+
if len(host_port_socket) > 1:
90+
# when dbhost = host:port or host:socket
91+
config['dbhost'] = host_port_socket[0]
92+
if os.path.exists(host_port_socket[1]):
93+
config['usock'] = host_port_socket[1]
94+
elif host_port_socket[1].isdigit():
95+
config['dbport'] = host_port_socket[1]
96+
else:
97+
Warnings.append("Unknown socket or port value.")
98+
elif os.path.exists(config['dbhost']):
99+
# when dbhost = socket
100+
config['usock'] = config['dbhost']
101+
config['dbhost'] = ''
102+
if config['dbtype'] == 'pgsql':
103+
# Don't know currently how to handle this situation properly. Using default port value for socket name.
104+
if config['usock']:
105+
config['usock'] += '.s.PGSQL.5432'
106+
107+
108+
def find_db_configuration() -> bool:
109+
"""Finding working way to connect to database, if will found global `Config` will be changed according to it."""
110+
global Warnings, Config
111+
# Trying what we parsed from cloud config.
112+
if test_connection(Config, get_basic_db_provider(Config['dbtype'])):
113+
return True
114+
# Trying without socket.
115+
if Config['usock']:
116+
usock = Config['usock']
117+
Config['usock'] = ''
118+
if test_connection(Config, get_basic_db_provider(Config['dbtype'])):
119+
return True
120+
Config['usock'] = usock
121+
if Config['dbtype'] == 'mysql':
122+
# when no dbport or usock found in cloud config, trying php socket configuration.
123+
php_ok, php_info = occ_cloud.php_call('-r', "phpinfo();")
124+
if not php_ok:
125+
Warnings.append("Cant get php info.")
126+
else:
127+
m_groups = re.search(r'pdo_mysql\.default_socket\s*=>\s*(.*)\s*=>\s*(.*)',
128+
php_info, flags=re.MULTILINE + re.IGNORECASE)
129+
if m_groups is None:
130+
Warnings.append("Cant parse php info.")
131+
else:
132+
socket_path = m_groups.groups()[-1].strip()
133+
if os.path.exists(socket_path):
134+
usock = Config['usock']
135+
Config['usock'] = socket_path
136+
if test_connection(Config, get_basic_db_provider(Config['dbtype'])):
137+
return True
138+
Config['usock'] = usock
139+
# If we got here then all is not so good, as it can be. Last try with default host and port.
140+
host = Config['dbhost']
141+
port = Config['dbport']
142+
Config['dbhost'] = ''
143+
Config['dbport'] = ''
144+
if test_connection(Config, get_basic_db_provider(Config['dbtype'])):
145+
return True
146+
Config['dbhost'] = host
147+
Config['dbport'] = port
148+
return False
85149

86150

87151
def init():
88-
global Config, DatabaseProvider, Warnings
152+
global Config, DatabaseProvider
89153
map_schema = {'datadir': 'datadirectory',
90154
'dbname': 'dbname',
91155
'dbtprefix': 'dbtableprefix',
92156
'dbuser': 'dbuser',
93157
'dbpassword': 'dbpassword',
94158
'dbhost': 'dbhost',
95-
'dbport': 'dbport',
96159
'dbtype': 'dbtype'}
97160
Config['usock'] = ''
161+
Config['dbport'] = ''
98162
if not init_cloud_config(map_schema):
99163
return DbType.UNKNOWN
100-
php_ok, php_info = occ_cloud.php_call('-r', "phpinfo();")
101-
if not php_ok:
102-
php_info = ''
103-
Warnings.append("Cant get php info.")
164+
postprocess_config(Config)
104165
if Config['dbtype'] == 'mysql':
105-
detect_msql_socket(Config, php_info)
106166
DatabaseProvider = 'pymysql'
107167
return DbType.MYSQL
108168
if Config['dbtype'] == 'pgsql':
109-
detect_pgsql_socket(Config, php_info)
110169
DatabaseProvider = 'pg8000'
111170
return DbType.PGSQL
112171
if Config['dbtype'] == 'oci':
@@ -238,6 +297,7 @@ def check_db() -> list:
238297
return Errors
239298
if not DatabaseProvider:
240299
return [f"Unsupported DB type:`{Config['dbtype']}`"]
300+
find_db_configuration()
241301
ret = []
242302
success = False
243303
try:

lib/Service/python/install.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ def check() -> dict:
354354
if not ret['required']:
355355
log_error(*db.check_db())
356356
ret['errors'] = ErrorsContainer
357+
ret['warnings'] = db.get_warnings()
357358
return ret
358359

359360

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mediadc",
33
"description": "Collect photo and video duplicates to save your cloud storage",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"author": "Andrey Borysenko <[email protected]>",
66
"contributors": [
77
"Andrey Borysenko <[email protected]>",

0 commit comments

Comments
 (0)