Compare commits
269 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aadf43ecaf | ||
|
|
a361bc6d5e | ||
|
|
331544c1de | ||
|
|
b51d8a4513 | ||
|
|
dbfb7a85a4 | ||
|
|
3084d255f4 | ||
|
|
1fe0a8a75e | ||
|
|
003139b29a | ||
|
|
f0c0005c28 | ||
|
|
4cbc208aa7 | ||
|
|
e8a73a5e85 | ||
|
|
41f76d784f | ||
|
|
0c551be9d8 | ||
|
|
7290d84243 | ||
|
|
09f4c898dd | ||
|
|
aec3cd573a | ||
|
|
dc2bf2902a | ||
|
|
c6f16dba0c | ||
|
|
4b796a480a | ||
|
|
5553402b6b | ||
|
|
4741293b79 | ||
|
|
9e3f0fff9a | ||
|
|
9c1ffe79fe | ||
|
|
728f4964f3 | ||
|
|
4b52ecbdfc | ||
|
|
b5fb89b17f | ||
|
|
1e225d8d24 | ||
|
|
b60105b3c6 | ||
|
|
f928e1408b | ||
|
|
71a683f67b | ||
|
|
c3e38ded21 | ||
|
|
c84e653629 | ||
|
|
2dd191a8ca | ||
|
|
9c18d71f3e | ||
|
|
9f7dbb3c03 | ||
|
|
7c6537355c | ||
|
|
c8ed7eff7e | ||
|
|
3943bf3dc6 | ||
|
|
0714b93dc9 | ||
|
|
d391709be6 | ||
|
|
adb9d5242e | ||
|
|
b72e493675 | ||
|
|
907c460d97 | ||
|
|
98572919b2 | ||
|
|
febd844c91 | ||
|
|
bda3c61f7f | ||
|
|
254734bdc1 | ||
|
|
829281b616 | ||
|
|
dff86d9232 | ||
|
|
50668efc1b | ||
|
|
845634e8f1 | ||
|
|
9ad2253972 | ||
|
|
a155c3a442 | ||
|
|
4ac9622374 | ||
|
|
c59e2bb3b9 | ||
|
|
e6bb583601 | ||
|
|
a7f1b0c906 | ||
|
|
1607961543 | ||
|
|
e1d2e5e4d9 | ||
|
|
a30eb683d6 | ||
|
|
e454d46e49 | ||
|
|
067f631c6c | ||
|
|
3b36854a21 | ||
|
|
421d8afa36 | ||
|
|
5ad8ccdf2f | ||
|
|
313152ed92 | ||
|
|
e5c6578264 | ||
|
|
9e7ff7fff7 | ||
|
|
f0f57cf093 | ||
|
|
30db4523f4 | ||
|
|
894b4b37e7 | ||
|
|
17832f1ac1 | ||
|
|
1adbb294aa | ||
|
|
804ab650d7 | ||
|
|
efd5dd9bd5 | ||
|
|
af13edbf0c | ||
|
|
bb79429079 | ||
|
|
aa1debcba3 | ||
|
|
dd757c2aca | ||
|
|
aeb53708de | ||
|
|
aac4937f35 | ||
|
|
d0a58d5d08 | ||
|
|
aa54385ce2 | ||
|
|
36d0e4ac6a | ||
|
|
db5f811ae4 | ||
|
|
a056596325 | ||
|
|
d8edf39aab | ||
|
|
33d0b6be0d | ||
|
|
7b6bc83953 | ||
|
|
ec71b8a43a | ||
|
|
84ee089e3d | ||
|
|
6a330a7215 | ||
|
|
d6bb68ec5d | ||
|
|
d40bfe99e1 | ||
|
|
3cad6c4735 | ||
|
|
b236a0816f | ||
|
|
fe727ed1b4 | ||
|
|
538e3cf5c7 | ||
|
|
00548b99ff | ||
|
|
4459cd400f | ||
|
|
02bd858756 | ||
|
|
0d71453f8b | ||
|
|
ef67809f38 | ||
|
|
e9bdc5e334 | ||
|
|
9332bb52e6 | ||
|
|
4c5f0a00af | ||
|
|
849b623a04 | ||
|
|
b1194462cc | ||
|
|
412d07c4ab | ||
|
|
26b4a90870 | ||
|
|
a45dcc1b6b | ||
|
|
4b7dff9c79 | ||
|
|
8c9e530396 | ||
|
|
c23d08449f | ||
|
|
f039112f27 | ||
|
|
ebba55cbea | ||
|
|
d63ef87223 | ||
|
|
e60e4a9e05 | ||
|
|
4fe5f87c43 | ||
|
|
6ca1b92b0b | ||
|
|
6a085ba387 | ||
|
|
3c55c84f8d | ||
|
|
7fa0d97187 | ||
|
|
a592d42db1 | ||
|
|
6f4eefe600 | ||
|
|
6f667d96c2 | ||
|
|
8555aa5263 | ||
|
|
5ae8050b55 | ||
|
|
4e2308e9b5 | ||
|
|
cc71b347fb | ||
|
|
8478627095 | ||
|
|
a4715789a2 | ||
|
|
66403abc01 | ||
|
|
ad3e072c1c | ||
|
|
30c8f83c9f | ||
|
|
3c6a4f722e | ||
|
|
0f137755b5 | ||
|
|
1c9bc7fd5f | ||
|
|
d632cb75a7 | ||
|
|
68ab7f1696 | ||
|
|
c9380ec484 | ||
|
|
b8cb4a0116 | ||
|
|
f543365ab2 | ||
|
|
eb77dee91c | ||
|
|
23f823d96f | ||
|
|
ae4a1abdbe | ||
|
|
c998435312 | ||
|
|
9d58df9b89 | ||
|
|
4f0b299923 | ||
|
|
8719820f69 | ||
|
|
2e103cbdee | ||
|
|
8443f59c4e | ||
|
|
79ff66093f | ||
|
|
1190798093 | ||
|
|
3a245b695b | ||
|
|
2c529ae117 | ||
|
|
1396e85a3e | ||
|
|
fa8cc19750 | ||
|
|
d5f0a7601f | ||
|
|
f96b2c827c | ||
|
|
15da9dbc08 | ||
|
|
10253c7021 | ||
|
|
30eb4cd66d | ||
|
|
200d3ba26c | ||
|
|
1ece55d6a0 | ||
|
|
a63019fd2d | ||
|
|
5b6987227a | ||
|
|
6447e5eb79 | ||
|
|
5bd6de3875 | ||
|
|
a1dc295b68 | ||
|
|
dd390280fb | ||
|
|
ce510d8d21 | ||
|
|
d1b7867b37 | ||
|
|
8498414fb0 | ||
|
|
e2162300f0 | ||
|
|
d4512eef97 | ||
|
|
cb71b09aad | ||
|
|
d982c64aa7 | ||
|
|
5a541b78f1 | ||
|
|
7f8692ee29 | ||
|
|
716bb0dbce | ||
|
|
a5414ed424 | ||
|
|
67e0d0c8a5 | ||
|
|
85caccc899 | ||
|
|
060cccdcf9 | ||
|
|
abbd6ceb48 | ||
|
|
f02e6b8e5a | ||
|
|
a30f602b65 | ||
|
|
06e20737d0 | ||
|
|
bd335509ff | ||
|
|
6ad6662e01 | ||
|
|
a632cf3a2f | ||
|
|
0e09b8db39 | ||
|
|
04ef6d63bb | ||
|
|
aa5d1af299 | ||
|
|
c4ab287ba7 | ||
|
|
5a16c6f800 | ||
|
|
a3a74d23c4 | ||
|
|
6a05fadd45 | ||
|
|
e182597d8f | ||
|
|
4bc22a94eb | ||
|
|
a5afe90267 | ||
|
|
a3e1e62082 | ||
|
|
b90d24e5de | ||
|
|
39fe7b58a4 | ||
|
|
217a0294ac | ||
|
|
2c21d76245 | ||
|
|
83de634df1 | ||
|
|
7861eeeb07 | ||
|
|
9f5baf2aa1 | ||
|
|
8de5223dfc | ||
|
|
64200cc48f | ||
|
|
8ff4c8360f | ||
|
|
13fe2ec7ed | ||
|
|
57bae97ad7 | ||
|
|
c4df0914d9 | ||
|
|
9dd9fe9d9c | ||
|
|
75dbef8196 | ||
|
|
5f53228113 | ||
|
|
24114c1281 | ||
|
|
9702908b4d | ||
|
|
4e96d4f48e | ||
|
|
8f3be09c9a | ||
|
|
8360f6f4f7 | ||
|
|
e9c7412227 | ||
|
|
c105921694 | ||
|
|
20f6283fe4 | ||
|
|
16bdafd44b | ||
|
|
b10e46554f | ||
|
|
334ba3f178 | ||
|
|
2fcd62df7c | ||
|
|
4166d08be1 | ||
|
|
60b70082ba | ||
|
|
c02eb1d02c | ||
|
|
69a802af55 | ||
|
|
14654b1033 | ||
|
|
1265b35d8f | ||
|
|
f5e2ab423d | ||
|
|
05c6407298 | ||
|
|
0d2ac63c61 | ||
|
|
1bd6f39650 | ||
|
|
5478728253 | ||
|
|
4ed8b57674 | ||
|
|
a2500d45d7 | ||
|
|
d7306d3fe9 | ||
|
|
20dc64834a | ||
|
|
16faf90989 | ||
|
|
f320d28f37 | ||
|
|
1a1bff7287 | ||
|
|
9aedb0280b | ||
|
|
75e0d16ac5 | ||
|
|
b174012e5a | ||
|
|
c2ab9251c6 | ||
|
|
d0b2601f80 | ||
|
|
f0e431ff7c | ||
|
|
29838b1028 | ||
|
|
d98dfadf6c | ||
|
|
24a2811533 | ||
|
|
8ba031efe5 | ||
|
|
bec1c9b3b6 | ||
|
|
899d2233af | ||
|
|
e5b5b80b53 | ||
|
|
3dbf7b2342 | ||
|
|
d439d026fe | ||
|
|
010f406203 | ||
|
|
9437d11674 | ||
|
|
5d7181aecc | ||
|
|
c4b8d78280 | ||
|
|
b9299098c8 |
2
.gitattributes
vendored
Normal file
2
.gitattributes
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
**/Resources/Lang/*.resx linguist-generated=true
|
||||
**/wwwroot/js/lang/*.js linguist-generated=true
|
||||
2
.github/FUNDING.yml
vendored
Normal file
2
.github/FUNDING.yml
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
github: [cirx08]
|
||||
buy_me_a_coffee: cirx08
|
||||
39
.github/ISSUE_TEMPLATE/bug-report.md
vendored
Normal file
39
.github/ISSUE_TEMPLATE/bug-report.md
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
---
|
||||
name: Bug Report
|
||||
about: Create a report to help us improve
|
||||
title: "[Bug] - "
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Description:**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**Expected Behaviour:**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Reproduction Steps:**
|
||||
Steps to reproduce the behaviour:
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Cache Cleared:**
|
||||
If the issue is a front end issue please confirm you have first attempted to clear the cache and the issue still persists.
|
||||
|
||||
**Docker Compose:**
|
||||
If possible please provide your compose setup or a list of environment variables to help debug the issue.
|
||||
|
||||
**Device:**
|
||||
Pleases tell me some more details about the device you are having the issue on. Is the issue isolated to the device or can you reproduce on multiple devices?
|
||||
|
||||
- Desktop: [e.g. 1080p Chrome, 2K Safari, 4K Brave]
|
||||
- Mobile [e.g. iPhone 16, Samsung S24 Ultra]
|
||||
|
||||
**Screenshots:**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Additional Details:**
|
||||
Add any other context about the problem here.
|
||||
17
.github/ISSUE_TEMPLATE/feature-request.md
vendored
Normal file
17
.github/ISSUE_TEMPLATE/feature-request.md
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
---
|
||||
name: Feature Request
|
||||
about: Suggest an idea or enhancement for this project
|
||||
title: "[Feature] "
|
||||
labels: enhancement, feature
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Description:**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Examples:**
|
||||
A clear and concise description of any alternative solutions you've used that have similar functionality.
|
||||
|
||||
**Additional Details:**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
14
.github/ISSUE_TEMPLATE/translation-request.md
vendored
Normal file
14
.github/ISSUE_TEMPLATE/translation-request.md
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
---
|
||||
name: Translation Request
|
||||
about: Create a report to help us improve
|
||||
title: "[Translations] - "
|
||||
labels: translations
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Description:**
|
||||
A clear and concise description of the changes being made.
|
||||
|
||||
**Pull Request:**
|
||||
A link to the pull request containing the updates to BOTH languages resource as specified by the documentation - `https://docs.wedding-share.org/docs/languages#addingupdating-languages`.
|
||||
15
.gitignore
vendored
15
.gitignore
vendored
@@ -1,4 +1,4 @@
|
||||
################################################################################
|
||||
################################################################################
|
||||
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
|
||||
################################################################################
|
||||
|
||||
@@ -14,3 +14,16 @@
|
||||
/WeddingShare/wwwroot/uploads/*
|
||||
!/WeddingShare/wwwroot/uploads/default
|
||||
/WeddingShare/wwwroot/uploads/default/*
|
||||
/WeddingShare/wwwroot/custom_resources/*
|
||||
/release notes
|
||||
/WeddingShare/weddingshare.db
|
||||
/WeddingShare/wedding-share.db
|
||||
/WeddingShare/config/wedding-share.db
|
||||
/WeddingShare/wwwroot/thumbnails
|
||||
/WeddingShare/wwwroot/temp
|
||||
/WeddingShare/ffmpeg
|
||||
/WeddingShare/wwwroot/logos
|
||||
/WeddingShare/wwwroot/banners
|
||||
/WeddingShare/wwwroot/images/custom_resources
|
||||
/WeddingShare/config/wedding-share.db.bak
|
||||
/WeddingShare/config/key-*.xml
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
image: mcr.microsoft.com/dotnet/sdk:latest
|
||||
|
||||
stages:
|
||||
- build
|
||||
- test
|
||||
- push
|
||||
- release
|
||||
|
||||
variables:
|
||||
BUILD_DOCKERFILE: 'WeddingShare/Dockerfile'
|
||||
BUILD_CONFIGURATION: 'Release'
|
||||
BUILD_PLATFORMS: 'linux/arm/v7,linux/arm64/v8,linux/amd64'
|
||||
BUILD_BUILDER_NAME: 'wedding-share-builder'
|
||||
OBJECTS_DIRECTORY: 'obj'
|
||||
NUGET_PACKAGES_DIRECTORY: '.nuget'
|
||||
SOURCE_CODE_PATH: '*/*/'
|
||||
@@ -21,14 +24,27 @@ cache:
|
||||
|
||||
before_script:
|
||||
- 'docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY'
|
||||
# - 'docker buildx rm --all-inactive --force'
|
||||
# - 'docker buildx create --name $BUILD_BUILDER_NAME --driver=docker-container'
|
||||
|
||||
build:
|
||||
stage: build
|
||||
test:
|
||||
stage: test
|
||||
script:
|
||||
- 'docker build -t $CI_REGISTRY_IMAGE:dev -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA -f WeddingShare/Dockerfile .'
|
||||
- 'docker push $CI_REGISTRY_IMAGE:dev'
|
||||
- 'docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA'
|
||||
- 'dotnet restore'
|
||||
- 'dotnet test'
|
||||
|
||||
push_release:
|
||||
variables:
|
||||
GIT_STRATEGY: none
|
||||
stage: push
|
||||
only:
|
||||
- /^(feature\/|release\/).+/
|
||||
- /^(prerel|rc|release)-.+/
|
||||
script:
|
||||
- 'docker buildx build --tag $CI_REGISTRY_IMAGE:pre_release --platform $BUILD_PLATFORMS --builder $BUILD_BUILDER_NAME --push -f $BUILD_DOCKERFILE .'
|
||||
needs:
|
||||
- test
|
||||
|
||||
push_latest:
|
||||
variables:
|
||||
GIT_STRATEGY: none
|
||||
@@ -37,9 +53,9 @@ push_latest:
|
||||
- main
|
||||
- master
|
||||
script:
|
||||
- 'docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA'
|
||||
- 'docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $CI_REGISTRY_IMAGE:latest'
|
||||
- 'docker push $CI_REGISTRY_IMAGE:latest'
|
||||
- 'docker buildx build --tag $CI_REGISTRY_IMAGE:latest --platform $BUILD_PLATFORMS --builder $BUILD_BUILDER_NAME --push -f $BUILD_DOCKERFILE .'
|
||||
needs:
|
||||
- test
|
||||
|
||||
push_tag:
|
||||
variables:
|
||||
@@ -48,20 +64,18 @@ push_tag:
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- 'docker pull $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA'
|
||||
- 'docker tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHORT_SHA $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME'
|
||||
- 'docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME'
|
||||
- 'docker buildx build --tag $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME --platform $BUILD_PLATFORMS --builder $BUILD_BUILDER_NAME --push -f $BUILD_DOCKERFILE .'
|
||||
needs:
|
||||
- test
|
||||
|
||||
push_docker_hub:
|
||||
variables:
|
||||
GIT_STRATEGY: none
|
||||
stage: push
|
||||
stage: release
|
||||
only:
|
||||
- tags
|
||||
script:
|
||||
- 'docker pull $CI_REGISTRY_IMAGE:latest'
|
||||
- 'docker login -u $DOCKERHUB_USERNAME -p $DOCKERHUB_TOKEN'
|
||||
- 'docker tag $CI_REGISTRY_IMAGE:latest $DOCKERHUB_USERNAME/wedding_share:latest'
|
||||
- 'docker push $DOCKERHUB_USERNAME/wedding_share:latest'
|
||||
- 'docker tag $CI_REGISTRY_IMAGE:latest $DOCKERHUB_USERNAME/wedding_share:$CI_COMMIT_REF_NAME'
|
||||
- 'docker push $DOCKERHUB_USERNAME/wedding_share:$CI_COMMIT_REF_NAME'
|
||||
- 'docker buildx build --tag $DOCKERHUB_USERNAME/wedding_share:$CI_COMMIT_REF_NAME --tag $DOCKERHUB_USERNAME/wedding_share:latest --platform $BUILD_PLATFORMS --builder $BUILD_BUILDER_NAME --push -f $BUILD_DOCKERFILE .'
|
||||
needs:
|
||||
- push_tag
|
||||
674
LICENSE
Normal file
674
LICENSE
Normal file
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
2
WeddingShare.UnitTests/GlobalUsings.cs
Normal file
2
WeddingShare.UnitTests/GlobalUsings.cs
Normal file
@@ -0,0 +1,2 @@
|
||||
global using NUnit.Framework;
|
||||
global using NSubstitute;
|
||||
14
WeddingShare.UnitTests/Helpers/ConfigurationHelper.cs
Normal file
14
WeddingShare.UnitTests/Helpers/ConfigurationHelper.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace WeddingShare.UnitTests.Helpers
|
||||
{
|
||||
internal class ConfigurationHelper
|
||||
{
|
||||
public static IConfiguration MockConfiguration(IDictionary<string, string?> settings)
|
||||
{
|
||||
return new ConfigurationBuilder()
|
||||
.AddInMemoryCollection(settings ?? new Dictionary<string, string?>())
|
||||
.Build();
|
||||
}
|
||||
}
|
||||
}
|
||||
23
WeddingShare.UnitTests/Helpers/JsonResponseHelper.cs
Normal file
23
WeddingShare.UnitTests/Helpers/JsonResponseHelper.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
namespace WeddingShare.UnitTests.Helpers
|
||||
{
|
||||
internal class JsonResponseHelper
|
||||
{
|
||||
public static T GetPropertyValue<T>(object? obj, string propertyName, T defaultValue)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var val = obj?.GetType()?.GetProperty(propertyName)?.GetValue(obj, null);
|
||||
if (val != null)
|
||||
{
|
||||
return (T)val;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
74
WeddingShare.UnitTests/Helpers/MockData.cs
Normal file
74
WeddingShare.UnitTests/Helpers/MockData.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.UnitTests.Helpers
|
||||
{
|
||||
internal class MockData
|
||||
{
|
||||
public static DefaultHttpContext MockHttpContext(Dictionary<string, StringValues>? form = null, IFormFileCollection? files = null, MockSession session = null)
|
||||
{
|
||||
var ctx = new DefaultHttpContext()
|
||||
{
|
||||
Session = session ?? new MockSession()
|
||||
};
|
||||
|
||||
ctx.Request.Form = new FormCollection(form, files);
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
public static List<GalleryItemModel> MockGalleryItems(int count = 10, int? galleryId = null, GalleryItemState state = GalleryItemState.All)
|
||||
{
|
||||
var result = new List<GalleryItemModel>();
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
result.Add(MockGalleryItem(galleryId, state));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static GalleryItemModel MockGalleryItem(int? galleryId = null, GalleryItemState state = GalleryItemState.All)
|
||||
{
|
||||
var rand = new Random();
|
||||
|
||||
return new GalleryItemModel()
|
||||
{
|
||||
Id = rand.Next(),
|
||||
GalleryId = galleryId != null ? (int)galleryId : rand.Next(),
|
||||
Title = $"{Guid.NewGuid()}.{MockFileExtension()}",
|
||||
UploadedBy = rand.Next(2) % 2 == 0 ? Guid.NewGuid().ToString() : null,
|
||||
MediaType = (MediaType)rand.Next(3),
|
||||
State = state == GalleryItemState.All ? (GalleryItemState)rand.Next(2) : state,
|
||||
FileSize = (int)rand.Next(2),
|
||||
};
|
||||
}
|
||||
|
||||
public static string MockFileExtension()
|
||||
{
|
||||
var rand = new Random();
|
||||
|
||||
string extension;
|
||||
switch (rand.Next(4))
|
||||
{
|
||||
case 0:
|
||||
extension = "jpg";
|
||||
break;
|
||||
case 1:
|
||||
extension = "jpeg";
|
||||
break;
|
||||
case 2:
|
||||
extension = "png";
|
||||
break;
|
||||
default:
|
||||
extension = "ffff";
|
||||
break;
|
||||
}
|
||||
|
||||
return rand.Next(2) % 2 == 0 ? extension.ToUpper() : extension.ToLower();
|
||||
}
|
||||
}
|
||||
}
|
||||
26
WeddingShare.UnitTests/Helpers/MockHttpMessageHandler.cs
Normal file
26
WeddingShare.UnitTests/Helpers/MockHttpMessageHandler.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace WeddingShare.UnitTests.Helpers
|
||||
{
|
||||
public class MockHttpMessageHandler : HttpMessageHandler
|
||||
{
|
||||
private readonly HttpStatusCode _statusCode;
|
||||
private readonly object? _responseContent;
|
||||
|
||||
public MockHttpMessageHandler(HttpStatusCode statusCode, object? responseContent = null)
|
||||
{
|
||||
_statusCode = statusCode;
|
||||
_responseContent = responseContent;
|
||||
}
|
||||
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await Task.FromResult(new HttpResponseMessage
|
||||
{
|
||||
StatusCode = _statusCode,
|
||||
Content = JsonContent.Create(_responseContent)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
65
WeddingShare.UnitTests/Helpers/MockSession.cs
Normal file
65
WeddingShare.UnitTests/Helpers/MockSession.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace WeddingShare.UnitTests.Helpers
|
||||
{
|
||||
internal class MockSession : ISession
|
||||
{
|
||||
private IDictionary<string, string> Values = new Dictionary<string, string>();
|
||||
|
||||
public bool IsAvailable => throw new NotImplementedException();
|
||||
|
||||
public string Id => throw new NotImplementedException();
|
||||
|
||||
public IEnumerable<string> Keys => throw new NotImplementedException();
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
this.Values.Clear();
|
||||
}
|
||||
|
||||
public Task CommitAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task LoadAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Remove(string key)
|
||||
{
|
||||
this.Values.Remove(key);
|
||||
}
|
||||
|
||||
public void Set(string key, string value)
|
||||
{
|
||||
this.Set(key, Encoding.UTF8.GetBytes(value));
|
||||
}
|
||||
|
||||
public void Set(string key, byte[] value)
|
||||
{
|
||||
if (this.Values.ContainsKey(key))
|
||||
{
|
||||
this.Values.Remove(key);
|
||||
}
|
||||
|
||||
this.Values.Add(key, Encoding.UTF8.GetString(value));
|
||||
}
|
||||
|
||||
public bool TryGetValue(string key, [NotNullWhen(true)] out byte[]? value)
|
||||
{
|
||||
if (this.Values.ContainsKey(key))
|
||||
{
|
||||
value = Encoding.UTF8.GetBytes(this.Values[key]);
|
||||
return true;
|
||||
}
|
||||
|
||||
value = null;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,431 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using NSubstitute.ReturnsExtensions;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Controllers;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
using WeddingShare.Models;
|
||||
using WeddingShare.Models.Database;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class GalleryControllerTests
|
||||
{
|
||||
private readonly IWebHostEnvironment _env = Substitute.For<IWebHostEnvironment>();
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
private readonly IDatabaseHelper _database = Substitute.For<IDatabaseHelper>();
|
||||
private readonly IFileHelper _file = Substitute.For<IFileHelper>();
|
||||
private readonly IDeviceDetector _deviceDetector = Substitute.For<IDeviceDetector>();
|
||||
private readonly IImageHelper _image = Substitute.For<IImageHelper>();
|
||||
private readonly INotificationHelper _notification = Substitute.For<INotificationHelper>();
|
||||
private readonly IEncryptionHelper _encryption = Substitute.For<IEncryptionHelper>();
|
||||
private readonly WeddingShare.Helpers.IUrlHelper _url = Substitute.For<WeddingShare.Helpers.IUrlHelper>();
|
||||
private readonly ILogger<GalleryController> _logger = Substitute.For<ILogger<GalleryController>>();
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer = Substitute.For<IStringLocalizer<Lang.Translations>>();
|
||||
|
||||
public GalleryControllerTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_env.WebRootPath.Returns("/app/wwwroot");
|
||||
|
||||
var mockData = GetMockData();
|
||||
|
||||
_database.GetGallery(1).Returns(Task.FromResult<GalleryModel?>(mockData["default"]));
|
||||
_database.GetGallery(2).Returns(Task.FromResult<GalleryModel?>(mockData["blaa"]));
|
||||
_database.GetGallery(3).Returns(Task.FromResult<GalleryModel?>(null));
|
||||
|
||||
_database.GetGalleryId("default").Returns(Task.FromResult<int?>(mockData["default"].Id));
|
||||
_database.GetGalleryId("blaa").Returns(Task.FromResult<int?>(mockData["blaa"].Id));
|
||||
_database.GetGalleryId("missing").Returns(Task.FromResult<int?>(null));
|
||||
|
||||
_database.GetGalleryIdByName("default").Returns(Task.FromResult<int?>(mockData["default"].Id));
|
||||
_database.GetGalleryIdByName("blaa").Returns(Task.FromResult<int?>(mockData["blaa"].Id));
|
||||
_database.GetGalleryIdByName("missing").Returns(Task.FromResult<int?>(null));
|
||||
|
||||
_database.AddGallery(Arg.Any<GalleryModel>()).Returns(Task.FromResult<GalleryModel?>(new GalleryModel()
|
||||
{
|
||||
Id = 101,
|
||||
Name = "missing",
|
||||
SecretKey = "123456",
|
||||
ApprovedItems = 0,
|
||||
PendingItems = 0,
|
||||
TotalItems = 0,
|
||||
Owner = 0
|
||||
}));
|
||||
_database.AddGalleryItem(Arg.Any<GalleryItemModel>()).Returns(Task.FromResult<GalleryItemModel?>(MockData.MockGalleryItem()));
|
||||
|
||||
_database.GetAllGalleryItems(Arg.Any<int>(), GalleryItemState.All, Arg.Any<MediaType>(), Arg.Any<ImageOrientation>(), Arg.Any<GalleryGroup>(), Arg.Any<GalleryOrder>(), Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(MockData.MockGalleryItems(10, 1, GalleryItemState.All)));
|
||||
_database.GetAllGalleryItems(Arg.Any<int>(), GalleryItemState.Pending, Arg.Any<MediaType>(), Arg.Any<ImageOrientation>(), Arg.Any<GalleryGroup>(), Arg.Any<GalleryOrder>(), Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(MockData.MockGalleryItems(10, 1, GalleryItemState.Pending)));
|
||||
_database.GetAllGalleryItems(Arg.Any<int>(), GalleryItemState.Approved, Arg.Any<MediaType>(), Arg.Any<ImageOrientation>(), Arg.Any<GalleryGroup>(), Arg.Any<GalleryOrder>(), Arg.Any<int>(), Arg.Any<int>()).Returns(Task.FromResult(MockData.MockGalleryItems(10, 1, GalleryItemState.Approved)));
|
||||
_database.GetGalleryItemByChecksum(Arg.Any<int>(), Arg.Any<string>()).ReturnsNull();
|
||||
|
||||
_settings.GetOrDefault(Settings.Gallery.Upload, Arg.Any<bool>(), Arg.Any<int>()).Returns(true);
|
||||
_settings.GetOrDefault(Settings.Gallery.Download, Arg.Any<bool>(), Arg.Any<int>()).Returns(true);
|
||||
_settings.GetOrDefault(Settings.Gallery.UploadPeriod, Arg.Any<string>(), Arg.Any<int>()).Returns("1970-01-01 00:00:00");
|
||||
_settings.GetOrDefault(Settings.Gallery.PreventDuplicates, Arg.Any<bool>(), Arg.Any<int>()).Returns(true);
|
||||
_settings.GetOrDefault(Settings.Gallery.DefaultView, Arg.Any<int>(), Arg.Any<int>()).Returns((int)ViewMode.Default);
|
||||
_settings.GetOrDefault(Settings.Gallery.AllowedFileTypes, Arg.Any<string>(), Arg.Any<int>()).Returns(".jpg,.jpeg,.png,.mp4,.mov");
|
||||
_settings.GetOrDefault(Settings.Gallery.RequireReview, Arg.Any<bool>(), Arg.Any<int>()).Returns(true);
|
||||
_settings.GetOrDefault(Settings.Gallery.MaxFileSizeMB, Arg.Any<int>(), Arg.Any<int>()).Returns(10);
|
||||
|
||||
_file.GetChecksum(Arg.Any<string>()).Returns(Guid.NewGuid().ToString());
|
||||
|
||||
_notification.Send(Arg.Any<string>(), Arg.Any<string>()).Returns(Task.FromResult(true));
|
||||
|
||||
_localizer[Arg.Any<string>()].Returns(new LocalizedString("UnitTest", "UnitTest"));
|
||||
}
|
||||
|
||||
[TestCase(DeviceType.Desktop, 1, "default", "default", "password", ViewMode.Default, GalleryGroup.None, GalleryOrder.Descending, true)]
|
||||
[TestCase(DeviceType.Mobile, 2, "blaa", "blaa", "456789", ViewMode.Presentation, GalleryGroup.Date, GalleryOrder.Ascending, true)]
|
||||
[TestCase(DeviceType.Tablet, 101, "missing", "missing", "123456", ViewMode.Slideshow, GalleryGroup.Uploader, GalleryOrder.Ascending, false)]
|
||||
public async Task GalleryController_Index(DeviceType deviceType, int id, string? identifier, string? name, string? key, ViewMode? mode, GalleryGroup group, GalleryOrder order, bool existing)
|
||||
{
|
||||
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(deviceType);
|
||||
_settings.GetOrDefault(Settings.Basic.SingleGalleryMode, Arg.Any<bool>()).Returns(false);
|
||||
_settings.GetOrDefault(Settings.Basic.GuestGalleryCreation, Arg.Any<bool>()).Returns(false);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext();
|
||||
|
||||
if (existing)
|
||||
{
|
||||
ViewResult actual = (ViewResult)await controller.Index(identifier, name, key, mode, group, order);
|
||||
Assert.That(actual, Is.TypeOf<ViewResult>());
|
||||
Assert.That(actual?.Model, Is.Not.Null);
|
||||
|
||||
PhotoGallery model = (PhotoGallery)actual.Model;
|
||||
Assert.That(model?.Gallery?.Id, Is.EqualTo(id));
|
||||
Assert.That(model?.Gallery?.Identifier, Is.EqualTo(identifier));
|
||||
Assert.That(model?.Gallery?.Name, Is.EqualTo(name));
|
||||
Assert.That(model?.SecretKey, Is.EqualTo(key));
|
||||
Assert.That(model.ViewMode, Is.EqualTo(mode));
|
||||
}
|
||||
else
|
||||
{
|
||||
RedirectToActionResult actual = (RedirectToActionResult)await controller.Index(identifier, name, key, mode, group, order);
|
||||
Assert.That(actual, Is.TypeOf<RedirectToActionResult>());
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase(null, "default", "default")]
|
||||
[TestCase("default", null, "default")]
|
||||
[TestCase("default", "blaa", "blaa")]
|
||||
public async Task GalleryController_Index_GetByIdentifier(string? id, string? identifier, string expected)
|
||||
{
|
||||
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(DeviceType.Desktop);
|
||||
_settings.GetOrDefault(Settings.Basic.SingleGalleryMode, Arg.Any<bool>()).Returns(false);
|
||||
_settings.GetOrDefault(Settings.Basic.GuestGalleryCreation, Arg.Any<bool>()).Returns(false);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext();
|
||||
|
||||
ViewResult actual = (ViewResult)await controller.Index(id, identifier, "password", ViewMode.Default, GalleryGroup.None, GalleryOrder.Random);
|
||||
Assert.That(actual, Is.TypeOf<ViewResult>());
|
||||
Assert.That(actual?.Model, Is.Not.Null);
|
||||
|
||||
PhotoGallery model = (PhotoGallery)actual.Model;
|
||||
Assert.That(model?.Gallery?.Identifier, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(true, true)]
|
||||
[TestCase(false, false)]
|
||||
public async Task GalleryController_UploadDisabled(bool enabled, bool expected)
|
||||
{
|
||||
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(DeviceType.Desktop);
|
||||
_settings.GetOrDefault(Settings.Basic.SingleGalleryMode, Arg.Any<bool>()).Returns(false);
|
||||
_settings.GetOrDefault(Settings.Gallery.Upload, Arg.Any<bool>(), Arg.Any<int>()).Returns(enabled);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext();
|
||||
|
||||
ViewResult actual = (ViewResult)await controller.Index("default", "default", "password", ViewMode.Default, GalleryGroup.None, GalleryOrder.Descending);
|
||||
Assert.That(actual, Is.TypeOf<ViewResult>());
|
||||
Assert.That(actual?.Model, Is.Not.Null);
|
||||
|
||||
PhotoGallery model = (PhotoGallery)actual.Model;
|
||||
Assert.That(model?.UploadActivated, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("1970-01-01 00:00", true)]
|
||||
[TestCase("3000-01-01 00:00", false)]
|
||||
[TestCase("1970-01-01 00:00 / 1980-01-01 00:00", false)]
|
||||
[TestCase("2999-01-01 00:00 / 3000-01-01 00:00", false)]
|
||||
[TestCase("1970-01-01 00:00 / 3000-01-01 00:00", true)]
|
||||
public async Task GalleryController_UploadDisabled(string uploadPeriod, bool expected)
|
||||
{
|
||||
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(DeviceType.Desktop);
|
||||
_settings.GetOrDefault(Settings.Basic.SingleGalleryMode, Arg.Any<bool>()).Returns(false);
|
||||
_settings.GetOrDefault(Settings.Gallery.UploadPeriod, Arg.Any<string>(), Arg.Any<int>()).Returns(uploadPeriod);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext();
|
||||
|
||||
ViewResult actual = (ViewResult)await controller.Index("default", "default", "password", ViewMode.Default, GalleryGroup.None, GalleryOrder.Descending);
|
||||
Assert.That(actual, Is.TypeOf<ViewResult>());
|
||||
Assert.That(actual?.Model, Is.Not.Null);
|
||||
|
||||
PhotoGallery model = (PhotoGallery)actual.Model;
|
||||
Assert.That(model?.UploadActivated, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(DeviceType.Desktop, ViewMode.Default, GalleryGroup.None, GalleryOrder.Descending)]
|
||||
[TestCase(DeviceType.Mobile, ViewMode.Presentation, GalleryGroup.Date, GalleryOrder.Ascending)]
|
||||
[TestCase(DeviceType.Tablet, ViewMode.Slideshow, GalleryGroup.Uploader, GalleryOrder.Ascending)]
|
||||
public async Task GalleryController_Index_SingleGalleryMode(DeviceType deviceType, ViewMode? mode, GalleryGroup group, GalleryOrder order)
|
||||
{
|
||||
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(deviceType);
|
||||
_settings.GetOrDefault(Settings.Basic.SingleGalleryMode, Arg.Any<bool>()).Returns(true);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext();
|
||||
|
||||
ViewResult actual = (ViewResult)await controller.Index("default", "default", "password", mode, group, order);
|
||||
Assert.That(actual, Is.TypeOf<ViewResult>());
|
||||
Assert.That(actual?.Model, Is.Not.Null);
|
||||
|
||||
PhotoGallery model = (PhotoGallery)actual.Model;
|
||||
Assert.That(model?.Gallery?.Id, Is.EqualTo(1));
|
||||
Assert.That(model?.Gallery?.Identifier, Is.EqualTo("default"));
|
||||
Assert.That(model?.Gallery?.Name, Is.EqualTo("default"));
|
||||
Assert.That(model?.SecretKey, Is.EqualTo("password"));
|
||||
Assert.That(model.ViewMode, Is.EqualTo(mode));
|
||||
}
|
||||
|
||||
[TestCase(true, 1, null)]
|
||||
[TestCase(true, 3, "Bob")]
|
||||
[TestCase(false, 1, "")]
|
||||
[TestCase(false, 3, "Unit Testing")]
|
||||
public async Task GalleryController_UploadImage(bool requiresReview, int fileCount, string? uploadedBy)
|
||||
{
|
||||
_settings.GetOrDefault(Settings.Gallery.RequireReview, Arg.Any<bool>()).Returns(requiresReview);
|
||||
|
||||
var files = new FormFileCollection();
|
||||
for (var i = 0; i < fileCount; i++)
|
||||
{
|
||||
files.Add(new FormFile(null, 0, 0, "TestFile_001", $"{Guid.NewGuid()}.jpg"));
|
||||
}
|
||||
|
||||
var session = new MockSession();
|
||||
session.Set(SessionKey.ViewerIdentity, uploadedBy ?? string.Empty);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
|
||||
session: session,
|
||||
form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", "1" },
|
||||
{ "SecretKey", "password" }
|
||||
},
|
||||
files: files);
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.True);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(files.Count));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploadedBy", string.Empty), Is.EqualTo(!string.IsNullOrWhiteSpace(uploadedBy) ? uploadedBy : string.Empty));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[TestCase]
|
||||
public async Task GalleryController_UploadImage_Duplicate()
|
||||
{
|
||||
_database.GetGalleryItemByChecksum(Arg.Any<int>(), Arg.Any<string>()).Returns(Task.FromResult(MockData.MockGalleryItems(1, 1, GalleryItemState.Approved).FirstOrDefault()));
|
||||
|
||||
var files = new FormFileCollection();
|
||||
files.Add(new FormFile(null, 0, 0, "TestFile_001", $"{Guid.NewGuid()}.jpg"));
|
||||
|
||||
var session = new MockSession();
|
||||
session.Set(SessionKey.ViewerIdentity, string.Empty);
|
||||
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
|
||||
session: session,
|
||||
form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", "1" },
|
||||
{ "SecretKey", "password" }
|
||||
},
|
||||
files: files);
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
[TestCase(null)]
|
||||
[TestCase("")]
|
||||
public async Task GalleryController_UploadImage_InvalidGallery(string? id)
|
||||
{
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", id }
|
||||
});
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
[TestCase(null)]
|
||||
[TestCase("")]
|
||||
public async Task GalleryController_UploadImage_InvalidSecretKey(string? key)
|
||||
{
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", "1" },
|
||||
{ "SecretKey", key }
|
||||
});
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task GalleryController_UploadImage_MissingGallery()
|
||||
{
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", Guid.NewGuid().ToString() }
|
||||
});
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task GalleryController_UploadImage_NoFiles()
|
||||
{
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", "1" },
|
||||
{ "SecretKey", "password" }
|
||||
});
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task GalleryController_UploadImage_FileTooBig()
|
||||
{
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
|
||||
form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", "1" },
|
||||
{ "SecretKey", "password" }
|
||||
},
|
||||
files: new FormFileCollection() {
|
||||
new FormFile(null, 0, int.MaxValue, "TestFile_001", $"{Guid.NewGuid()}.jpg")
|
||||
});
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task GalleryController_UploadImage_InvalidFileType()
|
||||
{
|
||||
var controller = new GalleryController(_env, _settings, _database, _file, _deviceDetector, _image, _notification, _encryption, _url, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = MockData.MockHttpContext(
|
||||
form: new Dictionary<string, StringValues>
|
||||
{
|
||||
{ "Id", "1" },
|
||||
{ "SecretKey", "password" }
|
||||
},
|
||||
files: new FormFileCollection() {
|
||||
new FormFile(null, 0, int.MaxValue, "TestFile_001", $"{Guid.NewGuid()}.blaa")
|
||||
});
|
||||
|
||||
JsonResult actual = (JsonResult)await controller.UploadImage();
|
||||
Assert.That(actual, Is.TypeOf<JsonResult>());
|
||||
Assert.That(actual?.Value, Is.Not.Null);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "success", false), Is.False);
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "uploaded", 0), Is.EqualTo(0));
|
||||
Assert.That(JsonResponseHelper.GetPropertyValue(actual.Value, "errors", new List<string>()).Count, Is.GreaterThan(0));
|
||||
}
|
||||
|
||||
private IDictionary<string, GalleryModel> GetMockData()
|
||||
{
|
||||
return new Dictionary<string, GalleryModel>()
|
||||
{
|
||||
{
|
||||
"default", new GalleryModel()
|
||||
{
|
||||
Id = 1,
|
||||
Identifier = "default",
|
||||
Name = "default",
|
||||
SecretKey = "password",
|
||||
ApprovedItems = 32,
|
||||
PendingItems = 50,
|
||||
TotalItems = 72
|
||||
}
|
||||
},
|
||||
{
|
||||
"blaa", new GalleryModel()
|
||||
{
|
||||
Id = 2,
|
||||
Identifier = "blaa",
|
||||
Name = "blaa",
|
||||
SecretKey = "456789",
|
||||
ApprovedItems = 2,
|
||||
PendingItems = 1,
|
||||
TotalItems = 3
|
||||
}
|
||||
},
|
||||
{
|
||||
"missing", new GalleryModel()
|
||||
{
|
||||
Id = 101,
|
||||
Identifier = "missing",
|
||||
Name = "missing",
|
||||
SecretKey = "123456",
|
||||
ApprovedItems = 0,
|
||||
PendingItems = 0,
|
||||
TotalItems = 0,
|
||||
Owner = 0
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Routing;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Controllers;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models.Database;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class HomeControllerTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
private readonly IDatabaseHelper _database = Substitute.For<IDatabaseHelper>();
|
||||
private readonly IDeviceDetector _deviceDetector = Substitute.For<IDeviceDetector>();
|
||||
private readonly IAuditHelper _audit = Substitute.For<IAuditHelper>();
|
||||
private readonly ILogger<HomeController> _logger = Substitute.For<ILogger<HomeController>>();
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer = Substitute.For<IStringLocalizer<Lang.Translations>>();
|
||||
|
||||
public HomeControllerTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase(DeviceType.Desktop, true, "", true)]
|
||||
[TestCase(DeviceType.Desktop, false, "", false)]
|
||||
[TestCase(DeviceType.Mobile, true, "", true)]
|
||||
[TestCase(DeviceType.Mobile, false, "", false)]
|
||||
[TestCase(DeviceType.Desktop, true, "123456", false)]
|
||||
[TestCase(DeviceType.Desktop, false, "Abc123!", false)]
|
||||
[TestCase(DeviceType.Mobile, true, "abc123!", false)]
|
||||
[TestCase(DeviceType.Mobile, false, "adsbsds", false)]
|
||||
public async Task HomeController_Index(DeviceType deviceType, bool singleGalleryMode, string secretKey, bool isRedirect)
|
||||
{
|
||||
_deviceDetector.ParseDeviceType(Arg.Any<string>()).Returns(deviceType);
|
||||
_database.GetGallery(1).Returns(new GalleryModel()
|
||||
{
|
||||
SecretKey = secretKey,
|
||||
});
|
||||
_settings.GetOrDefault(Settings.Basic.SingleGalleryMode, Arg.Any<bool>()).Returns(singleGalleryMode);
|
||||
|
||||
var controller = new HomeController(_settings, _database, _deviceDetector, _audit, _logger, _localizer);
|
||||
controller.ControllerContext.HttpContext = new DefaultHttpContext()
|
||||
{
|
||||
Session = new MockSession()
|
||||
};
|
||||
|
||||
if (!isRedirect)
|
||||
{
|
||||
ViewResult actual = (ViewResult)await controller.Index();
|
||||
Assert.That(actual, Is.TypeOf<ViewResult>());
|
||||
}
|
||||
else
|
||||
{
|
||||
RedirectToActionResult actual = (RedirectToActionResult)await controller.Index();
|
||||
Assert.That(actual, Is.TypeOf<RedirectToActionResult>());
|
||||
Assert.That(actual.Permanent, Is.EqualTo(false));
|
||||
Assert.That(actual.ControllerName, Is.EqualTo("Gallery"));
|
||||
Assert.That(actual.ActionName, Is.EqualTo("Index"));
|
||||
Assert.That(actual.RouteValues, singleGalleryMode ? Is.EqualTo(new RouteValueDictionary { { "identifier", "default" } }) : Is.Null);
|
||||
Assert.That(actual.Fragment, Is.Null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using WeddingShare.Extensions;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class DictionaryExtensionsTests
|
||||
{
|
||||
private readonly IDictionary<string, string> _data;
|
||||
|
||||
public DictionaryExtensionsTests()
|
||||
{
|
||||
_data = new Dictionary<string, string>()
|
||||
{
|
||||
{ "KEY_1", "1" },
|
||||
{ "KEY_2", "2" },
|
||||
{ "KEY_3", "3" },
|
||||
{ "KEY_4", "4" },
|
||||
};
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("KEY_1", "1")]
|
||||
[TestCase("KEY_2", "2")]
|
||||
[TestCase("KEY_3", "3")]
|
||||
[TestCase("KEY_99", "")]
|
||||
public void DictionaryExtensions_GetValue(string key, string expected)
|
||||
{
|
||||
var actual = _data.GetValue(key);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("KEY_1", "Default", "1")]
|
||||
[TestCase("KEY_2", "Default", "2")]
|
||||
[TestCase("KEY_3", "Default", "3")]
|
||||
[TestCase("KEY_99", "Default", "Default")]
|
||||
public void DictionaryExtensions_GetValue_DefaultValue(string key, string defaultValue, string? expected)
|
||||
{
|
||||
var actual = _data.GetValue(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
39
WeddingShare.UnitTests/Tests/Extensions/StringExtensions.cs
Normal file
39
WeddingShare.UnitTests/Tests/Extensions/StringExtensions.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using WeddingShare.Extensions;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class StringExtensionsTests
|
||||
{
|
||||
public StringExtensionsTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("abcdefgh", new [] { "g" }, "+", "abcdef+h")]
|
||||
[TestCase("abcdefgh", new [] { "b", "e" }, "-", "a-cd-fgh")]
|
||||
[TestCase("abcdefgh", new [] { "bc", "e" }, "-", "a-d-fgh")]
|
||||
[TestCase("abcdefgeh", new [] { "bc", "e" }, "$", "a$d$fg$h")]
|
||||
[TestCase("abcdefgeh", new[] { "ee", "z" }, "$", "abcdefgeh")]
|
||||
public void StringExtensions_Replace(string input, string[] oldChars, string newChar, string expected)
|
||||
{
|
||||
var actual = input.Replace(oldChars, newChar);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("abcdefge", "c", "abdefge")]
|
||||
[TestCase("abcdefge", "cd", "abefge")]
|
||||
[TestCase("abcdefge", "def", "abcge")]
|
||||
[TestCase("abcdefge", "e", "abcdfg")]
|
||||
[TestCase("abcdefg", "g", "abcdef")]
|
||||
[TestCase("abcdefg", "a", "bcdefg")]
|
||||
public void StringExtensions_Remove(string input, string value, string expected)
|
||||
{
|
||||
var actual = input.Remove(value);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
167
WeddingShare.UnitTests/Tests/Helpers/ConfigHelper.cs
Normal file
167
WeddingShare.UnitTests/Tests/Helpers/ConfigHelper.cs
Normal file
@@ -0,0 +1,167 @@
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class ConfigHelperTests
|
||||
{
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly IEnvironmentWrapper _environment = Substitute.For<IEnvironmentWrapper>();
|
||||
private readonly ILogger<ConfigHelper> _logger = Substitute.For<ILogger<ConfigHelper>>();
|
||||
|
||||
public ConfigHelperTests()
|
||||
{
|
||||
_environment.GetEnvironmentVariable("VERSION").Returns("v2.0.0");
|
||||
_environment.GetEnvironmentVariable("ENVKEY_1").Returns("EnvValue1");
|
||||
_environment.GetEnvironmentVariable("ENVKEY_2").Returns("EnvValue2");
|
||||
_environment.GetEnvironmentVariable("ENVKEY_3").Returns("EnvValue3");
|
||||
|
||||
_configuration = ConfigurationHelper.MockConfiguration(new Dictionary<string, string?>()
|
||||
{
|
||||
{ "Release:Version", "v1.0.0" },
|
||||
{ "Release:Plugin:Version", "v3.0.0" },
|
||||
|
||||
{ "String1:Key1", "Value1" },
|
||||
{ "String1:Key2", "Value2" },
|
||||
{ "String2:Key1", "Value3" },
|
||||
|
||||
{ "Int1:Key1", "1" },
|
||||
{ "Int1:Key2", "2" },
|
||||
{ "Int2:Key1", "3" },
|
||||
|
||||
{ "Long1:Key1", "4" },
|
||||
{ "Long1:Key2", "5" },
|
||||
{ "Long2:Key1", "6" },
|
||||
|
||||
{ "Decimal1:Key1", "4.12" },
|
||||
{ "Decimal1:Key2", "5.45" },
|
||||
{ "Decimal2:Key1", "6.733" },
|
||||
|
||||
{ "Double1:Key1", "4.12" },
|
||||
{ "Double1:Key2", "5.45" },
|
||||
{ "Double2:Key1", "6.733" },
|
||||
|
||||
{ "Boolean1:Key1", "true" },
|
||||
{ "Boolean1:Key2", "false" },
|
||||
{ "Boolean2:Key1", "true" },
|
||||
|
||||
{ "DateTime1:Key1", "1987-11-20 08:00:00" },
|
||||
{ "DateTime1:Key2", "2000-08-12 12:00:00" },
|
||||
{ "DateTime2:Key1", "2018-01-01 20:30:10" },
|
||||
});
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("SETTINGS:ENVKEY:1", "EnvValue1")]
|
||||
[TestCase("SETTINGS:ENVKEY:2", "EnvValue2")]
|
||||
[TestCase("SETTINGS:ENVKEY:3", "EnvValue3")]
|
||||
[TestCase("SETTINGS:ENVKEY:4", null)]
|
||||
[TestCase("RELEASE:VERSION", null)]
|
||||
public void ConfigHelper_GetEnvironmentVariable(string section, string? expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetEnvironmentVariable(section);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("String1:Key1", "Value1")]
|
||||
[TestCase("String1:Key2", "Value2")]
|
||||
[TestCase("String2:Key1", "Value3")]
|
||||
[TestCase("String2:Key2", null)]
|
||||
[TestCase("Release:Version", "v1.0.0")]
|
||||
public void ConfigHelper_GetConfigValue(string key, string? expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetConfigValue(key);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("String1:Key1", "Value1")]
|
||||
[TestCase("String1:Key2", "Value2")]
|
||||
[TestCase("String2:Key1", "Value3")]
|
||||
[TestCase("String2:Key2", null)]
|
||||
[TestCase("Release:Version", "v1.0.0")]
|
||||
public void ConfigHelper_Get(string key, string? expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).Get(key);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("String1:Key1", "Default", "Value1")]
|
||||
[TestCase("String1:Key2", "Default", "Value2")]
|
||||
[TestCase("String2:Key1", "Default", "Value3")]
|
||||
[TestCase("String2:Key2", "Default", "Default")]
|
||||
[TestCase("Release:Version", "v0.0.0", "v1.0.0")]
|
||||
[TestCase("Release:Plugin:Version", "v0.0.0", "v3.0.0")]
|
||||
public void ConfigHelper_GetOrDefault(string key, string defaultValue, string expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Int1:Key1", 999, 1)]
|
||||
[TestCase("Int1:Key2", 999, 2)]
|
||||
[TestCase("Int2:Key1", 999, 3)]
|
||||
[TestCase("Int2:Key2", 999, 999)]
|
||||
public void ConfigHelper_GetOrDefault(string key, int defaultValue, int expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Long1:Key1", 999, 4)]
|
||||
[TestCase("Long1:Key2", 999, 5)]
|
||||
[TestCase("Long2:Key1", 999, 6)]
|
||||
[TestCase("Long2:Key2", 999, 999)]
|
||||
public void ConfigHelper_GetOrDefault(string key, long defaultValue, long expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Decimal1:Key1", 999, 4.12)]
|
||||
[TestCase("Decimal1:Key2", 999, 5.45)]
|
||||
[TestCase("Decimal2:Key1", 999, 6.733)]
|
||||
[TestCase("Decimal2:Key2", 999, 999)]
|
||||
public void ConfigHelper_GetOrDefault(string key, decimal defaultValue, decimal expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Double1:Key1", 999, 4.12)]
|
||||
[TestCase("Double1:Key2", 999, 5.45)]
|
||||
[TestCase("Double2:Key1", 999, 6.733)]
|
||||
[TestCase("Double2:Key2", 999, 999)]
|
||||
public void ConfigHelper_GetOrDefault(string key, double defaultValue, double expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Boolean1:Key1", false, true)]
|
||||
[TestCase("Boolean1:Key2", false, false)]
|
||||
[TestCase("Boolean2:Key1", false, true)]
|
||||
[TestCase("Boolean2:Key2", true, true)]
|
||||
public void ConfigHelper_GetOrDefault(string key, bool defaultValue, bool expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("DateTime1:Key1", null, "1987-11-20 08:00:00")]
|
||||
[TestCase("DateTime1:Key2", null, "2000-08-12 12:00:00")]
|
||||
[TestCase("DateTime2:Key1", null, "2018-01-01 20:30:10")]
|
||||
[TestCase("DateTime2:Key2", null, null)]
|
||||
[TestCase("DateTime3:Key3", "2350-05-05 00:05:12", "2350-05-05 00:05:12")]
|
||||
public void ConfigHelper_GetOrDefault(string key, DateTime? defaultValue, string? expected)
|
||||
{
|
||||
var actual = new ConfigHelper(_environment, _configuration, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(!string.IsNullOrWhiteSpace(expected) ? DateTime.Parse(expected) : null));
|
||||
}
|
||||
}
|
||||
}
|
||||
23
WeddingShare.UnitTests/Tests/Helpers/DeviceDetector.cs
Normal file
23
WeddingShare.UnitTests/Tests/Helpers/DeviceDetector.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class DeviceDetectorTests
|
||||
{
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36", DeviceType.Desktop)]
|
||||
[TestCase("Mozilla/5.0 (Linux; Android 7.0; SM-T827R4 Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.116 Safari/537.36", DeviceType.Tablet)]
|
||||
[TestCase("Mozilla/5.0 (Linux; Android 13; SM-G998B) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Mobile Safari/537.36", DeviceType.Mobile)]
|
||||
[TestCase("Googlebot-Image/1.0", DeviceType.Desktop)]
|
||||
public async Task DeviceDetector_ParseDeviceType(string userAgent, DeviceType expected)
|
||||
{
|
||||
var actual = await new DeviceDetector().ParseDeviceType(userAgent);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
110
WeddingShare.UnitTests/Tests/Helpers/EmailHelper.cs
Normal file
110
WeddingShare.UnitTests/Tests/Helpers/EmailHelper.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
using System.Net.Mail;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NSubstitute;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class EmailHelperTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
private readonly ISmtpClientWrapper _smtp = Substitute.For<ISmtpClientWrapper>();
|
||||
private readonly ILogger<EmailHelper> _logger = Substitute.For<ILogger<EmailHelper>>();
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer = Substitute.For<IStringLocalizer<Lang.Translations>>();
|
||||
|
||||
public EmailHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_smtp.SendMailAsync(Arg.Any<SmtpClient>(), Arg.Any<MailMessage>()).Returns(Task.FromResult(true));
|
||||
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Enabled, Arg.Any<bool>()).Returns(true);
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Recipient, Arg.Any<string>()).Returns("unit@test.com");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Host, Arg.Any<string>()).Returns("https://unit.test.com/");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Port, Arg.Any<int>()).Returns(999);
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Username, Arg.Any<string>()).Returns("Unit");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Password, Arg.Any<string>()).Returns("Test");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.From, Arg.Any<string>()).Returns("unittest@test.com");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.DisplayName, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.UseSSL, Arg.Any<bool>()).Returns(true);
|
||||
}
|
||||
|
||||
[TestCase("unit", "test")]
|
||||
public async Task EmailHelper_Success(string title, string message)
|
||||
{
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send(title, message);
|
||||
Assert.That(actual, Is.EqualTo(true));
|
||||
}
|
||||
|
||||
[TestCase(true, true)]
|
||||
[TestCase(false, false)]
|
||||
public async Task EmailHelper_Enabled(bool enabled, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Enabled, Arg.Any<bool>()).Returns(enabled);
|
||||
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("", false)]
|
||||
[TestCase("blaa@blaa.com", true)]
|
||||
public async Task EmailHelper_Recipient(string recipient, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Recipient, Arg.Any<string>()).Returns(recipient);
|
||||
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("", false)]
|
||||
[TestCase("https://unit.test.com/", true)]
|
||||
public async Task EmailHelper_Host(string host, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Host, Arg.Any<string>()).Returns(host);
|
||||
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(-100, false)]
|
||||
[TestCase(-1, false)]
|
||||
[TestCase(0, false)]
|
||||
[TestCase(1, true)]
|
||||
public async Task EmailHelper_Port(int port, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Port, Arg.Any<int>()).Returns(port);
|
||||
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("", false)]
|
||||
[TestCase("blaa@blaa.com", true)]
|
||||
public async Task EmailHelper_From(string from, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.From, Arg.Any<string>()).Returns(from);
|
||||
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, true)]
|
||||
[TestCase("", true)]
|
||||
[TestCase("UnitTest", true)]
|
||||
public async Task EmailHelper_DisplayName(string displayName, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.DisplayName, Arg.Any<string>()).Returns(displayName);
|
||||
|
||||
var actual = await new EmailHelper(_settings, _smtp, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class EmailValidationHelperTests
|
||||
{
|
||||
public EmailValidationHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("test", false)]
|
||||
[TestCase("test.unit", false)]
|
||||
[TestCase("test.unit@", false)]
|
||||
[TestCase("test.unit@com", false)]
|
||||
[TestCase("test.unit@unit@com", false)]
|
||||
[TestCase("test.unit@unit.com", true)]
|
||||
public void EmailValidationHelper_IsValid(string input, bool expected)
|
||||
{
|
||||
var actual = EmailValidationHelper.IsValid(input);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
36
WeddingShare.UnitTests/Tests/Helpers/EncodingHelper.cs
Normal file
36
WeddingShare.UnitTests/Tests/Helpers/EncodingHelper.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class EncodingHelperTests
|
||||
{
|
||||
public EncodingHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("This is a test...", "VGhpcyBpcyBhIHRlc3QuLi4=")]
|
||||
[TestCase("TESTING", "VEVTVElORw==")]
|
||||
[TestCase("kdsjfhksjhfkdshfkjhskjhfkhskjdfhksdhfkhsdkjfhksdhfkjdshf@Asd!!&$^$$**==", "a2RzamZoa3NqaGZrZHNoZmtqaHNramhma2hza2pkZmhrc2RoZmtoc2RramZoa3NkaGZramRzaGZAQXNkISEmJF4kJCoqPT0=")]
|
||||
public void EncrytpionHelper_Base64Encode(string input, string output)
|
||||
{
|
||||
var actual = EncodingHelper.Base64Encode(input);
|
||||
|
||||
Assert.That(actual, Is.EqualTo(output));
|
||||
}
|
||||
|
||||
[TestCase("VGhpcyBpcyBhIHRlc3QuLi4=", "This is a test...")]
|
||||
[TestCase("VEVTVElORw==", "TESTING")]
|
||||
[TestCase("a2RzamZoa3NqaGZrZHNoZmtqaHNramhma2hza2pkZmhrc2RoZmtoc2RramZoa3NkaGZramRzaGZAQXNkISEmJF4kJCoqPT0=", "kdsjfhksjhfkdshfkjhskjhfkhskjdfhksdhfkhsdkjfhksdhfkjdshf@Asd!!&$^$$**==")]
|
||||
public void EncrytpionHelper_Base64Decode(string input, string output)
|
||||
{
|
||||
var actual = EncodingHelper.Base64Decode(input);
|
||||
|
||||
Assert.That(actual, Is.EqualTo(output));
|
||||
}
|
||||
}
|
||||
}
|
||||
86
WeddingShare.UnitTests/Tests/Helpers/EncrytpionHelper.cs
Normal file
86
WeddingShare.UnitTests/Tests/Helpers/EncrytpionHelper.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class EncrytpionHelperTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
|
||||
public EncrytpionHelperTests()
|
||||
{
|
||||
_settings.GetOrDefault(Security.Encryption.HashType, Arg.Any<string>()).Returns("SHA256");
|
||||
_settings.GetOrDefault(Security.Encryption.Iterations, Arg.Any<int>()).Returns(1000);
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("Test", "Key1", "Salt1", "ZMw15YpZ+uph9psdR6tEZg==")]
|
||||
[TestCase("Test", "Key2", "Salt2", "p/fwjLVXvJ2dRKbXDNhxDA==")]
|
||||
[TestCase("Test", "Key3", "Salt3", "47VYeotX2C8GPuhaQlrWXg==")]
|
||||
public void EncrytpionHelper_ValidDetails(string value, string key, string salt, string expected)
|
||||
{
|
||||
_settings.GetOrDefault(Security.Encryption.Key, Arg.Any<string>()).Returns(key);
|
||||
_settings.GetOrDefault(Security.Encryption.Salt, Arg.Any<string>()).Returns(salt);
|
||||
|
||||
var actual = new EncryptionHelper(_settings).Encrypt(value);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Test1", "Salt1")]
|
||||
[TestCase("Test2", "Salt2")]
|
||||
[TestCase("Test3", "Salt3")]
|
||||
public void EncrytpionHelper_NoKey(string value, string salt)
|
||||
{
|
||||
_settings.GetOrDefault(Security.Encryption.Key, Arg.Any<string>()).Returns(string.Empty);
|
||||
_settings.GetOrDefault(Security.Encryption.Salt, Arg.Any<string>()).Returns(salt);
|
||||
|
||||
var actual = new EncryptionHelper(_settings).Encrypt(value);
|
||||
Assert.That(actual, Is.EqualTo(value));
|
||||
}
|
||||
|
||||
[TestCase("Test1", "Key1")]
|
||||
[TestCase("Test2", "Key2")]
|
||||
[TestCase("Test3", "Key3")]
|
||||
public void EncrytpionHelper_NoSalt(string value, string key)
|
||||
{
|
||||
_settings.GetOrDefault(Security.Encryption.Key, Arg.Any<string>()).Returns(key);
|
||||
_settings.GetOrDefault(Security.Encryption.Salt, Arg.Any<string>()).Returns(string.Empty);
|
||||
|
||||
var actual = new EncryptionHelper(_settings).Encrypt(value);
|
||||
Assert.That(actual, Is.EqualTo(value));
|
||||
}
|
||||
|
||||
[TestCase("Test1", "Key1", "Salt1")]
|
||||
[TestCase("Test2", "Key2", "Salt2")]
|
||||
[TestCase("Test3", "Key3", "Salt3")]
|
||||
public void EncrytpionHelper_DifferentHashes(string value, string key, string salt)
|
||||
{
|
||||
_settings.GetOrDefault(Security.Encryption.Key, Arg.Any<string>()).Returns(key);
|
||||
_settings.GetOrDefault(Security.Encryption.Salt, Arg.Any<string>()).Returns(salt);
|
||||
var helper1 = new EncryptionHelper(_settings).Encrypt(value);
|
||||
|
||||
_settings.GetOrDefault(Security.Encryption.Key, Arg.Any<string>()).Returns("Unit");
|
||||
_settings.GetOrDefault(Security.Encryption.Salt, Arg.Any<string>()).Returns("Test");
|
||||
var helper2 = new EncryptionHelper(_settings).Encrypt(value);
|
||||
|
||||
Assert.That(helper1, Is.Not.EqualTo(helper2));
|
||||
}
|
||||
|
||||
[TestCase("Key", "", false)]
|
||||
[TestCase("", "Salt", false)]
|
||||
[TestCase("Key", "Salt", true)]
|
||||
public void EncrytpionHelper_IsEncryptionEnabled(string key, string salt, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Security.Encryption.Key, Arg.Any<string>()).Returns(key);
|
||||
_settings.GetOrDefault(Security.Encryption.Salt, Arg.Any<string>()).Returns(salt);
|
||||
|
||||
var actual = new EncryptionHelper(_settings).IsEncryptionEnabled();
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
81
WeddingShare.UnitTests/Tests/Helpers/FileHelper.cs
Normal file
81
WeddingShare.UnitTests/Tests/Helpers/FileHelper.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class FileHelperTests
|
||||
{
|
||||
private readonly ILogger<FileHelper> _logger = Substitute.For<ILogger<FileHelper>>();
|
||||
|
||||
public FileHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase(-1, "0 B")]
|
||||
[TestCase(0, "0 B")]
|
||||
[TestCase(1, "1 B")]
|
||||
[TestCase(2, "2 B")]
|
||||
[TestCase(3, "3 B")]
|
||||
public void FileHelper_BytesToHumanReadable(long bytes, string expected)
|
||||
{
|
||||
var actual = new FileHelper(_logger).BytesToHumanReadable(bytes, 0);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(-1, "0 B")]
|
||||
[TestCase(0, "0 B")]
|
||||
[TestCase(1, "1 B")]
|
||||
[TestCase(10, "10 B")]
|
||||
[TestCase(100, "100 B")]
|
||||
[TestCase(1000, "1 KB")]
|
||||
[TestCase(1000000, "1 MB")]
|
||||
[TestCase(1000000000, "1 GB")]
|
||||
[TestCase(1000000000000, "1 TB")]
|
||||
[TestCase(1000000000000000, "1 PB")]
|
||||
[TestCase(1000000000000000000, "1 EB")]
|
||||
public void FileHelper_BytesToHumanReadable_No_Places(long bytes, string expected)
|
||||
{
|
||||
var actual = new FileHelper(_logger).BytesToHumanReadable(bytes, 0);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(-1, "0.0 B")]
|
||||
[TestCase(0, "0.0 B")]
|
||||
[TestCase(1, "1.0 B")]
|
||||
[TestCase(10, "10.0 B")]
|
||||
[TestCase(100, "100.0 B")]
|
||||
[TestCase(1000, "1.0 KB")]
|
||||
[TestCase(1000000, "1.0 MB")]
|
||||
[TestCase(1000000000, "1.0 GB")]
|
||||
[TestCase(1000000000000, "1.0 TB")]
|
||||
[TestCase(1000000000000000, "1.0 PB")]
|
||||
[TestCase(1000000000000000000, "1.0 EB")]
|
||||
public void FileHelper_BytesToHumanReadable_1_Places(long bytes, string expected)
|
||||
{
|
||||
var actual = new FileHelper(_logger).BytesToHumanReadable(bytes, 1);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(-1, "0.00 B")]
|
||||
[TestCase(0, "0.00 B")]
|
||||
[TestCase(1, "1.00 B")]
|
||||
[TestCase(10, "10.00 B")]
|
||||
[TestCase(100, "100.00 B")]
|
||||
[TestCase(1000, "1.00 KB")]
|
||||
[TestCase(1000000, "1.00 MB")]
|
||||
[TestCase(1000000000, "1.00 GB")]
|
||||
[TestCase(1000000000000, "1.00 TB")]
|
||||
[TestCase(1000000000000000, "1.00 PB")]
|
||||
[TestCase(1000000000000000000, "1.00 EB")]
|
||||
public void FileHelper_BytesToHumanReadable_2_Places(long bytes, string expected)
|
||||
{
|
||||
var actual = new FileHelper(_logger).BytesToHumanReadable(bytes, 2);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
45
WeddingShare.UnitTests/Tests/Helpers/GalleryHelper.cs
Normal file
45
WeddingShare.UnitTests/Tests/Helpers/GalleryHelper.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class GalleryHelperTests
|
||||
{
|
||||
public GalleryHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("all", true)]
|
||||
[TestCase("default", true)]
|
||||
[TestCase("38be9fc63cf343929629a1aff93f2598", true)]
|
||||
[TestCase("All", false)]
|
||||
[TestCase("Default", false)]
|
||||
[TestCase("38Be9fc63cf343929629a1aff93f2598", false)]
|
||||
[TestCase("38be9f-c63cf343929629a1af-f93f2598", false)]
|
||||
public void GalleryHelper_IsValidGalleryIdentifier(string value, bool expected)
|
||||
{
|
||||
var actual = GalleryHelper.IsValidGalleryIdentifier(value);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public void GalleryHelper_GenerateGalleryIdentifier()
|
||||
{
|
||||
var actual = GalleryHelper.IsValidGalleryIdentifier(GalleryHelper.GenerateGalleryIdentifier());
|
||||
Assert.That(actual, Is.EqualTo(true));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public void GalleryHelper_GenerateGalleryIdentifier_Unique()
|
||||
{
|
||||
var identity1 = GalleryHelper.GenerateGalleryIdentifier();
|
||||
var identity2 = GalleryHelper.GenerateGalleryIdentifier();
|
||||
|
||||
Assert.That(identity1, Is.Not.EqualTo(identity2));
|
||||
}
|
||||
}
|
||||
}
|
||||
88
WeddingShare.UnitTests/Tests/Helpers/GotifyHelper.cs
Normal file
88
WeddingShare.UnitTests/Tests/Helpers/GotifyHelper.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Net;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class GotifyHelperTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
private readonly IHttpClientFactory _clientFactory = Substitute.For<IHttpClientFactory>();
|
||||
private readonly ILogger<GotifyHelper> _logger = Substitute.For<ILogger<GotifyHelper>>();
|
||||
|
||||
public GotifyHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var client = new HttpClient(new MockHttpMessageHandler(HttpStatusCode.OK));
|
||||
client.BaseAddress = new Uri("https://unit.test.com/");
|
||||
|
||||
_clientFactory.CreateClient(Arg.Any<string>()).Returns(client);
|
||||
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Enabled, Arg.Any<bool>()).Returns(true);
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Endpoint, Arg.Any<string>()).Returns("https://unit.test.com/");
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Token, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Priority, Arg.Any<int>()).Returns(4);
|
||||
}
|
||||
|
||||
[TestCase("unit", "test")]
|
||||
public async Task GotifyHelper_Success(string title, string message)
|
||||
{
|
||||
var actual = await new GotifyHelper(_settings, _clientFactory, _logger).Send(title, message);
|
||||
Assert.That(actual, Is.EqualTo(true));
|
||||
}
|
||||
|
||||
[TestCase(true, true)]
|
||||
[TestCase(false, false)]
|
||||
public async Task GotifyHelper_Enabled(bool enabled, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Enabled, Arg.Any<bool>()).Returns(enabled);
|
||||
|
||||
var actual = await new GotifyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("http://unittest.com", true)]
|
||||
[TestCase("https://unittest.com", true)]
|
||||
public async Task GotifyHelper_Endpoint(string? endpoint, bool expected)
|
||||
{
|
||||
var client = new HttpClient(new MockHttpMessageHandler(HttpStatusCode.OK));
|
||||
client.BaseAddress = endpoint != null ? new Uri(endpoint) : null;
|
||||
|
||||
_clientFactory.CreateClient(Arg.Any<string>()).Returns(client);
|
||||
|
||||
var actual = await new GotifyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("", false)]
|
||||
[TestCase("UnitTest", true)]
|
||||
public async Task GotifyHelper_Token(string? token, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Token, Arg.Any<string>()).Returns(token);
|
||||
|
||||
var actual = await new GotifyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(-100, false)]
|
||||
[TestCase(-1, false)]
|
||||
[TestCase(0, false)]
|
||||
[TestCase(1, true)]
|
||||
[TestCase(100, true)]
|
||||
public async Task GotifyHelper_Priority(int priority, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Priority, Arg.Any<int>()).Returns(priority);
|
||||
|
||||
var actual = await new GotifyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
62
WeddingShare.UnitTests/Tests/Helpers/HtmlSanitizer.cs
Normal file
62
WeddingShare.UnitTests/Tests/Helpers/HtmlSanitizer.cs
Normal file
@@ -0,0 +1,62 @@
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class HtmlSanitizerTests
|
||||
{
|
||||
public HtmlSanitizerTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("<div><b>Test</b></div>", new[] { "b" }, "<div>Test</div>")]
|
||||
[TestCase("<div><b>Test</b></div>", new[] { "div" }, "<b>Test</b>")]
|
||||
[TestCase("<div><b>Test</b></div>", new[] { "div", "b" }, "Test")]
|
||||
[TestCase("<script src=\"asdasd\">Test</script>", new[] { "script" }, "Test")]
|
||||
[TestCase("<script src=\"asdasd\"/>", new[] { "script" }, "")]
|
||||
[TestCase("<img src=\"asdasd\"/>", new[] { "img" }, "")]
|
||||
[TestCase("Blaa<div><b>Test</b></div>", new[] { ".*" }, "Blaa")]
|
||||
public void HtmlSanitizer_SanitizeHtmlTags(string input, string[] tags, string expected)
|
||||
{
|
||||
var actual = HtmlSanitizer.SanitizeHtmlTags(input, tags);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("<script src=\"blaa\" link=\"unit\"></script>", new[] { "src" }, "<script link=\"unit\"></script>")]
|
||||
[TestCase("<script src=\"blaa\" link=\"unit\"></script>", new[] { "link" }, "<script src=\"blaa\" ></script>")]
|
||||
[TestCase("<script src=\"blaa\" link=\"unit\"></script>", new[] { ".*" }, "></script>")]
|
||||
public void HtmlSanitizer_SanitizeHtmlAttributes(string input, string[] tags, string expected)
|
||||
{
|
||||
var actual = HtmlSanitizer.SanitizeHtmlAttributes(input, tags);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("this is a https://unit.com/ test", "this is a test")]
|
||||
[TestCase("this is a https://www.unit.com/ test", "this is a test")]
|
||||
[TestCase("this is a https://www.unit.com/home test", "this is a test")]
|
||||
[TestCase("this is a http://unit.com/ test", "this is a test")]
|
||||
[TestCase("this is a http://www.unit.com/ test", "this is a test")]
|
||||
[TestCase("this is a http://www.unit.com/home test", "this is a test")]
|
||||
public void HtmlSanitizer_SanitizeHtmlAttributes(string input, string expected)
|
||||
{
|
||||
var actual = HtmlSanitizer.SanitizeLinks(input);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("this is a test", false)]
|
||||
[TestCase("this is a <img src=x onerror=alert(0)> test", true)]
|
||||
[TestCase("this is a <script/> test", true)]
|
||||
[TestCase("this is a <script></script> test", true)]
|
||||
[TestCase("this is a http://www.unit.com/home test", true)]
|
||||
[TestCase("this is a https://www.unit.com/home test", true)]
|
||||
public void HtmlSanitizer_MayContainXss(string input, bool expected)
|
||||
{
|
||||
var actual = HtmlSanitizer.MayContainXss(input);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
44
WeddingShare.UnitTests/Tests/Helpers/ImageHelper.cs
Normal file
44
WeddingShare.UnitTests/Tests/Helpers/ImageHelper.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class ImageHelperTests
|
||||
{
|
||||
private readonly IFileHelper _fileHelper = Substitute.For<IFileHelper>();
|
||||
private readonly ILogger<ImageHelper> _logger = Substitute.For<ILogger<ImageHelper>>();
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer = Substitute.For<IStringLocalizer<Lang.Translations>>();
|
||||
private readonly IDictionary<ImageOrientation, Image?> _imageCollection;
|
||||
|
||||
public ImageHelperTests()
|
||||
{
|
||||
_imageCollection = new Dictionary<ImageOrientation, Image?>()
|
||||
{
|
||||
{ ImageOrientation.Square, new Image<Rgba32>(100, 100) },
|
||||
{ ImageOrientation.Landscape, new Image<Rgba32>(200, 100) },
|
||||
{ ImageOrientation.Portrait, new Image<Rgba32>(100, 200) }
|
||||
};
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase(ImageOrientation.Square)]
|
||||
[TestCase(ImageOrientation.Landscape)]
|
||||
[TestCase(ImageOrientation.Portrait)]
|
||||
public void ImageHelper_GetOrientation(ImageOrientation orientation)
|
||||
{
|
||||
var image = _imageCollection[orientation];
|
||||
Assert.IsNotNull(image);
|
||||
|
||||
var actual = new ImageHelper(_fileHelper, _logger, _localizer).GetOrientation(image);
|
||||
Assert.That(actual, Is.EqualTo(orientation));
|
||||
}
|
||||
}
|
||||
}
|
||||
50
WeddingShare.UnitTests/Tests/Helpers/LanguageHelper.cs
Normal file
50
WeddingShare.UnitTests/Tests/Helpers/LanguageHelper.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Globalization;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class LanguageHelperTests
|
||||
{
|
||||
private readonly List<CultureInfo> _supportedCultures;
|
||||
|
||||
public LanguageHelperTests()
|
||||
{
|
||||
_supportedCultures = new List<CultureInfo>()
|
||||
{
|
||||
new CultureInfo("en-GB"),
|
||||
new CultureInfo("fr-FR"),
|
||||
new CultureInfo("de-DE"),
|
||||
};
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("en-GB", true)]
|
||||
[TestCase("fr-FR", true)]
|
||||
[TestCase("de-DE", true)]
|
||||
[TestCase("de-de", true)]
|
||||
[TestCase("en-US", false)]
|
||||
[TestCase("cn-CSS", false)]
|
||||
[TestCase("", false)]
|
||||
public void LanguageHelper_IsCultureSupported(string culture, bool expected)
|
||||
{
|
||||
var actual = new LanguageHelper().IsCultureSupported(culture, _supportedCultures);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("en-GB", "en-GB", "en-GB")]
|
||||
[TestCase("en-US", "en-GB", "en-GB")]
|
||||
[TestCase("en-US", "en-US", "en-GB")]
|
||||
[TestCase("fr-FR", "en-GB", "fr-FR")]
|
||||
[TestCase("de-DE", "fr-FR", "de-DE")]
|
||||
[TestCase("de-de", "fr-fr", "de-DE")]
|
||||
public void LanguageHelper_GetOrFallbackCulture(string culture, string fallback, string expected)
|
||||
{
|
||||
var actual = new LanguageHelper().GetOrFallbackCulture(culture, fallback, _supportedCultures);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
97
WeddingShare.UnitTests/Tests/Helpers/NotificationBroker.cs
Normal file
97
WeddingShare.UnitTests/Tests/Helpers/NotificationBroker.cs
Normal file
@@ -0,0 +1,97 @@
|
||||
using System.Net;
|
||||
using System.Net.Mail;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class NotificationBrokerTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
private readonly IHttpClientFactory _clientFactory = Substitute.For<IHttpClientFactory>();
|
||||
private readonly ISmtpClientWrapper _smtp = Substitute.For<ISmtpClientWrapper>();
|
||||
private readonly ILoggerFactory _logger = Substitute.For<ILoggerFactory>();
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer = Substitute.For<IStringLocalizer<Lang.Translations>>();
|
||||
|
||||
public NotificationBrokerTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var client = new HttpClient(new MockHttpMessageHandler(HttpStatusCode.OK));
|
||||
client.BaseAddress = new Uri("https://unit.test.com/");
|
||||
|
||||
_clientFactory.CreateClient(Arg.Any<string>()).Returns(client);
|
||||
|
||||
_smtp.SendMailAsync(Arg.Any<SmtpClient>(), Arg.Any<MailMessage>()).Returns(Task.FromResult(true));
|
||||
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Enabled, Arg.Any<bool>()).Returns(true);
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Recipient, Arg.Any<string>()).Returns("unit@test.com");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Host, Arg.Any<string>()).Returns("https://unit.test.com/");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Port, Arg.Any<int>()).Returns(999);
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Username, Arg.Any<string>()).Returns("Unit");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Password, Arg.Any<string>()).Returns("Test");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.From, Arg.Any<string>()).Returns("unittest@test.com");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.DisplayName, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.UseSSL, Arg.Any<bool>()).Returns(true);
|
||||
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Enabled, Arg.Any<bool>()).Returns(true);
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Endpoint, Arg.Any<string>()).Returns("https://unit.test.com/");
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Token, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Topic, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Priority, Arg.Any<int>()).Returns(4);
|
||||
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Enabled, Arg.Any<bool>()).Returns(true);
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Endpoint, Arg.Any<string>()).Returns("https://unit.test.com/");
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Token, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Priority, Arg.Any<int>()).Returns(4);
|
||||
}
|
||||
|
||||
[TestCase(false, false, false, true)]
|
||||
[TestCase(true, false, false, true)]
|
||||
[TestCase(false, true, false, true)]
|
||||
[TestCase(false, false, true, true)]
|
||||
[TestCase(true, true, true, true)]
|
||||
public async Task NotificationBroker_Success(bool smtp, bool ntfy, bool gotify, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Enabled, Arg.Any<bool>()).Returns(smtp);
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Enabled, Arg.Any<bool>()).Returns(ntfy);
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Enabled, Arg.Any<bool>()).Returns(gotify);
|
||||
|
||||
var actual = await new NotificationBroker(_settings, _smtp, _clientFactory, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task NotificationBroker_Issue_Smtp()
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Smtp.Host, Arg.Any<string>()).Returns(string.Empty);
|
||||
|
||||
var actual = await new NotificationBroker(_settings, _smtp, _clientFactory, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(false));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task NotificationBroker_Issue_Ntfy()
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Endpoint, Arg.Any<string>()).Returns(string.Empty);
|
||||
|
||||
var actual = await new NotificationBroker(_settings, _smtp, _clientFactory, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(false));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public async Task NotificationBroker_Issue_Gotify()
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Gotify.Endpoint, Arg.Any<string>()).Returns(string.Empty);
|
||||
|
||||
var actual = await new NotificationBroker(_settings, _smtp, _clientFactory, _logger, _localizer).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(false));
|
||||
}
|
||||
}
|
||||
}
|
||||
101
WeddingShare.UnitTests/Tests/Helpers/NtfyHelper.cs
Normal file
101
WeddingShare.UnitTests/Tests/Helpers/NtfyHelper.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class NtfyHelperTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
private readonly IHttpClientFactory _clientFactory = Substitute.For<IHttpClientFactory>();
|
||||
private readonly ILogger<NtfyHelper> _logger = Substitute.For<ILogger<NtfyHelper>>();
|
||||
|
||||
public NtfyHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var client = new HttpClient(new MockHttpMessageHandler(HttpStatusCode.OK));
|
||||
client.BaseAddress = new Uri("https://unit.test.com/");
|
||||
|
||||
_clientFactory.CreateClient(Arg.Any<string>()).Returns(client);
|
||||
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Enabled, Arg.Any<bool>()).Returns(true);
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Endpoint, Arg.Any<string>()).Returns("https://unit.test.com/");
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Token, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Topic, Arg.Any<string>()).Returns("UnitTest");
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Priority, Arg.Any<int>()).Returns(4);
|
||||
}
|
||||
|
||||
[TestCase("unit", "test")]
|
||||
public async Task NtfyHelper_Success(string title, string message)
|
||||
{
|
||||
var actual = await new NtfyHelper(_settings, _clientFactory, _logger).Send(title, message);
|
||||
Assert.That(actual, Is.EqualTo(true));
|
||||
}
|
||||
|
||||
[TestCase(true, true)]
|
||||
[TestCase(false, false)]
|
||||
public async Task NtfyHelper_Enabled(bool enabled, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Enabled, Arg.Any<bool>()).Returns(enabled);
|
||||
|
||||
var actual = await new NtfyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("http://unittest.com", true)]
|
||||
[TestCase("https://unittest.com", true)]
|
||||
public async Task NtfyHelper_Endpoint(string? endpoint, bool expected)
|
||||
{
|
||||
var client = new HttpClient(new MockHttpMessageHandler(HttpStatusCode.OK));
|
||||
client.BaseAddress = endpoint != null ? new Uri(endpoint) : null;
|
||||
|
||||
_clientFactory.CreateClient(Arg.Any<string>()).Returns(client);
|
||||
|
||||
var actual = await new NtfyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, true)]
|
||||
[TestCase("", true)]
|
||||
[TestCase("UnitTest", true)]
|
||||
public async Task NtfyHelper_Token(string? token, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Token, Arg.Any<string>()).Returns(token);
|
||||
|
||||
var actual = await new NtfyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, false)]
|
||||
[TestCase("", false)]
|
||||
[TestCase("UnitTest", true)]
|
||||
public async Task NtfyHelper_Topic(string? topic, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Topic, Arg.Any<string>()).Returns(topic);
|
||||
|
||||
var actual = await new NtfyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(-100, false)]
|
||||
[TestCase(-1, false)]
|
||||
[TestCase(0, false)]
|
||||
[TestCase(1, true)]
|
||||
[TestCase(100, true)]
|
||||
public async Task NtfyHelper_Priority(int priority, bool expected)
|
||||
{
|
||||
_settings.GetOrDefault(Constants.Notifications.Ntfy.Priority, Arg.Any<int>()).Returns(priority);
|
||||
|
||||
var actual = await new NtfyHelper(_settings, _clientFactory, _logger).Send("unit", "test");
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
76
WeddingShare.UnitTests/Tests/Helpers/PasswordHelper.cs
Normal file
76
WeddingShare.UnitTests/Tests/Helpers/PasswordHelper.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class PasswordHelperTests
|
||||
{
|
||||
public PasswordHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase("Password1!", true)]
|
||||
[TestCase("Password1", false)]
|
||||
[TestCase("Password!", false)]
|
||||
[TestCase("password1!", false)]
|
||||
[TestCase("PASSWORD1!", false)]
|
||||
[TestCase("Pass1!", false)]
|
||||
public void PasswordHelper_IsValid(string input, bool expected)
|
||||
{
|
||||
var actual = PasswordHelper.IsValid(input);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Password1!", true)]
|
||||
[TestCase("Admin1!", true)]
|
||||
[TestCase("Q*hgJ8FcSkm9$q7B9Zf#T7*LJ5", false)]
|
||||
public void PasswordHelper_IsWeak(string input, bool expected)
|
||||
{
|
||||
var actual = PasswordHelper.IsWeak(input);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(true, true, true, true, 30, true)]
|
||||
[TestCase(true, true, true, true, 10, true)]
|
||||
[TestCase(true, true, true, true, 5, false)]
|
||||
[TestCase(false, true, true, true, 30, false)]
|
||||
[TestCase(true, false, true, true, 30, false)]
|
||||
[TestCase(true, true, false, true, 30, false)]
|
||||
[TestCase(true, true, true, false, 30, false)]
|
||||
[TestCase(false, false, false, false, 30, false)]
|
||||
public void PasswordHelper_GenerateTempPassword(bool lower, bool upper, bool numbers, bool symbols, int length, bool isStrong)
|
||||
{
|
||||
var password = PasswordHelper.GenerateTempPassword(lower: lower, upper: upper, numbers: numbers, symbols: symbols, length: length);
|
||||
var actual = PasswordHelper.IsValid(password) && !PasswordHelper.IsWeak(password);
|
||||
Assert.That(actual, Is.EqualTo(isStrong), $"Password: '{password}' is not valid");
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public void PasswordHelper_GenerateGallerySecretKey()
|
||||
{
|
||||
var actual = PasswordHelper.GenerateGallerySecretKey();
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.That(actual.Length, Is.AtLeast(30));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public void PasswordHelper_GenerateSecretCode()
|
||||
{
|
||||
var actual = PasswordHelper.GenerateSecretCode();
|
||||
Assert.That(actual, Is.Not.Null);
|
||||
Assert.That(actual.Length, Is.AtLeast(20));
|
||||
}
|
||||
|
||||
[TestCase()]
|
||||
public void PasswordHelper_GenerateSecretCode_IsDifferent()
|
||||
{
|
||||
var actual1 = PasswordHelper.GenerateSecretCode();
|
||||
var actual2 = PasswordHelper.GenerateSecretCode();
|
||||
Assert.That(actual1, Is.Not.EqualTo(actual2));
|
||||
}
|
||||
}
|
||||
}
|
||||
195
WeddingShare.UnitTests/Tests/Helpers/SettingsHelper.cs
Normal file
195
WeddingShare.UnitTests/Tests/Helpers/SettingsHelper.cs
Normal file
@@ -0,0 +1,195 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using System;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models.Database;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class SettingsHelperTests
|
||||
{
|
||||
private readonly IConfigHelper _config;
|
||||
private readonly IDatabaseHelper _database = Substitute.For<IDatabaseHelper>();
|
||||
private readonly ILogger<SettingsHelper> _logger = Substitute.For<ILogger<SettingsHelper>>();
|
||||
|
||||
public SettingsHelperTests()
|
||||
{
|
||||
var environment = Substitute.For<IEnvironmentWrapper>();
|
||||
environment.GetEnvironmentVariable("VERSION").Returns("v2.0.0");
|
||||
environment.GetEnvironmentVariable("ENVKEY_1").Returns("EnvValue1");
|
||||
environment.GetEnvironmentVariable("ENVKEY_2").Returns("EnvValue2");
|
||||
environment.GetEnvironmentVariable("ENVKEY_3").Returns("EnvValue3");
|
||||
|
||||
var configuration = ConfigurationHelper.MockConfiguration(new Dictionary<string, string?>()
|
||||
{
|
||||
{ "Release:Version", "v1.0.0" },
|
||||
{ "Release:Plugin:Version", "v3.0.0" },
|
||||
|
||||
{ "String1:Key1", "Value1" },
|
||||
{ "String1:Key2", "Value2" },
|
||||
{ "String2:Key1", "Value3" },
|
||||
|
||||
{ "Int1:Key1", "1" },
|
||||
{ "Int1:Key2", "2" },
|
||||
{ "Int2:Key1", "3" },
|
||||
|
||||
{ "Long1:Key1", "4" },
|
||||
{ "Long1:Key2", "5" },
|
||||
{ "Long2:Key1", "6" },
|
||||
|
||||
{ "Decimal1:Key1", "4.12" },
|
||||
{ "Decimal1:Key2", "5.45" },
|
||||
{ "Decimal2:Key1", "6.733" },
|
||||
|
||||
{ "Double1:Key1", "4.12" },
|
||||
{ "Double1:Key2", "5.45" },
|
||||
{ "Double2:Key1", "6.733" },
|
||||
|
||||
{ "Boolean1:Key1", "true" },
|
||||
{ "Boolean1:Key2", "false" },
|
||||
{ "Boolean2:Key1", "true" },
|
||||
|
||||
{ "DateTime1:Key1", "1987-11-20 08:00:00" },
|
||||
{ "DateTime1:Key2", "2000-08-12 12:00:00" },
|
||||
{ "DateTime2:Key1", "2018-01-01 20:30:10" },
|
||||
});
|
||||
_config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());
|
||||
|
||||
_database.GetSetting("Setting1").Returns(new SettingModel() { Value = "001" });
|
||||
_database.GetSetting("Setting2").Returns(new SettingModel() { Value = "002" });
|
||||
_database.GetSetting("Version").Returns(new SettingModel() { Value = "v4.0.0" });
|
||||
|
||||
_database.AddSetting(Arg.Any<SettingModel>()).Returns(new SettingModel() { Value = "Added" });
|
||||
_database.EditSetting(Arg.Any<SettingModel>()).Returns(new SettingModel() { Value = "Updated" });
|
||||
_database.SetSetting(Arg.Any<SettingModel>()).Returns(new SettingModel() { Value = "Set" });
|
||||
_database.DeleteSetting(Arg.Any<SettingModel>()).Returns(true);
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
}
|
||||
|
||||
[TestCase(null, null)]
|
||||
[TestCase("", null)]
|
||||
[TestCase("Setting1", "001")]
|
||||
[TestCase("Setting2", "002")]
|
||||
public async Task SettingsHelper_GetSetting(string key, string expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).Get(key);
|
||||
|
||||
Assert.That(actual?.Value, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase(null, null)]
|
||||
[TestCase("", null)]
|
||||
[TestCase("Setting1", "Set")]
|
||||
public async Task SettingsHelper_SetSetting(string key, string expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).SetSetting(key, "FakeValue");
|
||||
|
||||
Assert.That(actual?.Value, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Setting1", true)]
|
||||
public async Task SettingsHelper_DeleteSetting(string key, bool expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).DeleteSetting(key);
|
||||
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("String1:Key1", "Default", "Value1")]
|
||||
[TestCase("String1:Key2", "Default", "Value2")]
|
||||
[TestCase("String2:Key1", "Default", "Value3")]
|
||||
[TestCase("String2:Key2", "Default", "Default")]
|
||||
[TestCase("Release:Version", "v0.0.0", "v1.0.0")]
|
||||
[TestCase("Release:Plugin:Version", "v0.0.0", "v3.0.0")]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, string defaultValue, string expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Int1:Key1", 999, 1)]
|
||||
[TestCase("Int1:Key2", 999, 2)]
|
||||
[TestCase("Int2:Key1", 999, 3)]
|
||||
[TestCase("Int2:Key2", 999, 999)]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, int defaultValue, int expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Long1:Key1", 999, 4)]
|
||||
[TestCase("Long1:Key2", 999, 5)]
|
||||
[TestCase("Long2:Key1", 999, 6)]
|
||||
[TestCase("Long2:Key2", 999, 999)]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, long defaultValue, long expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Decimal1:Key1", 999, 4.12)]
|
||||
[TestCase("Decimal1:Key2", 999, 5.45)]
|
||||
[TestCase("Decimal2:Key1", 999, 6.733)]
|
||||
[TestCase("Decimal2:Key2", 999, 999)]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, decimal defaultValue, decimal expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Double1:Key1", 999, 4.12)]
|
||||
[TestCase("Double1:Key2", 999, 5.45)]
|
||||
[TestCase("Double2:Key1", 999, 6.733)]
|
||||
[TestCase("Double2:Key2", 999, 999)]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, double defaultValue, double expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("Boolean1:Key1", false, true)]
|
||||
[TestCase("Boolean1:Key2", false, false)]
|
||||
[TestCase("Boolean2:Key1", false, true)]
|
||||
[TestCase("Boolean2:Key2", true, true)]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, bool defaultValue, bool expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("DateTime1:Key1", null, "1987-11-20 08:00:00")]
|
||||
[TestCase("DateTime1:Key2", null, "2000-08-12 12:00:00")]
|
||||
[TestCase("DateTime2:Key1", null, "2018-01-01 20:30:10")]
|
||||
[TestCase("DateTime2:Key2", null, null)]
|
||||
[TestCase("DateTime3:Key3", "2350-05-05 00:05:12", "2350-05-05 00:05:12")]
|
||||
public async Task SettingsHelper_GetOrDefault(string key, DateTime? defaultValue, string? expected)
|
||||
{
|
||||
var actual = await new SettingsHelper(_database, _config, _logger).GetOrDefault(key, defaultValue);
|
||||
Assert.That(actual, Is.EqualTo(!string.IsNullOrWhiteSpace(expected) ? DateTime.Parse(expected) : null));
|
||||
}
|
||||
|
||||
[TestCase("1.0.0", 3, "1.0.0")]
|
||||
[TestCase("1.0.0", 4, "1.0.0.0")]
|
||||
[TestCase("1.0.0.0", 3, "1.0.0")]
|
||||
[TestCase("1.0.0.0", 4, "1.0.0.0")]
|
||||
[TestCase("1.2.3.4", 2, "1.2")]
|
||||
public async Task SettingsHelper_GetReleaseVersion(string version, int places, string expected)
|
||||
{
|
||||
var environment = Substitute.For<IEnvironmentWrapper>();
|
||||
var configuration = ConfigurationHelper.MockConfiguration(new Dictionary<string, string?>()
|
||||
{
|
||||
{ "Release:Version", version },
|
||||
});
|
||||
var config = new ConfigHelper(environment, configuration, Substitute.For<ILogger<ConfigHelper>>());
|
||||
|
||||
var actual = await new SettingsHelper(_database, config, _logger).GetReleaseVersion(places);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
136
WeddingShare.UnitTests/Tests/Helpers/UrlHelper.cs
Normal file
136
WeddingShare.UnitTests/Tests/Helpers/UrlHelper.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.UnitTests.Helpers;
|
||||
|
||||
namespace WeddingShare.UnitTests.Tests.Helpers
|
||||
{
|
||||
public class UrlHelperTests
|
||||
{
|
||||
private readonly ISettingsHelper _settings = Substitute.For<ISettingsHelper>();
|
||||
|
||||
public UrlHelperTests()
|
||||
{
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_settings.GetOrDefault(Settings.Basic.ForceHttps, Arg.Any<bool>()).Returns(false);
|
||||
}
|
||||
|
||||
[TestCase("http", "unittest.com", null, "http://unittest.com/")]
|
||||
[TestCase("https", "unittest.org", null, "https://unittest.org/")]
|
||||
[TestCase("http", "www.unittest.com", null, "http://www.unittest.com/")]
|
||||
[TestCase("https", "mobile.unittest.org", null, "https://mobile.unittest.org/")]
|
||||
[TestCase("http", "unittest.com", "", "http://unittest.com/")]
|
||||
[TestCase("https", "unittest.org", "", "https://unittest.org/")]
|
||||
[TestCase("http", "www.unittest.com", "", "http://www.unittest.com/")]
|
||||
[TestCase("https", "mobile.unittest.org", "", "https://mobile.unittest.org/")]
|
||||
[TestCase("http", "unittest.com", "/unittest", "http://unittest.com/unittest")]
|
||||
[TestCase("https", "unittest.org", "?unit=test", "https://unittest.org/?unit=test")]
|
||||
[TestCase("http", "www.unittest.com", "?unit=test&blaa=test", "http://www.unittest.com/?unit=test&blaa=test")]
|
||||
[TestCase("https", "mobile.unittest.org", "/unittest?unit=test&blaa=test", "https://mobile.unittest.org/unittest?unit=test&blaa=test")]
|
||||
public void UrlHelper_GenerateBaseUrl(string scheme, string host, string? querystring, string expected)
|
||||
{
|
||||
_settings.GetOrDefault(Settings.Basic.BaseUrl, Arg.Any<string>()).Returns(host);
|
||||
|
||||
var mockContext = MockData.MockHttpContext();
|
||||
mockContext.Request.Scheme = scheme;
|
||||
mockContext.Request.Host = new HostString(host);
|
||||
|
||||
var actual = new UrlHelper(_settings).GenerateBaseUrl(mockContext?.Request, querystring);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("?a=123&b=456&c=789", "d=111", "", "?a=123&b=456&c=789&d=111")]
|
||||
[TestCase("?a=123&b=456&c=789", "d=111,e=222", "", "?a=123&b=456&c=789&d=111&e=222")]
|
||||
[TestCase("?a=123&b=456&c=789", "e=222,f=333,d=111", "", "?a=123&b=456&c=789&e=222&f=333&d=111")]
|
||||
[TestCase("?a=123&b=456&c=789", "d=t$&", "", "?a=123&b=456&c=789&d=t%24%26")]
|
||||
public void UrlHelper_GenerateQueryString_Append(string queryString, string append, string exclude, string expected)
|
||||
{
|
||||
var mockContext = MockData.MockHttpContext();
|
||||
mockContext.Request.Scheme = "https";
|
||||
mockContext.Request.Host = new HostString("unit.test.com");
|
||||
mockContext.Request.QueryString = new QueryString(queryString);
|
||||
|
||||
var include = append?.Split(',', StringSplitOptions.RemoveEmptyEntries)?.Select(x => {
|
||||
var val = x.Split('=');
|
||||
return new KeyValuePair<string, string>(val.FirstOrDefault() ?? "default", val.LastOrDefault() ?? "default");
|
||||
})?.ToList();
|
||||
|
||||
var actual = new UrlHelper(_settings).GenerateQueryString(mockContext?.Request, include, exclude?.Split(',', StringSplitOptions.RemoveEmptyEntries)?.ToList());
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("?a=123&b=456&c=789", "", "", "?a=123&b=456&c=789")]
|
||||
[TestCase("?a=123&b=456&c=789", "", "d", "?a=123&b=456&c=789")]
|
||||
[TestCase("?a=123&b=456&c=789", "", "a", "?b=456&c=789")]
|
||||
[TestCase("?a=123&b=456&c=789", "", "b", "?a=123&c=789")]
|
||||
[TestCase("?a=123&b=456&c=789", "", "c", "?a=123&b=456")]
|
||||
public void UrlHelper_GenerateQueryString_Exclude(string queryString, string append, string exclude, string expected)
|
||||
{
|
||||
var mockContext = MockData.MockHttpContext();
|
||||
mockContext.Request.Scheme = "https";
|
||||
mockContext.Request.Host = new HostString("unit.test.com");
|
||||
mockContext.Request.QueryString = new QueryString(queryString);
|
||||
|
||||
var include = append?.Split(',', StringSplitOptions.RemoveEmptyEntries)?.Select(x => {
|
||||
var val = x.Split('=');
|
||||
return new KeyValuePair<string, string>(val.FirstOrDefault() ?? "default", val.LastOrDefault() ?? "default");
|
||||
})?.ToList();
|
||||
|
||||
var actual = new UrlHelper(_settings).GenerateQueryString(mockContext?.Request, include, exclude?.Split(',', StringSplitOptions.RemoveEmptyEntries)?.ToList());
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("?a=123&b=456&c=789", "d=111", "a", "?b=456&c=789&d=111")]
|
||||
[TestCase("?a=123&b=456&c=789", "d=111,e=222", "b", "?a=123&c=789&d=111&e=222")]
|
||||
[TestCase("?a=123&b=456&c=789", "e=222,f=333,d=111", "c", "?a=123&b=456&e=222&f=333&d=111")]
|
||||
[TestCase("?a=123&b=456&c=789", "d=111,e=222,f=333", "e", "?a=123&b=456&c=789&d=111&e=222&f=333")]
|
||||
public void UrlHelper_GenerateQueryString_AppendExclude(string queryString, string append, string exclude, string expected)
|
||||
{
|
||||
var mockContext = MockData.MockHttpContext();
|
||||
mockContext.Request.Scheme = "https";
|
||||
mockContext.Request.Host = new HostString("unit.test.com");
|
||||
mockContext.Request.QueryString = new QueryString(queryString);
|
||||
|
||||
var include = append?.Split(',', StringSplitOptions.RemoveEmptyEntries)?.Select(x => {
|
||||
var val = x.Split('=');
|
||||
return new KeyValuePair<string, string>(val.FirstOrDefault() ?? "default", val.LastOrDefault() ?? "default");
|
||||
})?.ToList();
|
||||
|
||||
var actual = new UrlHelper(_settings).GenerateQueryString(mockContext?.Request, include, exclude?.Split(',', StringSplitOptions.RemoveEmptyEntries)?.ToList());
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("unit.test.com", "unit.test.com")]
|
||||
[TestCase("unit.test.com/", "unit.test.com")]
|
||||
[TestCase("http://unit.test.com", "unit.test.com")]
|
||||
[TestCase("http://unit.test.com/", "unit.test.com")]
|
||||
[TestCase("https://unit.test.com", "unit.test.com")]
|
||||
[TestCase("https://unit.test.com/", "unit.test.com")]
|
||||
[TestCase("http://test.com/", "test.com")]
|
||||
[TestCase("https://test.com/", "test.com")]
|
||||
public void UrlHelper_ExtractHost(string host, string expected)
|
||||
{
|
||||
var actual = new UrlHelper(_settings).ExtractHost(host);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
|
||||
[TestCase("?a=123&b=456&c=789", "a", "123")]
|
||||
[TestCase("?a=123&b=456&c=789", "b", "456")]
|
||||
[TestCase("?a=123&b=456&c=789", "c", "789")]
|
||||
[TestCase("?a=123&b=456&c=789", "d", "")]
|
||||
public void UrlHelper_ExtractHost(string queryString, string key, string expected)
|
||||
{
|
||||
var mockContext = MockData.MockHttpContext();
|
||||
mockContext.Request.Scheme = "https";
|
||||
mockContext.Request.Host = new HostString("unit.test.com");
|
||||
mockContext.Request.QueryString = new QueryString(queryString);
|
||||
|
||||
var actual = new UrlHelper(_settings).ExtractQueryValue(mockContext?.Request, key);
|
||||
Assert.That(actual, Is.EqualTo(expected));
|
||||
}
|
||||
}
|
||||
}
|
||||
30
WeddingShare.UnitTests/WeddingShare.UnitTests.csproj
Normal file
30
WeddingShare.UnitTests/WeddingShare.UnitTests.csproj
Normal file
@@ -0,0 +1,30 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="dbup-sqlite" Version="6.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.0" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="NUnit" Version="3.13.3" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
|
||||
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\WeddingShare\WeddingShare.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -5,6 +5,8 @@ VisualStudioVersion = 17.8.34511.84
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WeddingShare", "WeddingShare\WeddingShare.csproj", "{1E5B4227-C87D-42DA-9946-4F86C9034F35}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WeddingShare.UnitTests", "WeddingShare.UnitTests\WeddingShare.UnitTests.csproj", "{B1BAAF2F-454D-4B57-AD6B-9701D73EB8C6}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -15,6 +17,10 @@ Global
|
||||
{1E5B4227-C87D-42DA-9946-4F86C9034F35}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{1E5B4227-C87D-42DA-9946-4F86C9034F35}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{1E5B4227-C87D-42DA-9946-4F86C9034F35}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{B1BAAF2F-454D-4B57-AD6B-9701D73EB8C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{B1BAAF2F-454D-4B57-AD6B-9701D73EB8C6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{B1BAAF2F-454D-4B57-AD6B-9701D73EB8C6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{B1BAAF2F-454D-4B57-AD6B-9701D73EB8C6}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
55
WeddingShare/Attributes/RequiresRoleAttribute.cs
Normal file
55
WeddingShare/Attributes/RequiresRoleAttribute.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Extensions;
|
||||
using WeddingShare.Models;
|
||||
|
||||
namespace WeddingShare.Attributes
|
||||
{
|
||||
public class RequiresRoleAttribute : ActionFilterAttribute
|
||||
{
|
||||
public UserLevel User { get; set; } = UserLevel.Free;
|
||||
public ReviewPermissions ReviewPermission { get; set; } = ReviewPermissions.None;
|
||||
public GalleryPermissions GalleryPermission { get; set; } = GalleryPermissions.None;
|
||||
public UserPermissions UserPermission { get; set; } = UserPermissions.None;
|
||||
public CustomResourcePermissions CustomResourcePermission { get; set; } = CustomResourcePermissions.None;
|
||||
public SettingsPermissions SettingsPermission { get; set; } = SettingsPermissions.None;
|
||||
public AuditPermissions AuditPermission { get; set; } = AuditPermissions.None;
|
||||
public DataPermissions DataPermission { get; set; } = DataPermissions.None;
|
||||
|
||||
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
var level = filterContext.HttpContext?.User?.Identity?.GetUserLevel() ?? UserLevel.Free;
|
||||
if (level < this.User)
|
||||
{
|
||||
filterContext.Result = new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.Unauthorized }, false);
|
||||
}
|
||||
|
||||
var pemissions = filterContext.HttpContext?.User?.Identity?.GetUserPermissions() ?? new Permissions();
|
||||
if (
|
||||
(pemissions.Review != ReviewPermissions.None && !pemissions.Review.HasFlag(this.ReviewPermission))
|
||||
|| (pemissions.Gallery != GalleryPermissions.None && !pemissions.Gallery.HasFlag(this.GalleryPermission))
|
||||
|| (pemissions.Users != UserPermissions.None && !pemissions.Users.HasFlag(this.UserPermission))
|
||||
|| (pemissions.CustomResources != CustomResourcePermissions.None && !pemissions.CustomResources.HasFlag(this.CustomResourcePermission))
|
||||
|| (pemissions.Settings != SettingsPermissions.None && !pemissions.Settings.HasFlag(this.SettingsPermission))
|
||||
|| (pemissions.Audit != AuditPermissions.None && !pemissions.Audit.HasFlag(this.AuditPermission))
|
||||
|| (pemissions.Data != DataPermissions.None && !pemissions.Data.HasFlag(this.DataPermission))
|
||||
)
|
||||
{
|
||||
filterContext.Result = new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.Unauthorized }, false);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = filterContext.HttpContext.RequestServices.GetService<ILogger<RequiresSecretKeyAttribute>>();
|
||||
if (logger != null)
|
||||
{
|
||||
logger.LogError(ex, $"Failed to validate user role - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
85
WeddingShare/Attributes/RequiresSecretKeyAttribute.cs
Normal file
85
WeddingShare/Attributes/RequiresSecretKeyAttribute.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Web;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
|
||||
namespace WeddingShare.Attributes
|
||||
{
|
||||
public class RequiresSecretKeyAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
||||
{
|
||||
try
|
||||
{
|
||||
int? galleryId = null;
|
||||
|
||||
var request = filterContext.HttpContext.Request;
|
||||
var databaseHelper = filterContext.HttpContext.RequestServices.GetService<IDatabaseHelper>();
|
||||
if (databaseHelper != null)
|
||||
{
|
||||
var galleryIdentifier = (request.Query.ContainsKey("identifier") && !string.IsNullOrWhiteSpace(request.Query["identifier"])) ? request.Query["identifier"].ToString().ToLower() : null;
|
||||
if (!string.IsNullOrWhiteSpace(galleryIdentifier))
|
||||
{
|
||||
galleryId = databaseHelper.GetGalleryId(galleryIdentifier).Result;
|
||||
}
|
||||
|
||||
if (galleryId == null)
|
||||
{
|
||||
var galleryName = (request.Query.ContainsKey("id") && !string.IsNullOrWhiteSpace(request.Query["id"])) ? request.Query["id"].ToString().ToLower() : "default";
|
||||
if (!string.IsNullOrWhiteSpace(galleryName))
|
||||
{
|
||||
galleryId = (databaseHelper?.GetGalleryIdByName(galleryName)?.Result) ?? 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (galleryId != null)
|
||||
{
|
||||
var gallery = databaseHelper?.GetGallery(galleryId.Value).Result;
|
||||
if (gallery != null)
|
||||
{
|
||||
var encryptionHelper = filterContext.HttpContext.RequestServices.GetService<IEncryptionHelper>();
|
||||
if (encryptionHelper != null)
|
||||
{
|
||||
var key = request.Query.ContainsKey("key") ? request.Query["key"].ToString() : string.Empty;
|
||||
|
||||
var isEncrypted = request.Query.ContainsKey("enc") ? bool.Parse(request.Query["enc"].ToString().ToLower()) : false;
|
||||
if (!isEncrypted && !string.IsNullOrWhiteSpace(key) && encryptionHelper.IsEncryptionEnabled())
|
||||
{
|
||||
var queryString = HttpUtility.ParseQueryString(request.QueryString.ToString());
|
||||
queryString.Set("enc", "true");
|
||||
queryString.Set("key", encryptionHelper.Encrypt(key));
|
||||
|
||||
filterContext.Result = new RedirectResult($"/Gallery?{queryString.ToString()}");
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(gallery.SecretKey))
|
||||
{
|
||||
var secretKey = encryptionHelper.IsEncryptionEnabled() ? encryptionHelper.Encrypt(gallery.SecretKey) : gallery.SecretKey;
|
||||
if (!string.IsNullOrWhiteSpace(secretKey) && !string.Equals(secretKey, key))
|
||||
{
|
||||
var logger = filterContext.HttpContext.RequestServices.GetService<ILogger<RequiresSecretKeyAttribute>>();
|
||||
if (logger != null)
|
||||
{
|
||||
logger.LogWarning($"A request was made to an endpoint with an invalid secure key");
|
||||
}
|
||||
|
||||
filterContext.Result = new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.InvalidSecretKey }, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var logger = filterContext.HttpContext.RequestServices.GetService<ILogger<RequiresSecretKeyAttribute>>();
|
||||
if (logger != null)
|
||||
{
|
||||
logger.LogError(ex, $"Failed to validate secure key - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
77
WeddingShare/BackgroundWorkers/CleanupService.cs
Normal file
77
WeddingShare/BackgroundWorkers/CleanupService.cs
Normal file
@@ -0,0 +1,77 @@
|
||||
using NCrontab;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.BackgroundWorkers
|
||||
{
|
||||
public sealed class CleanupService(IWebHostEnvironment hostingEnvironment, ISettingsHelper settingsHelper, IFileHelper fileHelper, ILogger<CleanupService> logger) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var enabled = await settingsHelper.GetOrDefault(BackgroundServices.Cleanup.Enabled, true);
|
||||
if (enabled)
|
||||
{
|
||||
var cron = await settingsHelper.GetOrDefault(BackgroundServices.Cleanup.Schedule, "0 4 * * *");
|
||||
var nextExecutionTime = DateTime.Now.AddMinutes(1);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var currentCron = await settingsHelper.GetOrDefault(BackgroundServices.Cleanup.Schedule, "0 4 * * *");
|
||||
|
||||
var now = DateTime.Now;
|
||||
if (now >= nextExecutionTime)
|
||||
{
|
||||
await Cleanup();
|
||||
|
||||
var schedule = CrontabSchedule.Parse(cron, new CrontabSchedule.ParseOptions() { IncludingSeconds = cron.Split(new[] { ' ' }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Length == 6 });
|
||||
nextExecutionTime = schedule.GetNextOccurrence(now);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!currentCron.Equals(cron))
|
||||
{
|
||||
nextExecutionTime = DateTime.Now;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
|
||||
}
|
||||
|
||||
cron = currentCron;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task Cleanup()
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Run(() =>
|
||||
{
|
||||
var paths = new List<string>()
|
||||
{
|
||||
Path.Combine(hostingEnvironment.WebRootPath, Directories.TempFiles)
|
||||
};
|
||||
|
||||
if (paths != null)
|
||||
{
|
||||
foreach (var path in paths)
|
||||
{
|
||||
try
|
||||
{
|
||||
fileHelper.DeleteDirectoryIfExists(path);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"An error occurred while running cleanup of '{path}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"CleanupService - Failed to clean up files - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
280
WeddingShare/BackgroundWorkers/DirectoryScanner.cs
Normal file
280
WeddingShare/BackgroundWorkers/DirectoryScanner.cs
Normal file
@@ -0,0 +1,280 @@
|
||||
using NCrontab;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.Processing;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.BackgroundWorkers
|
||||
{
|
||||
public sealed class DirectoryScanner(IWebHostEnvironment hostingEnvironment, ISettingsHelper settingsHelper, IDatabaseHelper databaseHelper, IFileHelper fileHelper, IImageHelper imageHelper, ILogger<DirectoryScanner> logger) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var enabled = await settingsHelper.GetOrDefault(BackgroundServices.DirectoryScanner.Enabled, true);
|
||||
if (enabled)
|
||||
{
|
||||
var cron = await settingsHelper.GetOrDefault(BackgroundServices.DirectoryScanner.Schedule, "*/30 * * * *");
|
||||
var nextExecutionTime = DateTime.Now.AddMinutes(1);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var currentCron = await settingsHelper.GetOrDefault(BackgroundServices.DirectoryScanner.Schedule, "*/30 * * * *");
|
||||
|
||||
var now = DateTime.Now;
|
||||
if (now >= nextExecutionTime)
|
||||
{
|
||||
await ScanForFiles();
|
||||
|
||||
var schedule = CrontabSchedule.Parse(cron, new CrontabSchedule.ParseOptions() { IncludingSeconds = cron.Split(new[] { ' ' }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Length == 6 });
|
||||
nextExecutionTime = schedule.GetNextOccurrence(now);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!currentCron.Equals(cron))
|
||||
{
|
||||
nextExecutionTime = DateTime.Now;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
|
||||
}
|
||||
|
||||
cron = currentCron;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ScanForFiles()
|
||||
{
|
||||
if (Startup.Ready)
|
||||
{
|
||||
await this.ScanGalleryImages();
|
||||
await this.ScanCustomResources();
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogInformation($"Skipping directory scan, application not ready yet");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ScanGalleryImages()
|
||||
{
|
||||
try
|
||||
{
|
||||
var thumbnailsDirectory = Path.Combine(hostingEnvironment.WebRootPath, Directories.Thumbnails);
|
||||
fileHelper.CreateDirectoryIfNotExists(thumbnailsDirectory);
|
||||
|
||||
var uploadsDirectory = Path.Combine(hostingEnvironment.WebRootPath, Directories.Uploads);
|
||||
if (fileHelper.DirectoryExists(uploadsDirectory))
|
||||
{
|
||||
var galleryDirs = fileHelper.GetDirectories(uploadsDirectory, "*", SearchOption.TopDirectoryOnly)?.Where(x => !Path.GetFileName(x).StartsWith("."));
|
||||
if (galleryDirs != null)
|
||||
{
|
||||
foreach (var galleryDir in galleryDirs)
|
||||
{
|
||||
try
|
||||
{
|
||||
var galleryName = Path.GetFileName(galleryDir).ToLower();
|
||||
var identifier = galleryName;
|
||||
|
||||
var galleryId = await databaseHelper.GetGalleryId(identifier);
|
||||
if (galleryId == null && await databaseHelper.GetGalleryCount() < await settingsHelper.GetOrDefault(Settings.Basic.MaxGalleryCount, 1000000))
|
||||
{
|
||||
identifier = GalleryHelper.IsValidGalleryIdentifier(galleryName) ? galleryName : GalleryHelper.GenerateGalleryIdentifier();
|
||||
galleryId = (await databaseHelper.AddGallery(new GalleryModel()
|
||||
{
|
||||
Identifier = identifier,
|
||||
Name = galleryName,
|
||||
SecretKey = PasswordHelper.GenerateGallerySecretKey(),
|
||||
Owner = 0
|
||||
}))?.Id;
|
||||
}
|
||||
|
||||
if (galleryId != null)
|
||||
{
|
||||
var galleryItem = await databaseHelper.GetGallery(galleryId.Value);
|
||||
if (galleryItem != null)
|
||||
{
|
||||
var galleryPath = Path.Combine(uploadsDirectory, galleryItem.Identifier);
|
||||
if (!galleryDir.Equals(galleryPath))
|
||||
{
|
||||
fileHelper.MoveDirectoryIfExists(galleryDir, galleryPath);
|
||||
}
|
||||
|
||||
var allowedFileTypes = settingsHelper.GetOrDefault(Settings.Gallery.AllowedFileTypes, ".jpg,.jpeg,.png,.mp4,.mov", galleryItem?.Id).Result.Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
var galleryItems = await databaseHelper.GetAllGalleryItems(galleryItem.Id);
|
||||
|
||||
if (Path.Exists(galleryPath))
|
||||
{
|
||||
var approvedFiles = fileHelper.GetFiles(galleryPath, "*.*", SearchOption.TopDirectoryOnly).Where(x => allowedFileTypes.Any(y => string.Equals(Path.GetExtension(x).Trim('.'), y.Trim('.'), StringComparison.OrdinalIgnoreCase)));
|
||||
if (approvedFiles != null)
|
||||
{
|
||||
foreach (var file in approvedFiles)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filename = Path.GetFileName(file);
|
||||
var g = galleryItems.FirstOrDefault(x => string.Equals(x.Title, filename, StringComparison.OrdinalIgnoreCase));
|
||||
if (g == null)
|
||||
{
|
||||
g = await databaseHelper.AddGalleryItem(new GalleryItemModel()
|
||||
{
|
||||
GalleryId = galleryItem.Id,
|
||||
Title = filename,
|
||||
Checksum = await fileHelper.GetChecksum(file),
|
||||
MediaType = imageHelper.GetMediaType(file),
|
||||
State = GalleryItemState.Approved,
|
||||
UploadedDate = await fileHelper.GetCreationDatetime(file),
|
||||
FileSize = fileHelper.FileSize(file)
|
||||
});
|
||||
}
|
||||
|
||||
var thumbnailDir = Path.Combine(thumbnailsDirectory, galleryItem.Identifier);
|
||||
var thumbnailPath = Path.Combine(thumbnailDir, $"{Path.GetFileNameWithoutExtension(file)}.webp");
|
||||
if (!fileHelper.FileExists(thumbnailPath))
|
||||
{
|
||||
fileHelper.CreateDirectoryIfNotExists(thumbnailDir);
|
||||
await imageHelper.GenerateThumbnail(file, thumbnailPath, settingsHelper.GetOrDefault(Settings.Basic.ThumbnailSize, 720).Result);
|
||||
fileHelper.DeleteFileIfExists(Path.Combine(thumbnailsDirectory, $"{Path.GetFileNameWithoutExtension(file)}.webp"));
|
||||
}
|
||||
else
|
||||
{
|
||||
using (var img = await Image.LoadAsync(thumbnailPath))
|
||||
{
|
||||
var width = img.Width;
|
||||
|
||||
img.Mutate(x => x.AutoOrient());
|
||||
|
||||
if (width != img.Width)
|
||||
{
|
||||
await img.SaveAsWebpAsync(thumbnailPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g != null)
|
||||
{
|
||||
var updated = false;
|
||||
|
||||
if (g.UploadedDate == null)
|
||||
{
|
||||
g.UploadedDate = new FileInfo(file).CreationTimeUtc;
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (g.MediaType == MediaType.Unknown)
|
||||
{
|
||||
g.MediaType = imageHelper.GetMediaType(file);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (g.Orientation == ImageOrientation.None)
|
||||
{
|
||||
g.Orientation = await imageHelper.GetOrientation(thumbnailPath);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (g.FileSize == 0)
|
||||
{
|
||||
g.FileSize = fileHelper.FileSize(file);
|
||||
updated = true;
|
||||
}
|
||||
|
||||
if (updated)
|
||||
{
|
||||
await databaseHelper.EditGalleryItem(g);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"An error occurred while scanning file '{file}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (Path.Exists(Path.Combine(galleryPath, "Pending")))
|
||||
{
|
||||
var pendingFiles = fileHelper.GetFiles(Path.Combine(galleryPath, "Pending"), "*.*", SearchOption.TopDirectoryOnly).Where(x => allowedFileTypes.Any(y => string.Equals(Path.GetExtension(x).Trim('.'), y.Trim('.'), StringComparison.OrdinalIgnoreCase)));
|
||||
if (pendingFiles != null)
|
||||
{
|
||||
foreach (var file in pendingFiles)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filename = Path.GetFileName(file);
|
||||
if (!galleryItems.Exists(x => string.Equals(x.Title, filename, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
await databaseHelper.AddGalleryItem(new GalleryItemModel()
|
||||
{
|
||||
GalleryId = galleryItem.Id,
|
||||
Title = filename,
|
||||
Checksum = await fileHelper.GetChecksum(file),
|
||||
MediaType = imageHelper.GetMediaType(file),
|
||||
State = GalleryItemState.Pending,
|
||||
UploadedDate = await fileHelper.GetCreationDatetime(file),
|
||||
FileSize = new FileInfo(file).Length
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"An error occurred while scanning file '{file}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"An error occurred while scanning directory '{galleryDir}'");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"DirectoryScanner - ScanGalleryImages - Failed to scan files - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task ScanCustomResources()
|
||||
{
|
||||
try
|
||||
{
|
||||
var existing = await databaseHelper.GetAllCustomResources();
|
||||
|
||||
var customResourcesDirectory = Path.Combine(hostingEnvironment.WebRootPath, Directories.CustomResources);
|
||||
fileHelper.CreateDirectoryIfNotExists(customResourcesDirectory);
|
||||
|
||||
foreach (var resource in fileHelper.GetFiles(customResourcesDirectory))
|
||||
{
|
||||
try
|
||||
{
|
||||
var filename = Path.GetFileName(resource);
|
||||
if (!existing.Any(x => filename.Equals(x.FileName, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
await databaseHelper.AddCustomResource(new CustomResourceModel()
|
||||
{
|
||||
FileName = filename,
|
||||
UploadedBy = "DirectoryScanner",
|
||||
Owner = 0
|
||||
});
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, $"DirectoryScanner - ScanCustomResources - Failed to scan files - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
93
WeddingShare/BackgroundWorkers/NotificationReport.cs
Normal file
93
WeddingShare/BackgroundWorkers/NotificationReport.cs
Normal file
@@ -0,0 +1,93 @@
|
||||
using System.Text;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using NCrontab;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
|
||||
namespace WeddingShare.BackgroundWorkers
|
||||
{
|
||||
public sealed class NotificationReport(ISettingsHelper settingsHelper, IDatabaseHelper databaseHelper, ISmtpClientWrapper smtpHelper, ILoggerFactory loggerFactory, IStringLocalizer<Lang.Translations> localizer) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var enabled = await settingsHelper.GetOrDefault(BackgroundServices.EmailReport.Enabled, true);
|
||||
if (enabled)
|
||||
{
|
||||
var cron = await settingsHelper.GetOrDefault(BackgroundServices.EmailReport.Schedule, "0 0 * * *");
|
||||
var nextExecutionTime = DateTime.Now.AddMinutes(1);
|
||||
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
var currentCron = await settingsHelper.GetOrDefault(BackgroundServices.EmailReport.Schedule, "0 0 * * *");
|
||||
|
||||
var now = DateTime.Now;
|
||||
if (now >= nextExecutionTime)
|
||||
{
|
||||
if (await settingsHelper.GetOrDefault(Settings.Basic.EmailReport, true) && await settingsHelper.GetOrDefault(Notifications.Smtp.Enabled, false))
|
||||
{
|
||||
await SendReport();
|
||||
}
|
||||
|
||||
var schedule = CrontabSchedule.Parse(cron, new CrontabSchedule.ParseOptions() { IncludingSeconds = cron.Split(new[] { ' ' }, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries).Length == 6 });
|
||||
nextExecutionTime = schedule.GetNextOccurrence(now);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!currentCron.Equals(cron))
|
||||
{
|
||||
nextExecutionTime = DateTime.Now;
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
|
||||
}
|
||||
|
||||
cron = currentCron;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendReport()
|
||||
{
|
||||
try
|
||||
{
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
var pendingItems = await databaseHelper.GetPendingGalleryItems();
|
||||
if (pendingItems != null && pendingItems.Any())
|
||||
{
|
||||
var builder = new StringBuilder();
|
||||
builder.AppendLine($"<h1>You have items pending review!</h1>");
|
||||
|
||||
foreach (var item in pendingItems.GroupBy(x => x.GalleryId).OrderByDescending(x => x.Count()))
|
||||
{
|
||||
var gallery = await databaseHelper.GetGallery(item.Key);
|
||||
if (gallery != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
builder.AppendLine($"<p style=\"font-size: 16pt;\">{gallery.Name} - Pending Items ({item.Count()})</p>");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
loggerFactory.CreateLogger<NotificationReport>().LogError(ex, $"Failed to build gallery report for '{gallery.Name}' - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var sent = await new EmailHelper(settingsHelper, smtpHelper, loggerFactory.CreateLogger<EmailHelper>(), localizer).Send("Pending Items Report", builder.ToString());
|
||||
if (!sent)
|
||||
{
|
||||
loggerFactory.CreateLogger<NotificationReport>().LogWarning($"Failed to send notification report");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
loggerFactory.CreateLogger<NotificationReport>().LogError(ex, $"NotificationReport - Failed to send report - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
32
WeddingShare/Configurations/DatabaseConfiguration.cs
Normal file
32
WeddingShare/Configurations/DatabaseConfiguration.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Helpers.Dbup;
|
||||
|
||||
namespace WeddingShare.Configurations
|
||||
{
|
||||
internal static class DatabaseConfiguration
|
||||
{
|
||||
public static IDatabaseHelper AddDatabaseConfiguration(this IServiceCollection services, IConfigHelper config, ILoggerFactory loggerFactory)
|
||||
{
|
||||
IDatabaseHelper helper;
|
||||
|
||||
var databaseType = config.GetOrDefault(Settings.Database.Type, "sqlite");
|
||||
switch (databaseType?.ToLower())
|
||||
{
|
||||
case "mysql":
|
||||
services.AddSingleton<IDatabaseHelper, MySqlDatabaseHelper>();
|
||||
helper = new MySqlDatabaseHelper(config, loggerFactory.CreateLogger<MySqlDatabaseHelper>());
|
||||
break;
|
||||
default:
|
||||
services.AddSingleton<IDatabaseHelper, SQLiteDatabaseHelper>();
|
||||
helper = new SQLiteDatabaseHelper(config, loggerFactory.CreateLogger<SQLiteDatabaseHelper>());
|
||||
break;
|
||||
}
|
||||
|
||||
services.AddHostedService<DbupMigrator>();
|
||||
|
||||
return helper;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.Configurations
|
||||
{
|
||||
internal static class DependencyInjectionConfiguration
|
||||
{
|
||||
public static void AddDependencyInjectionConfiguration(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<IConfigHelper, ConfigHelper>();
|
||||
services.AddSingleton<IEnvironmentWrapper, EnvironmentWrapper>();
|
||||
services.AddSingleton<ISettingsHelper, SettingsHelper>();
|
||||
services.AddSingleton<IImageHelper, ImageHelper>();
|
||||
services.AddSingleton<IFileHelper, FileHelper>();
|
||||
services.AddSingleton<IDeviceDetector, DeviceDetector>();
|
||||
services.AddSingleton<ISmtpClientWrapper, SmtpClientWrapper>();
|
||||
services.AddSingleton<IEncryptionHelper, EncryptionHelper>();
|
||||
services.AddSingleton<IUrlHelper, UrlHelper>();
|
||||
services.AddSingleton<IAuditHelper, AuditHelper>();
|
||||
services.AddSingleton<ILanguageHelper, LanguageHelper>();
|
||||
}
|
||||
}
|
||||
}
|
||||
55
WeddingShare/Configurations/LocalizationConfiguration.cs
Normal file
55
WeddingShare/Configurations/LocalizationConfiguration.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System.Globalization;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.Configurations
|
||||
{
|
||||
public static class LocalizationConfiguration
|
||||
{
|
||||
public static string CurrentCulture = "en-GB";
|
||||
|
||||
public static void AddLocalizationConfiguration(this IServiceCollection services, SettingsHelper settings)
|
||||
{
|
||||
services.AddLocalization(options =>
|
||||
{
|
||||
options.ResourcesPath = "Resources";
|
||||
});
|
||||
|
||||
services.Configure<RequestLocalizationOptions>(options => {
|
||||
var supportedCultures = new LanguageHelper().DetectSupportedCultures();
|
||||
|
||||
var language = settings.GetOrDefault(Settings.Languages.Default, "en-GB").Result;
|
||||
CurrentCulture = GetDefaultCulture(supportedCultures, language);
|
||||
|
||||
options.DefaultRequestCulture = new RequestCulture(CurrentCulture);
|
||||
options.SupportedCultures = supportedCultures;
|
||||
options.SupportedUICultures = supportedCultures;
|
||||
});
|
||||
}
|
||||
|
||||
private static string GetDefaultCulture(List<CultureInfo> supported, string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var culture in supported)
|
||||
{
|
||||
if (CultureMatches(culture, key))
|
||||
{
|
||||
return culture.Name;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
|
||||
return "en-GB";
|
||||
}
|
||||
|
||||
private static bool CultureMatches(CultureInfo culture, string key)
|
||||
{
|
||||
return string.Equals(culture.Name, key, StringComparison.OrdinalIgnoreCase) || string.Equals(culture.ThreeLetterISOLanguageName, key, StringComparison.OrdinalIgnoreCase) || string.Equals(culture.TwoLetterISOLanguageName, key, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
}
|
||||
}
|
||||
50
WeddingShare/Configurations/NotificationConfiguration.cs
Normal file
50
WeddingShare/Configurations/NotificationConfiguration.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using System.Net.Http.Headers;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
|
||||
namespace WeddingShare.Configurations
|
||||
{
|
||||
internal static class NotificationConfiguration
|
||||
{
|
||||
private const int CLIENT_DEFAULT_TIMEOUT = 10;
|
||||
|
||||
public static void AddNotificationConfiguration(this IServiceCollection services, SettingsHelper settings)
|
||||
{
|
||||
services.AddSingleton<INotificationHelper, NotificationBroker>();
|
||||
services.AddNtfyConfiguration(settings);
|
||||
services.AddGotifyConfiguration(settings);
|
||||
}
|
||||
|
||||
public static void AddNtfyConfiguration(this IServiceCollection services, SettingsHelper settings)
|
||||
{
|
||||
services.AddHttpClient("NtfyClient", (serviceProvider, httpClient) =>
|
||||
{
|
||||
var endpoint = settings.GetOrDefault(Constants.Notifications.Ntfy.Endpoint, string.Empty).Result;
|
||||
if (!string.IsNullOrWhiteSpace(endpoint))
|
||||
{
|
||||
var token = settings.GetOrDefault(Constants.Notifications.Ntfy.Token, string.Empty).Result;
|
||||
if (!string.IsNullOrWhiteSpace(token))
|
||||
{
|
||||
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
|
||||
}
|
||||
|
||||
httpClient.BaseAddress = new Uri(endpoint);
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(CLIENT_DEFAULT_TIMEOUT);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static void AddGotifyConfiguration(this IServiceCollection services, SettingsHelper settings)
|
||||
{
|
||||
services.AddHttpClient("GotifyClient", (serviceProvider, httpClient) =>
|
||||
{
|
||||
var endpoint = settings.GetOrDefault(Constants.Notifications.Gotify.Endpoint, string.Empty).Result;
|
||||
if (!string.IsNullOrWhiteSpace(endpoint))
|
||||
{
|
||||
httpClient.BaseAddress = new Uri(endpoint);
|
||||
httpClient.Timeout = TimeSpan.FromSeconds(CLIENT_DEFAULT_TIMEOUT);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
17
WeddingShare/Configurations/WebClientConfiguration.cs
Normal file
17
WeddingShare/Configurations/WebClientConfiguration.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
|
||||
namespace WeddingShare.Configurations
|
||||
{
|
||||
internal static class WebClientConfiguration
|
||||
{
|
||||
public static void AddWebClientConfiguration(this IServiceCollection services, IConfigHelper config)
|
||||
{
|
||||
services.AddHttpClient("SponsorsClient", (client) =>
|
||||
{
|
||||
client.BaseAddress = new Uri(config.GetOrDefault(Sponsors.Url, "http://localhost:5000/"));
|
||||
client.Timeout = TimeSpan.FromSeconds(5);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
26
WeddingShare/Constants/BackgroundServices.cs
Normal file
26
WeddingShare/Constants/BackgroundServices.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class BackgroundServices
|
||||
{
|
||||
public class DirectoryScanner
|
||||
{
|
||||
public const string BaseKey = "BackgroundServices:Directory_Scanner:";
|
||||
public const string Enabled = "BackgroundServices:Directory_Scanner:Enabled";
|
||||
public const string Schedule = "BackgroundServices:Directory_Scanner:Schedule";
|
||||
}
|
||||
|
||||
public class EmailReport
|
||||
{
|
||||
public const string BaseKey = "BackgroundServices:Email_Report:";
|
||||
public const string Enabled = "BackgroundServices:Email_Report:Enabled";
|
||||
public const string Schedule = "BackgroundServices:Email_Report:Schedule";
|
||||
}
|
||||
|
||||
public class Cleanup
|
||||
{
|
||||
public const string BaseKey = "BackgroundServices:Cleanup:";
|
||||
public const string Enabled = "BackgroundServices:Cleanup:Enabled";
|
||||
public const string Schedule = "BackgroundServices:Cleanup:Schedule";
|
||||
}
|
||||
}
|
||||
}
|
||||
12
WeddingShare/Constants/Directories.cs
Normal file
12
WeddingShare/Constants/Directories.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class Directories
|
||||
{
|
||||
public const string Config = "config";
|
||||
public const string CustomResources = "custom_resources";
|
||||
public const string Images = "images";
|
||||
public const string TempFiles = "temp";
|
||||
public const string Thumbnails = "thumbnails";
|
||||
public const string Uploads = "uploads";
|
||||
}
|
||||
}
|
||||
14
WeddingShare/Constants/ErrorCodes.cs
Normal file
14
WeddingShare/Constants/ErrorCodes.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public static class ErrorCode
|
||||
{
|
||||
public const int UnexpectedError = 400;
|
||||
public const int Unauthorized = 401;
|
||||
public const int InvalidSecretKey = 402;
|
||||
public const int GalleryCreationNotAllowed = 403;
|
||||
public const int GalleryLimitReached = 405;
|
||||
public const int InvalidGalleryId = 406;
|
||||
public const int InvalidVerificationLink = 407;
|
||||
public const int InvalidPasswordResetLink = 408;
|
||||
}
|
||||
}
|
||||
7
WeddingShare/Constants/FFMPEG.cs
Normal file
7
WeddingShare/Constants/FFMPEG.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class FFMPEG
|
||||
{
|
||||
public const string InstallPath = "FFMPEG:InstallPath";
|
||||
}
|
||||
}
|
||||
43
WeddingShare/Constants/Notifications.cs
Normal file
43
WeddingShare/Constants/Notifications.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class Notifications
|
||||
{
|
||||
public class Alerts
|
||||
{
|
||||
public const string FailedLogin = "Notifications:Alerts:Failed_Login";
|
||||
public const string AccountLockout = "Notifications:Alerts:Account_Lockout";
|
||||
public const string DestructiveAction = "Notifications:Alerts:Destructive_Action";
|
||||
public const string PendingReview = "Notifications:Alerts:Pending_Review";
|
||||
}
|
||||
|
||||
public class Gotify
|
||||
{
|
||||
public const string Enabled = "Notifications:Gotify:Enabled";
|
||||
public const string Endpoint = "Notifications:Gotify:Endpoint";
|
||||
public const string Token = "Notifications:Gotify:Token";
|
||||
public const string Priority = "Notifications:Gotify:Priority";
|
||||
}
|
||||
|
||||
public class Ntfy
|
||||
{
|
||||
public const string Enabled = "Notifications:Ntfy:Enabled";
|
||||
public const string Endpoint = "Notifications:Ntfy:Endpoint";
|
||||
public const string Token = "Notifications:Ntfy:Token";
|
||||
public const string Topic = "Notifications:Ntfy:Topic";
|
||||
public const string Priority = "Notifications:Ntfy:Priority";
|
||||
}
|
||||
|
||||
public class Smtp
|
||||
{
|
||||
public const string Enabled = "Notifications:Smtp:Enabled";
|
||||
public const string Recipient = "Notifications:Smtp:Recipient";
|
||||
public const string Host = "Notifications:Smtp:Host";
|
||||
public const string Port = "Notifications:Smtp:Port";
|
||||
public const string Username = "Notifications:Smtp:Username";
|
||||
public const string Password = "Notifications:Smtp:Password";
|
||||
public const string From = "Notifications:Smtp:From";
|
||||
public const string DisplayName = "Notifications:Smtp:DisplayName";
|
||||
public const string UseSSL = "Notifications:Smtp:Use_SSL";
|
||||
}
|
||||
}
|
||||
}
|
||||
7
WeddingShare/Constants/ProtectedValues.cs
Normal file
7
WeddingShare/Constants/ProtectedValues.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class ProtectedValues
|
||||
{
|
||||
public static readonly string[] GalleryNames = [ "All" ];
|
||||
}
|
||||
}
|
||||
7
WeddingShare/Constants/Release.cs
Normal file
7
WeddingShare/Constants/Release.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class Release
|
||||
{
|
||||
public const string Version = "Release:Version";
|
||||
}
|
||||
}
|
||||
26
WeddingShare/Constants/Security.cs
Normal file
26
WeddingShare/Constants/Security.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class Security
|
||||
{
|
||||
public class Encryption
|
||||
{
|
||||
public const string Key = "Security:Encryption:Key";
|
||||
public const string Salt = "Security:Encryption:Salt";
|
||||
public const string Iterations = "Security:Encryption:Iterations";
|
||||
public const string HashType = "Security:Encryption:HashType";
|
||||
}
|
||||
|
||||
public class Headers
|
||||
{
|
||||
public const string Enabled = "Security:Headers:Enabled";
|
||||
public const string XFrameOptions = "Security:Headers:X_Frame_Options";
|
||||
public const string XContentTypeOptions = "Security:Headers:X_Content_Type_Options";
|
||||
public const string CSP = "Security:Headers:CSP";
|
||||
}
|
||||
|
||||
public class MultiFactor
|
||||
{
|
||||
public const string ResetToDefault = "Security:2FA:Reset_To_Default";
|
||||
}
|
||||
}
|
||||
}
|
||||
148
WeddingShare/Constants/Settings.cs
Normal file
148
WeddingShare/Constants/Settings.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class Settings
|
||||
{
|
||||
public const string IsDemoMode = "Settings:Demo_Mode";
|
||||
|
||||
public class Account
|
||||
{
|
||||
public const string BaseKey = "Settings:Account:";
|
||||
public const string ShowProfileIcon = "Settings:Account:Show_Profile_Icon";
|
||||
public const string LockoutAttempts = "Settings:Account:Lockout_Attempts";
|
||||
public const string LockoutMins = "Settings:Account:Lockout_Mins";
|
||||
|
||||
public class Admin
|
||||
{
|
||||
public const string Username = "Settings:Account:Admin:Username";
|
||||
public const string Password = "Settings:Account:Admin:Password";
|
||||
}
|
||||
|
||||
public class Owner
|
||||
{
|
||||
public const string BaseKey = "Settings:Account:Owner:";
|
||||
public const string Username = "Settings:Account:Owner:Username";
|
||||
public const string Password = "Settings:Account:Owner:Password";
|
||||
public const string LogPassword = "Settings:Account:Owner:Log_Password";
|
||||
}
|
||||
|
||||
public class Registration
|
||||
{
|
||||
public const string BaseKey = "Settings:Account:Registration:";
|
||||
public const string Enabled = "Settings:Account:Registration:Enabled";
|
||||
public const string RequireEmailValidation = "Settings:Account:Registration:Require_Email_Validation";
|
||||
}
|
||||
}
|
||||
|
||||
public class Basic
|
||||
{
|
||||
public const string BaseKey = "Settings:";
|
||||
public const string Title = "Settings:Title";
|
||||
public const string Logo = "Settings:Logo";
|
||||
public const string BaseUrl = "Settings:Base_Url";
|
||||
public const string DefaultGallerySecretKey = "Settings:Gallery_Secret_Key";
|
||||
public const string ForceHttps = "Settings:Force_Https";
|
||||
public const string SingleGalleryMode = "Settings:Single_Gallery_Mode";
|
||||
public const string MaxGalleryCount = "Settings:Max_Gallery_Count";
|
||||
public const string HomeLink = "Settings:Home_Link";
|
||||
public const string GuestGalleryCreation = "Settings:Guest_Gallery_Creation";
|
||||
public const string HideKeyFromQRCode = "Settings:Hide_Key_From_QR_Code";
|
||||
public const string LinksOpenNewTab = "Settings:Links_Open_New_Tab";
|
||||
public const string ThumbnailSize = "Settings:Thumbnail_Size";
|
||||
public const string EmailReport = "Settings:Email_Report";
|
||||
}
|
||||
|
||||
public class Database
|
||||
{
|
||||
public const string BaseKey = "Settings:Database:";
|
||||
public const string Type = "Settings:Database:Type";
|
||||
public const string ConnectionString = "Settings:Database:Connection_String";
|
||||
public const string DatabaseName = "Settings:Database:Database_Name";
|
||||
public const string SyncFromConfig = "Settings:Database:Sync_From_Config";
|
||||
}
|
||||
|
||||
public class Gallery
|
||||
{
|
||||
public const string BaseKey = "Settings:Gallery:";
|
||||
public const string ShowTitle = "Settings:Gallery:Show_Title";
|
||||
public const string BannerImage = "Settings:Gallery:Banner_Image";
|
||||
public const string Quote = "Settings:Gallery:Quote";
|
||||
public const string Columns = "Settings:Gallery:Columns";
|
||||
public const string ItemsPerPage = "Settings:Gallery:Items_Per_Page";
|
||||
public const string FullWidth = "Settings:Gallery:Full_Width";
|
||||
public const string RetainRejectedItems = "Settings:Gallery:Retain_Rejected_Items";
|
||||
public const string Likes = "Settings:Gallery:Likes";
|
||||
public const string Upload = "Settings:Gallery:Upload";
|
||||
public const string Download = "Settings:Gallery:Download";
|
||||
public const string RequireReview = "Settings:Gallery:Require_Review";
|
||||
public const string ReviewCounter = "Settings:Gallery:Review_Counter";
|
||||
public const string PreventDuplicates = "Settings:Gallery:Prevent_Duplicates";
|
||||
public const string IdleRefreshMins = "Settings:Gallery:Idle_Refresh_Mins";
|
||||
public const string MaxSizeMB = "Settings:Gallery:Max_Size_MB";
|
||||
public const string MaxFileSizeMB = "Settings:Gallery:Max_File_Size_MB";
|
||||
public const string DefaultView = "Settings:Gallery:Default_View";
|
||||
public const string DefaultGroup = "Settings:Gallery:Default_Group";
|
||||
public const string DefaultOrder = "Settings:Gallery:Default_Order";
|
||||
public const string DefaultFilter = "Settings:Gallery:Default_Filter";
|
||||
public const string UploadPeriod = "Settings:Gallery:Upload_Period";
|
||||
public const string AllowedFileTypes = "Settings:Gallery:Allowed_File_Types";
|
||||
public const string CameraUploads = "Settings:Gallery:Camera_Uploads";
|
||||
public const string ShowFilters = "Settings:Gallery:Show_Filters";
|
||||
|
||||
public class QRCode
|
||||
{
|
||||
public const string BaseKey = "Settings:Gallery:QR_Code:";
|
||||
public const string Enabled = "Settings:Gallery:QR_Code:Enabled";
|
||||
public const string DefaultView = "Settings:Gallery:QR_Code:Default_View";
|
||||
public const string DefaultSort = "Settings:Gallery:QR_Code:Default_Sort";
|
||||
public const string IncludeCulture = "Settings:Gallery:QR_Code:Include_Culture";
|
||||
}
|
||||
}
|
||||
|
||||
public class GallerySelector
|
||||
{
|
||||
public const string BaseKey = "Settings:Gallery_Selector:";
|
||||
public const string Dropdown = "Settings:Gallery_Selector:Dropdown";
|
||||
public const string HideDefaultOption = "Settings:Gallery_Selector:Hide_Default_Option";
|
||||
}
|
||||
|
||||
public class IdentityCheck
|
||||
{
|
||||
public const string BaseKey = "Settings:Identity_Check:";
|
||||
public const string Enabled = "Settings:Identity_Check:Enabled";
|
||||
public const string ShowOnPageLoad = "Settings:Identity_Check:Show_On_Page_Load";
|
||||
public const string RequireIdentityForUpload = "Settings:Identity_Check:Require_Identity_For_Upload";
|
||||
public const string RequireName = "Settings:Identity_Check:Require_Name";
|
||||
public const string RequireEmail = "Settings:Identity_Check:Require_Email";
|
||||
}
|
||||
|
||||
public class Languages
|
||||
{
|
||||
public const string BaseKey = "Settings:Languages:";
|
||||
public const string Enabled = "Settings:Languages:Enabled";
|
||||
public const string Default = "Settings:Languages:Default";
|
||||
}
|
||||
|
||||
public class Slideshow
|
||||
{
|
||||
public const string BaseKey = "Settings:Slideshow:";
|
||||
public const string Interval = "Settings:Slideshow:Interval";
|
||||
public const string Fade = "Settings:Slideshow:Fade";
|
||||
public const string Limit = "Settings:Slideshow:Limit";
|
||||
public const string IncludeShareSlide = "Settings:Slideshow:Include_Share_Slide";
|
||||
}
|
||||
|
||||
public class Themes
|
||||
{
|
||||
public const string BaseKey = "Settings:Themes:";
|
||||
public const string Enabled = "Settings:Themes:Enabled";
|
||||
public const string Default = "Settings:Themes:Default";
|
||||
}
|
||||
|
||||
public class Policies
|
||||
{
|
||||
public const string BaseKey = "Settings:Policies:";
|
||||
public const string Enabled = "Settings:Policies:Enabled";
|
||||
public const string CookiePolicy = "Settings:Policies:CookiePolicy";
|
||||
}
|
||||
}
|
||||
}
|
||||
13
WeddingShare/Constants/Sponsors.cs
Normal file
13
WeddingShare/Constants/Sponsors.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class Sponsors
|
||||
{
|
||||
public const string Url = "Sponsors:Url";
|
||||
public const string Endpoint = "Sponsors:Endpoint";
|
||||
|
||||
public class Github
|
||||
{
|
||||
public const string ProfileUrl = "Sponsors:Github:ProfileUrl";
|
||||
}
|
||||
}
|
||||
}
|
||||
67
WeddingShare/Constants/ViewOptions.cs
Normal file
67
WeddingShare/Constants/ViewOptions.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
namespace WeddingShare.Constants
|
||||
{
|
||||
public class ViewOptions
|
||||
{
|
||||
public static IDictionary<string, string> YesNo = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Yes", "true" },
|
||||
{ "No", "false" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> YesNoInverted = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Yes", "false" },
|
||||
{ "No", "true" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> SingleGalleryMode = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Single", "true" },
|
||||
{ "Multiple", "false" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> GallerySelectorDropdown = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Dropdown", "true" },
|
||||
{ "Input", "false" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> GalleryWidth = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Full Width", "true" },
|
||||
{ "Default", "false" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> GalleryDefaultView = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Default", "default" },
|
||||
{ "Presentation", "presentation" },
|
||||
{ "Slideshow", "slideshow" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> GalleryDefaultGroup = new Dictionary<string, string>()
|
||||
{
|
||||
{ "None", "0" },
|
||||
{ "Date", "1" },
|
||||
{ "MediaType", "2" },
|
||||
{ "Uploader", "3" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> GalleryDefaultOrder = new Dictionary<string, string>()
|
||||
{
|
||||
{ "Ascending", "0" },
|
||||
{ "Descending", "1" },
|
||||
{ "Random", "2" }
|
||||
};
|
||||
|
||||
public static IDictionary<string, string> GalleryDefaultFilter = new Dictionary<string, string>()
|
||||
{
|
||||
{ "All", "0" },
|
||||
{ "Images", "1" },
|
||||
{ "Videos", "2" },
|
||||
{ "Landscape", "3" },
|
||||
{ "Portrait", "4" },
|
||||
{ "Square", "5" }
|
||||
};
|
||||
}
|
||||
}
|
||||
2101
WeddingShare/Controllers/AccountController.cs
Normal file
2101
WeddingShare/Controllers/AccountController.cs
Normal file
File diff suppressed because it is too large
Load Diff
87
WeddingShare/Controllers/BaseController.cs
Normal file
87
WeddingShare/Controllers/BaseController.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.IO.Compression;
|
||||
using WeddingShare.Models;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
public class BaseController : Controller
|
||||
{
|
||||
public BaseController()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
protected async Task<IActionResult> ZipFileResponse(string filename, ZipListing content)
|
||||
{
|
||||
return await ZipFileResponse(filename, new List<ZipListing>() { content });
|
||||
}
|
||||
|
||||
protected async Task<IActionResult> ZipFileResponse(string filename, IEnumerable<ZipListing> contentsList)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(filename) && contentsList != null && contentsList.Count() > 0)
|
||||
{
|
||||
HttpContext.Response.Headers.Append("Content-Type", "application/zip");
|
||||
HttpContext.Response.Headers.Append("Content-Disposition", $"attachment; filename=\"{filename}\"");
|
||||
|
||||
var bodyStream = Response.BodyWriter.AsStream(true);
|
||||
|
||||
using (var zipArchive = new ZipArchive(bodyStream, ZipArchiveMode.Create, true))
|
||||
{
|
||||
foreach (var contents in contentsList.Where(x => !string.IsNullOrWhiteSpace(x.SourcePath)))
|
||||
{
|
||||
var files = contents?.Files?.Where(x => x.StartsWith(contents.SourcePath, StringComparison.OrdinalIgnoreCase));
|
||||
if (files != null && files.Any())
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(contents?.FileName))
|
||||
{
|
||||
using (var ms = new MemoryStream())
|
||||
{
|
||||
using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true))
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
var path = Path.GetRelativePath(contents.SourcePath, file);
|
||||
var archiveEntry = archive.CreateEntry(path);
|
||||
|
||||
using (var es = archiveEntry.Open())
|
||||
using (var fs = System.IO.File.OpenRead(file))
|
||||
{
|
||||
await fs.CopyToAsync(es);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
var relativePath = $"{contents.FileName.TrimStart('/')}";
|
||||
var zipEntry = zipArchive.CreateEntry(!string.IsNullOrWhiteSpace(contents.Directory) ? Path.Combine(contents.Directory, relativePath) : relativePath);
|
||||
|
||||
using (var entryStream = zipEntry.Open())
|
||||
{
|
||||
ms.Seek(0, SeekOrigin.Begin);
|
||||
await ms.CopyToAsync(entryStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var file in files)
|
||||
{
|
||||
var relativePath = Path.GetRelativePath(contents.SourcePath, file);
|
||||
var zipEntry = zipArchive.CreateEntry(!string.IsNullOrWhiteSpace(contents.Directory) ? Path.Combine(contents.Directory, relativePath) : relativePath);
|
||||
|
||||
using (var fs = System.IO.File.OpenRead(file))
|
||||
using (var entryStream = zipEntry.Open())
|
||||
{
|
||||
await fs.CopyToAsync(entryStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new EmptyResult();
|
||||
}
|
||||
}
|
||||
}
|
||||
20
WeddingShare/Controllers/ErrorController.cs
Normal file
20
WeddingShare/Controllers/ErrorController.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
[AllowAnonymous]
|
||||
public class ErrorController : BaseController
|
||||
{
|
||||
public ErrorController()
|
||||
: base()
|
||||
{
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,145 +1,665 @@
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http.Timeouts;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Internal;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using WeddingShare.Attributes;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Extensions;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
using WeddingShare.Models;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
public class GalleryController : Controller
|
||||
[AllowAnonymous]
|
||||
public class GalleryController : BaseController
|
||||
{
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly IConfigHelper _config;
|
||||
private readonly ISettingsHelper _settings;
|
||||
private readonly IDatabaseHelper _database;
|
||||
private readonly IFileHelper _fileHelper;
|
||||
private readonly IDeviceDetector _deviceDetector;
|
||||
private readonly IImageHelper _imageHelper;
|
||||
private readonly INotificationHelper _notificationHelper;
|
||||
private readonly IEncryptionHelper _encryptionHelper;
|
||||
private readonly Helpers.IUrlHelper _urlHelper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
private readonly string UploadsDirectory;
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer;
|
||||
|
||||
public GalleryController(IWebHostEnvironment hostingEnvironment, IConfigHelper config, ILogger<GalleryController> logger)
|
||||
private readonly string ImagesDirectory;
|
||||
private readonly string TempDirectory;
|
||||
private readonly string UploadsDirectory;
|
||||
private readonly string ThumbnailsDirectory;
|
||||
|
||||
public GalleryController(IWebHostEnvironment hostingEnvironment, ISettingsHelper settings, IDatabaseHelper database, IFileHelper fileHelper, IDeviceDetector deviceDetector, IImageHelper imageHelper, INotificationHelper notificationHelper, IEncryptionHelper encryptionHelper, Helpers.IUrlHelper urlHelper, ILogger<GalleryController> logger, IStringLocalizer<Lang.Translations> localizer)
|
||||
: base()
|
||||
{
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_config = config;
|
||||
_settings = settings;
|
||||
_database = database;
|
||||
_fileHelper = fileHelper;
|
||||
_deviceDetector = deviceDetector;
|
||||
_imageHelper = imageHelper;
|
||||
_notificationHelper = notificationHelper;
|
||||
_encryptionHelper = encryptionHelper;
|
||||
_urlHelper = urlHelper;
|
||||
_logger = logger;
|
||||
_localizer = localizer;
|
||||
|
||||
UploadsDirectory = Path.Combine(_hostingEnvironment.WebRootPath, "uploads");
|
||||
ImagesDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.Images);
|
||||
TempDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.TempFiles);
|
||||
UploadsDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.Uploads);
|
||||
ThumbnailsDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.Thumbnails);
|
||||
}
|
||||
|
||||
public IActionResult Index(string id, string? key)
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Login(string? id, string? identifier, string? key = null)
|
||||
{
|
||||
id = id.ToLower();
|
||||
int? galleryId = 0;
|
||||
|
||||
var secretKey = _config.Get("Settings", "Secret_Key");
|
||||
if (!string.IsNullOrEmpty(secretKey) && !string.Equals(secretKey, key))
|
||||
if (!string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
_logger.LogWarning("A request was made using an invalid security hey");
|
||||
ViewBag.ErrorMessage = "Invalid gallery key";
|
||||
|
||||
return View("~/Views/Home/Index.cshtml");
|
||||
galleryId = await _database.GetGalleryId(identifier);
|
||||
}
|
||||
else if (string.IsNullOrEmpty(id))
|
||||
else if (!string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
ViewBag.ErrorMessage = "Invalid gallery id";
|
||||
|
||||
return View("~/Views/Home/Index.cshtml");
|
||||
galleryId = await _database.GetGalleryIdByName(id);
|
||||
}
|
||||
|
||||
ViewBag.SecretKey = key;
|
||||
|
||||
var galleryPath = Path.Combine(UploadsDirectory, id);
|
||||
var allowedFileTypes = _config.GetOrDefault("Settings", "Allowed_File_Types", ".jpg,.jpeg,.png").Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
var files = Directory.Exists(galleryPath) ? Directory.GetFiles(galleryPath, "*.*", SearchOption.TopDirectoryOnly)?.Where(x => allowedFileTypes.Any(y => string.Equals(Path.GetExtension(x).Trim('.'), y.Trim('.'), StringComparison.OrdinalIgnoreCase))) : null;
|
||||
var images = new PhotoGallery(_config.GetOrDefault("Settings", "Gallery_Columns", 4))
|
||||
GalleryModel? gallery = galleryId != null ? await _database.GetGallery(galleryId.Value) : null;
|
||||
if (gallery == null)
|
||||
{
|
||||
GalleryId = id,
|
||||
GalleryPath = $"/{galleryPath.Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}",
|
||||
Images = files?.OrderByDescending(x => new FileInfo(x).CreationTimeUtc)?.Select(x => Path.GetFileName(x))?.ToList(),
|
||||
FileUploader = new FileUploader(id, "/Gallery/UploadImage")
|
||||
if (User?.Identity != null || await _settings.GetOrDefault(Settings.Basic.GuestGalleryCreation, false))
|
||||
{
|
||||
if (await _database.GetGalleryCount() < await _settings.GetOrDefault(Settings.Basic.MaxGalleryCount, 1000000))
|
||||
{
|
||||
gallery = await _database.AddGallery(new GalleryModel()
|
||||
{
|
||||
Name = id?.ToLower() ?? GalleryHelper.GenerateGalleryIdentifier(),
|
||||
SecretKey = key,
|
||||
Owner = User?.Identity?.GetUserId() ?? 0
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
return new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.GalleryLimitReached }, false);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.GalleryCreationNotAllowed }, false);
|
||||
}
|
||||
}
|
||||
|
||||
var append = new List<KeyValuePair<string, string>>()
|
||||
{
|
||||
new KeyValuePair<string, string>("identifier", gallery.Identifier)
|
||||
};
|
||||
|
||||
return View(images);
|
||||
if (!string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
var enc = _encryptionHelper.IsEncryptionEnabled();
|
||||
append.Add(new KeyValuePair<string, string>("key", enc ? _encryptionHelper.Encrypt(key) : key));
|
||||
append.Add(new KeyValuePair<string, string>("enc", enc.ToString().ToLower()));
|
||||
}
|
||||
|
||||
var redirectUrl = _urlHelper.GenerateFullUrl(HttpContext.Request, "/Gallery", append);
|
||||
|
||||
return new JsonResult(new { success = true, redirectUrl });
|
||||
}
|
||||
|
||||
public async Task<IActionResult> UploadImage()
|
||||
[HttpGet]
|
||||
[RequiresSecretKey]
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public async Task<IActionResult> Index(string? id, string? identifier, string? key = null, ViewMode? mode = null, GalleryGroup? group = null, GalleryOrder? order = null, GalleryFilter? filter = null, string? culture = null, bool partial = false)
|
||||
{
|
||||
try
|
||||
int? galleryId = null;
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(identifier))
|
||||
{
|
||||
var secretKey = _config.Get("Settings", "Secret_Key");
|
||||
var key = Request?.Form?.FirstOrDefault(x => string.Equals("SecretKey", x.Key, StringComparison.OrdinalIgnoreCase)).Value;
|
||||
if (!string.IsNullOrEmpty(secretKey) && !string.Equals(secretKey, key))
|
||||
galleryId = await _database.GetGalleryId(identifier);
|
||||
}
|
||||
else if (!string.IsNullOrWhiteSpace(id))
|
||||
{
|
||||
galleryId = await _database.GetGalleryIdByName(id);
|
||||
}
|
||||
|
||||
if (galleryId != null)
|
||||
{
|
||||
var userPermissions = User?.Identity?.GetUserPermissions() ?? new Permissions();
|
||||
|
||||
if (galleryId < 1 && !userPermissions.Gallery.HasFlag(GalleryPermissions.ViewAllGallery))
|
||||
{
|
||||
_logger.LogWarning("A request was made using an invalid security hey");
|
||||
throw new UnauthorizedAccessException("The provided access token was invalid");
|
||||
return new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.InvalidGalleryId }, false);
|
||||
}
|
||||
|
||||
string galleryId = Request?.Form?.FirstOrDefault(x => string.Equals("GalleryId", x.Key, StringComparison.OrdinalIgnoreCase)).Value ?? string.Empty;
|
||||
if (string.IsNullOrEmpty(galleryId))
|
||||
if (!string.IsNullOrWhiteSpace(culture))
|
||||
{
|
||||
return Json(new { success = true, uploaded = 0, errors = new List<string>() { "Invalid gallery Id detected" } });
|
||||
}
|
||||
|
||||
galleryId = galleryId.ToLower();
|
||||
|
||||
var galleryPath = Path.Combine(UploadsDirectory, galleryId);
|
||||
var files = Request?.Form?.Files;
|
||||
if (files != null && files.Count > 0)
|
||||
{
|
||||
if (!Directory.Exists(galleryPath))
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(galleryPath);
|
||||
HttpContext.Session.SetString(SessionKey.SelectedLanguage, culture);
|
||||
Response.Cookies.Append(
|
||||
CookieRequestCultureProvider.DefaultCookieName,
|
||||
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
|
||||
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
|
||||
);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
ViewBag.ViewMode = mode ?? (ViewMode)await _settings.GetOrDefault(Settings.Gallery.DefaultView, (int)ViewMode.Default, galleryId);
|
||||
}
|
||||
catch
|
||||
{
|
||||
ViewBag.ViewMode = ViewMode.Default;
|
||||
}
|
||||
|
||||
var deviceType = HttpContext.Session.GetString(SessionKey.DeviceType);
|
||||
if (string.IsNullOrWhiteSpace(deviceType))
|
||||
{
|
||||
deviceType = (await _deviceDetector.ParseDeviceType(Request.Headers["User-Agent"].ToString())).ToString();
|
||||
HttpContext.Session.SetString(SessionKey.DeviceType, deviceType ?? "Desktop");
|
||||
}
|
||||
|
||||
ViewBag.IsMobile = !string.Equals("Desktop", deviceType, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
GalleryModel? gallery = await _database.GetGallery(galleryId.Value);
|
||||
if (gallery != null)
|
||||
{
|
||||
var galleryPath = Path.Combine(UploadsDirectory, gallery.Identifier);
|
||||
_fileHelper.CreateDirectoryIfNotExists(galleryPath);
|
||||
_fileHelper.CreateDirectoryIfNotExists(Path.Combine(galleryPath, "Pending"));
|
||||
|
||||
ViewBag.GalleryIdentifier = gallery.Identifier;
|
||||
ViewBag.SecretKey = gallery.SecretKey;
|
||||
|
||||
var currentPage = 1;
|
||||
try
|
||||
{
|
||||
currentPage = int.Parse((Request.Query.ContainsKey("page") && !string.IsNullOrWhiteSpace(Request.Query["page"])) ? Request.Query["page"].ToString().ToLower() : "1");
|
||||
}
|
||||
catch { }
|
||||
|
||||
var galleryGroup = group ?? (GalleryGroup)(await _settings.GetOrDefault(Settings.Gallery.DefaultGroup, (int)GalleryGroup.None, gallery?.Id));
|
||||
var galleryOrder = order ?? (GalleryOrder)(await _settings.GetOrDefault(Settings.Gallery.DefaultOrder, (int)GalleryOrder.Descending, gallery?.Id));
|
||||
var galleryFilter = filter ?? (GalleryFilter)(await _settings.GetOrDefault(Settings.Gallery.DefaultFilter, (int)GalleryFilter.All, gallery?.Id));
|
||||
|
||||
var mediaType = MediaType.All;
|
||||
if (mode == ViewMode.Slideshow)
|
||||
{
|
||||
mediaType = MediaType.Image;
|
||||
}
|
||||
else
|
||||
{
|
||||
switch (galleryFilter)
|
||||
{
|
||||
case GalleryFilter.Images:
|
||||
mediaType = MediaType.Image;
|
||||
break;
|
||||
case GalleryFilter.Videos:
|
||||
mediaType = MediaType.Video;
|
||||
break;
|
||||
default:
|
||||
mediaType = MediaType.All;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
var uploaded = 0;
|
||||
var errors = new List<string>();
|
||||
foreach (IFormFile file in files)
|
||||
var orientation = ImageOrientation.None;
|
||||
switch (galleryFilter)
|
||||
{
|
||||
case GalleryFilter.Landscape:
|
||||
orientation = ImageOrientation.Landscape;
|
||||
break;
|
||||
case GalleryFilter.Portrait:
|
||||
orientation = ImageOrientation.Portrait;
|
||||
break;
|
||||
case GalleryFilter.Square:
|
||||
orientation = ImageOrientation.Square;
|
||||
break;
|
||||
default:
|
||||
orientation = ImageOrientation.None;
|
||||
break;
|
||||
}
|
||||
|
||||
var itemsPerPage = await _settings.GetOrDefault(Settings.Gallery.ItemsPerPage, 50, gallery?.Id);
|
||||
var allowedFileTypes = (await _settings.GetOrDefault(Settings.Gallery.AllowedFileTypes, ".jpg,.jpeg,.png,.mp4,.mov", gallery?.Id)).Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
var items = (await _database.GetAllGalleryItems(gallery?.Id, GalleryItemState.Approved, mediaType, orientation, galleryGroup, galleryOrder, itemsPerPage, currentPage))?.Where(x => allowedFileTypes.Any(y => string.Equals(Path.GetExtension(x.Title).Trim('.'), y.Trim('.'), StringComparison.OrdinalIgnoreCase)));
|
||||
|
||||
var isGalleryAdmin = User?.Identity != null && User.Identity.IsAuthenticated && userPermissions.Gallery.HasFlag(GalleryPermissions.Upload);
|
||||
|
||||
var uploadActvated = !gallery.Identifier.Equals("All", StringComparison.OrdinalIgnoreCase) && (isGalleryAdmin || await _settings.GetOrDefault(Settings.Gallery.Upload, true, gallery?.Id));
|
||||
if (uploadActvated)
|
||||
{
|
||||
try
|
||||
{
|
||||
var extension = Path.GetExtension(file.FileName);
|
||||
var maxFilesSize = _config.GetOrDefault("Settings", "Max_File_Size_Mb", 10) * 1000000;
|
||||
var periods = (await _settings.GetOrDefault(Settings.Gallery.UploadPeriod, "1970-01-01 00:00", gallery?.Id))?.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
if (periods != null)
|
||||
{
|
||||
uploadActvated = false;
|
||||
|
||||
var allowedFileTypes = _config.GetOrDefault("Settings", "Allowed_File_Types", ".jpg,.jpeg,.png").Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
if (!allowedFileTypes.Any(x => string.Equals(x.Trim('.'), extension.Trim('.'), StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
errors.Add($"Failed to upload file '{Path.GetFileName(file.FileName)}'. File type is invalid");
|
||||
}
|
||||
else if (file.Length > maxFilesSize)
|
||||
{
|
||||
errors.Add($"Failed to upload file '{Path.GetFileName(file.FileName)}'. Max file size is {maxFilesSize} bytes");
|
||||
}
|
||||
else
|
||||
{
|
||||
var filePath = Path.Combine(galleryPath, $"{Guid.NewGuid()}{Path.GetExtension(file.FileName)}");
|
||||
if (!string.IsNullOrEmpty(filePath))
|
||||
var now = DateTime.UtcNow;
|
||||
foreach (var period in periods)
|
||||
{
|
||||
using (var fs = new FileStream(filePath, FileMode.Create))
|
||||
var timeRanges = period?.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
if (timeRanges != null && timeRanges.Length > 0)
|
||||
{
|
||||
await file.CopyToAsync(fs);
|
||||
uploaded++;
|
||||
var startDate = DateTime.Parse(timeRanges[0]).ToUniversalTime();
|
||||
|
||||
if (timeRanges.Length == 2)
|
||||
{
|
||||
var endDate = DateTime.Parse(timeRanges[1]).ToUniversalTime();
|
||||
if (now >= startDate && now < endDate)
|
||||
{
|
||||
uploadActvated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (timeRanges.Length == 1 && now >= startDate)
|
||||
{
|
||||
uploadActvated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to save image to gallery - {ex?.Message}");
|
||||
uploadActvated = true;
|
||||
}
|
||||
}
|
||||
|
||||
return Json(new { success = true, uploaded, errors });
|
||||
var itemCounts = await _database.GetGalleryItemCount(gallery?.Id, GalleryItemState.All, mediaType, orientation);
|
||||
var galleryIdentifiers = !gallery.Identifier.Equals("All", StringComparison.OrdinalIgnoreCase) ? new Dictionary<int, string>() { { gallery.Id, gallery.Identifier } } : items?.GroupBy(x => x.GalleryId)?.Select(x => new KeyValuePair<int, string>(x.Key, _database.GetGalleryIdentifier(x.Key).Result))?.ToDictionary();
|
||||
var model = new PhotoGallery()
|
||||
{
|
||||
Gallery = gallery,
|
||||
SecretKey = gallery.SecretKey,
|
||||
Images = items?.Select(x => {
|
||||
var galleryIdentifier = galleryIdentifiers != null && galleryIdentifiers.ContainsKey(x.GalleryId) ? galleryIdentifiers[x.GalleryId] : gallery.Identifier;
|
||||
return new PhotoGalleryImage()
|
||||
{
|
||||
Id = x.Id,
|
||||
GalleryId = x.GalleryId,
|
||||
GalleryName = gallery.Name,
|
||||
Name = Path.GetFileName(x.Title),
|
||||
UploadedBy = x.UploadedBy,
|
||||
UploaderEmailAddress = x.UploaderEmailAddress,
|
||||
UploadDate = x.UploadedDate,
|
||||
ImagePath = $"/{Path.Combine(UploadsDirectory, galleryIdentifier).Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{x.Title}",
|
||||
ThumbnailPath = $"/{Path.Combine(ThumbnailsDirectory, galleryIdentifier).Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{Path.GetFileNameWithoutExtension(x.Title)}.webp",
|
||||
MediaType = x.MediaType
|
||||
};
|
||||
})?.ToList(),
|
||||
CurrentPage = currentPage,
|
||||
ApprovedCount = (int)itemCounts["Approved"],
|
||||
PendingCount = (int)itemCounts["Pending"],
|
||||
ItemsPerPage = itemsPerPage,
|
||||
UploadActivated = uploadActvated,
|
||||
ViewMode = (ViewMode)ViewBag.ViewMode,
|
||||
GroupBy = galleryGroup,
|
||||
OrderBy = galleryOrder,
|
||||
Pagination = galleryOrder != GalleryOrder.Random,
|
||||
LoadScripts = !partial
|
||||
};
|
||||
|
||||
return partial ? PartialView("~/Views/Gallery/GalleryWrapper.cshtml", model) : View(model);
|
||||
}
|
||||
}
|
||||
|
||||
return new RedirectToActionResult("Index", "Error", new { Reason = ErrorCode.InvalidGalleryId }, false);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> UploadImage()
|
||||
{
|
||||
Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
|
||||
try
|
||||
{
|
||||
if (!int.TryParse((Request?.Form?.FirstOrDefault(x => string.Equals("Id", x.Key, StringComparison.OrdinalIgnoreCase)).Value)?.ToString()?.ToLower() ?? string.Empty, out var galleryId))
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["Invalid_Gallery_Id"].Value } });
|
||||
}
|
||||
|
||||
var gallery = await _database.GetGallery(galleryId);
|
||||
if (gallery != null)
|
||||
{
|
||||
string key = (Request?.Form?.FirstOrDefault(x => string.Equals("SecretKey", x.Key, StringComparison.OrdinalIgnoreCase)).Value)?.ToString() ?? string.Empty;
|
||||
if (!string.IsNullOrWhiteSpace(gallery.SecretKey) && !string.Equals(gallery.SecretKey, key))
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["Invalid_Secret_Key_Warning"].Value } });
|
||||
}
|
||||
|
||||
string uploadedBy = HttpContext.Session.GetString(SessionKey.ViewerIdentity)?.Trim() ?? "Anonymous";
|
||||
string uploaderEmail = HttpContext.Session.GetString(SessionKey.ViewerEmailAddress)?.Trim() ?? "Anonymous";
|
||||
|
||||
var files = Request?.Form?.Files;
|
||||
if (files != null && files.Count > 0)
|
||||
{
|
||||
var galleryOwner = await _database.GetUser(gallery.Owner);
|
||||
var isFreeGallery = gallery.Owner > 0 && (galleryOwner?.Level ?? UserLevel.Free) == UserLevel.Free;
|
||||
var requiresReview = !isFreeGallery && await _settings.GetOrDefault(Settings.Gallery.RequireReview, true, gallery.Id);
|
||||
|
||||
var uploaded = 0;
|
||||
var errors = new List<string>();
|
||||
foreach (IFormFile file in files)
|
||||
{
|
||||
try
|
||||
{
|
||||
var extension = Path.GetExtension(file.FileName);
|
||||
var maxGallerySize = await _settings.GetOrDefault(Settings.Gallery.MaxSizeMB, 1024L, gallery.Id) * 1000000;
|
||||
var maxFilesSize = await _settings.GetOrDefault(Settings.Gallery.MaxFileSizeMB, 50L, gallery.Id) * 1000000;
|
||||
var galleryPath = Path.Combine(UploadsDirectory, gallery.Identifier);
|
||||
|
||||
var allowedFileTypes = (await _settings.GetOrDefault(Settings.Gallery.AllowedFileTypes, ".jpg,.jpeg,.png,.mp4,.mov", gallery.Id)).Split(',', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
if (!allowedFileTypes.Any(x => string.Equals(x.Trim('.'), extension.Trim('.'), StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
errors.Add($"{_localizer["File_Upload_Failed"].Value}. {_localizer["Invalid_File_Type"].Value}");
|
||||
}
|
||||
else if (file.Length > maxFilesSize)
|
||||
{
|
||||
errors.Add($"{_localizer["File_Upload_Failed"].Value}. {_localizer["Max_File_Size"].Value} {maxFilesSize} bytes");
|
||||
}
|
||||
else if ((_fileHelper.GetDirectorySize(galleryPath) + file.Length) > maxGallerySize)
|
||||
{
|
||||
errors.Add($"{_localizer["File_Upload_Failed"].Value}. {_localizer["Gallery_Full"].Value} {maxGallerySize} bytes");
|
||||
}
|
||||
else
|
||||
{
|
||||
var fileName = _fileHelper.SanitizeFilename($"{(!string.IsNullOrWhiteSpace(uploadedBy) ? $"{uploadedBy.Replace(" ", "_")}-" : string.Empty)}{Guid.NewGuid()}{Path.GetExtension(file.FileName)}");
|
||||
galleryPath = requiresReview ? Path.Combine(galleryPath, "Pending") : galleryPath;
|
||||
|
||||
_fileHelper.CreateDirectoryIfNotExists(galleryPath);
|
||||
|
||||
var filePath = Path.Combine(galleryPath, fileName);
|
||||
if (!string.IsNullOrWhiteSpace(filePath))
|
||||
{
|
||||
var isDemoMode = await _settings.GetOrDefault(Settings.IsDemoMode, false);
|
||||
if (!isDemoMode)
|
||||
{
|
||||
await _fileHelper.SaveFile(file, filePath, FileMode.Create);
|
||||
}
|
||||
else
|
||||
{
|
||||
System.IO.File.Copy(Path.Combine(ImagesDirectory, $"DemoImage.png"), filePath, true);
|
||||
}
|
||||
|
||||
var checksum = await _fileHelper.GetChecksum(filePath);
|
||||
if (await _settings.GetOrDefault(Settings.Gallery.PreventDuplicates, true, gallery.Id) && (string.IsNullOrWhiteSpace(checksum) || await _database.GetGalleryItemByChecksum(gallery.Id, checksum) != null))
|
||||
{
|
||||
errors.Add($"{_localizer["File_Upload_Failed"].Value}. {_localizer["Duplicate_Item_Detected"].Value}");
|
||||
_fileHelper.DeleteFileIfExists(filePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
var gallerySavePath = Path.Combine(ThumbnailsDirectory, gallery.Identifier);
|
||||
|
||||
_fileHelper.CreateDirectoryIfNotExists(ThumbnailsDirectory);
|
||||
_fileHelper.CreateDirectoryIfNotExists(gallerySavePath);
|
||||
|
||||
var savePath = Path.Combine(gallerySavePath, $"{Path.GetFileNameWithoutExtension(filePath)}.webp");
|
||||
await _imageHelper.GenerateThumbnail(filePath, savePath, await _settings.GetOrDefault(Settings.Basic.ThumbnailSize, 720));
|
||||
|
||||
var item = await _database.AddGalleryItem(new GalleryItemModel()
|
||||
{
|
||||
GalleryId = gallery.Id,
|
||||
Title = fileName,
|
||||
UploadedBy = uploadedBy,
|
||||
UploaderEmailAddress = uploaderEmail,
|
||||
UploadedDate = await _fileHelper.GetCreationDatetime(filePath),
|
||||
Checksum = checksum,
|
||||
MediaType = _imageHelper.GetMediaType(filePath),
|
||||
Orientation = await _imageHelper.GetOrientation(savePath),
|
||||
State = requiresReview ? GalleryItemState.Pending : GalleryItemState.Approved,
|
||||
FileSize = file.Length,
|
||||
});
|
||||
|
||||
if (item?.Id > 0)
|
||||
{
|
||||
uploaded++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"{_localizer["Save_To_Gallery_Failed"].Value} - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
Response.StatusCode = (int)HttpStatusCode.OK;
|
||||
|
||||
return Json(new { success = uploaded > 0, uploaded, uploadedBy, requiresReview, errors });
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["No_Files_For_Upload"].Value } });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["Gallery_Does_Not_Exist"].Value } });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Failed to upload images - {ex?.Message}");
|
||||
_logger.LogError(ex, $"{_localizer["Image_Upload_Failed"].Value} - {ex?.Message}");
|
||||
}
|
||||
|
||||
return Json(new { success = false, uploaded = 0 });
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> UploadCompleted()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
|
||||
try
|
||||
{
|
||||
if (!int.TryParse((Request?.Form?.FirstOrDefault(x => string.Equals("Id", x.Key, StringComparison.OrdinalIgnoreCase)).Value)?.ToString()?.ToLower() ?? string.Empty, out var galleryId))
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["Invalid_Gallery_Id"].Value } });
|
||||
}
|
||||
|
||||
var gallery = await _database.GetGallery(galleryId);
|
||||
if (gallery != null)
|
||||
{
|
||||
string key = (Request?.Form?.FirstOrDefault(x => string.Equals("SecretKey", x.Key, StringComparison.OrdinalIgnoreCase)).Value)?.ToString() ?? string.Empty;
|
||||
if (!string.IsNullOrWhiteSpace(gallery.SecretKey) && !string.Equals(gallery.SecretKey, key))
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["Invalid_Secret_Key_Warning"].Value } });
|
||||
}
|
||||
|
||||
var uploadedBy = HttpContext.Session.GetString(SessionKey.ViewerIdentity) ?? "Anonymous";
|
||||
|
||||
var galleryOwner = await _database.GetUser(gallery.Owner);
|
||||
var isFreeGallery = gallery.Owner > 0 && (galleryOwner?.Level ?? UserLevel.Free) == UserLevel.Free;
|
||||
var requiresReview = !isFreeGallery && await _settings.GetOrDefault(Settings.Gallery.RequireReview, true, gallery.Id);
|
||||
|
||||
int uploaded = int.Parse((Request?.Form?.FirstOrDefault(x => string.Equals("Count", x.Key, StringComparison.OrdinalIgnoreCase)).Value)?.ToString() ?? "0");
|
||||
if (uploaded > 0 && requiresReview && await _settings.GetOrDefault(Notifications.Alerts.PendingReview, true))
|
||||
{
|
||||
await _notificationHelper.Send(_localizer["New_Items_Pending_Review"].Value, $"{uploaded} new item(s) have been uploaded to gallery '{gallery.Name}' by '{(!string.IsNullOrWhiteSpace(uploadedBy) ? uploadedBy : "Anonymous")}' and are awaiting your review.", _urlHelper.GenerateBaseUrl(HttpContext?.Request, "/Account"));
|
||||
}
|
||||
|
||||
Response.StatusCode = (int)HttpStatusCode.OK;
|
||||
|
||||
return Json(new { success = true, counters = new { total = gallery?.TotalItems ?? 0, approved = gallery?.ApprovedItems ?? 0, pending = gallery?.PendingItems ?? 0 }, uploaded, uploadedBy, requiresReview });
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json(new { success = false, uploaded = 0, errors = new List<string>() { _localizer["Gallery_Does_Not_Exist"].Value } });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{_localizer["Image_Upload_Failed"].Value} - {ex?.Message}");
|
||||
}
|
||||
|
||||
return Json(new { success = false });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[RequestTimeout("timeout_1h")]
|
||||
public async Task<IActionResult> DownloadGallery(int id, string? secretKey, string? group)
|
||||
{
|
||||
try
|
||||
{
|
||||
var gallery = await _database.GetGallery(id);
|
||||
if (gallery != null)
|
||||
{
|
||||
secretKey = secretKey ?? string.Empty;
|
||||
|
||||
if (!secretKey.Equals(gallery.SecretKey))
|
||||
{
|
||||
return Json(new { success = false, message = _localizer["Failed_Download_Gallery_Invalid_Key"].Value });
|
||||
}
|
||||
|
||||
if (await _settings.GetOrDefault(Settings.Gallery.Download, true, gallery?.Id) || (User?.Identity != null && User.Identity.IsAuthenticated))
|
||||
{
|
||||
var galleryDir = id > 0 ? Path.Combine(UploadsDirectory, gallery.Identifier) : UploadsDirectory;
|
||||
if (_fileHelper.DirectoryExists(galleryDir))
|
||||
{
|
||||
var fileFilter = new List<string>();
|
||||
if (!string.IsNullOrWhiteSpace(group))
|
||||
{
|
||||
var groupParts = group.Split('|', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
|
||||
if (groupParts != null && groupParts.Length == 2)
|
||||
{
|
||||
var items = await _database.GetAllGalleryItems(id, GalleryItemState.Approved);
|
||||
foreach (GalleryGroup type in Enum.GetValues(typeof(GalleryGroup)))
|
||||
{
|
||||
if (((int)type).ToString().Equals(groupParts[0]))
|
||||
{
|
||||
try
|
||||
{
|
||||
IEnumerable<IGrouping<string, GalleryItemModel>>? filtered = null;
|
||||
switch (type)
|
||||
{
|
||||
case GalleryGroup.Date:
|
||||
filtered = items?.GroupBy(x => x.UploadedDate != null ? x.UploadedDate.Value.ToString("dddd, d MMMM yyyy") : "Unknown");
|
||||
break;
|
||||
case GalleryGroup.MediaType:
|
||||
filtered = items?.GroupBy(x => x.MediaType.ToString());
|
||||
break;
|
||||
case GalleryGroup.Uploader:
|
||||
filtered = items?.GroupBy(x => x.UploadedBy ?? "Anonymous");
|
||||
break;
|
||||
}
|
||||
|
||||
if (filtered != null)
|
||||
{
|
||||
foreach (var f in filtered)
|
||||
{
|
||||
if (f.Key.Equals(groupParts[1]))
|
||||
{
|
||||
if (f.Any())
|
||||
{
|
||||
fileFilter.AddRange(f.Select(x => x.Title));
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Json(new { success = false, message = _localizer["Failed_Download_Gallery"].Value });
|
||||
}
|
||||
}
|
||||
|
||||
var archieveName = $"{gallery?.Identifier ?? "WeddingShare"}_{DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss")}.zip";
|
||||
|
||||
var listing = new List<ZipListing>();
|
||||
|
||||
if (User?.Identity == null || !User.Identity.IsAuthenticated)
|
||||
{
|
||||
var files = Directory.GetFiles(galleryDir, "*", SearchOption.TopDirectoryOnly);
|
||||
if (fileFilter != null && fileFilter.Any())
|
||||
{
|
||||
files = files.Where(x => fileFilter.Exists(y => Path.GetFileName(y).Equals(Path.GetFileName(x), StringComparison.OrdinalIgnoreCase))).ToArray();
|
||||
}
|
||||
|
||||
if (files != null && files.Any())
|
||||
{
|
||||
listing.Add(new ZipListing(galleryDir, files));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var scanners = new List<ZipListingScanner>()
|
||||
{
|
||||
new ZipListingScanner("Approved", galleryDir, SearchOption.TopDirectoryOnly),
|
||||
new ZipListingScanner("Pending", Path.Combine(galleryDir, "Pending"), SearchOption.AllDirectories),
|
||||
new ZipListingScanner("Rejected", Path.Combine(galleryDir, "Rejected"), SearchOption.AllDirectories),
|
||||
};
|
||||
|
||||
foreach (var scanner in scanners)
|
||||
{
|
||||
try
|
||||
{
|
||||
var files = Directory.GetFiles(scanner.Path, "*", scanner.SearchOption);
|
||||
if (fileFilter != null && fileFilter.Any())
|
||||
{
|
||||
files = files.Where(x => fileFilter.Exists(y => Path.GetFileName(y).Equals(Path.GetFileName(x), StringComparison.OrdinalIgnoreCase))).ToArray();
|
||||
}
|
||||
|
||||
if (files != null && files.Any())
|
||||
{
|
||||
listing.Add(new ZipListing(scanner.Path, files, scanner.Name));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
|
||||
return await ZipFileResponse(archieveName, listing);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
return Json(new { success = false, message = _localizer["Download_Gallery_Not_Allowed"].Value });
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
return Json(new { success = false, message = _localizer["Failed_Download_Gallery"].Value });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Response.StatusCode = (int)HttpStatusCode.BadRequest;
|
||||
_logger.LogError(ex, $"{_localizer["Failed_Download_Gallery"].Value} - {ex?.Message}");
|
||||
}
|
||||
|
||||
return Json(new { success = false });
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public string GenerateSecretKey()
|
||||
{
|
||||
return PasswordHelper.GenerateGallerySecretKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,134 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Extensions;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
[AllowAnonymous]
|
||||
public class HomeController : BaseController
|
||||
{
|
||||
private readonly ISettingsHelper _settings;
|
||||
private readonly IDatabaseHelper _database;
|
||||
private readonly IDeviceDetector _deviceDetector;
|
||||
private readonly IAuditHelper _audit;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public HomeController(ILogger<HomeController> logger)
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer;
|
||||
|
||||
public HomeController(ISettingsHelper settings, IDatabaseHelper database, IDeviceDetector deviceDetector, IAuditHelper audit, ILogger<HomeController> logger, IStringLocalizer<Lang.Translations> localizer)
|
||||
: base()
|
||||
{
|
||||
_settings = settings;
|
||||
_database = database;
|
||||
_deviceDetector = deviceDetector;
|
||||
_audit = audit;
|
||||
_logger = logger;
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
var model = new Views.Home.IndexModel();
|
||||
|
||||
try
|
||||
{
|
||||
var deviceType = HttpContext.Session.GetString(SessionKey.DeviceType);
|
||||
if (string.IsNullOrWhiteSpace(deviceType))
|
||||
{
|
||||
deviceType = (await _deviceDetector.ParseDeviceType(Request.Headers["User-Agent"].ToString())).ToString();
|
||||
HttpContext.Session.SetString(SessionKey.DeviceType, deviceType ?? "Desktop");
|
||||
}
|
||||
|
||||
if (await _settings.GetOrDefault(Settings.Basic.SingleGalleryMode, false))
|
||||
{
|
||||
var gallery = await _database.GetGallery(1);
|
||||
if (string.IsNullOrWhiteSpace(gallery?.SecretKey))
|
||||
{
|
||||
return RedirectToAction("Index", "Gallery", new { identifier = "default" });
|
||||
}
|
||||
}
|
||||
|
||||
var isDropdownMode = await _settings.GetOrDefault(Settings.GallerySelector.Dropdown, false);
|
||||
var galleryNames = isDropdownMode ? (await _database.GetGalleryNames()).Where(x => !x.Value.Equals("all", StringComparison.OrdinalIgnoreCase)) : new Dictionary<string, string>();
|
||||
if (await _settings.GetOrDefault(Settings.GallerySelector.HideDefaultOption, false))
|
||||
{
|
||||
galleryNames = galleryNames.Where(x => !x.Key.Equals("default", StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
|
||||
model.GalleryNames = galleryNames.OrderBy(gallery => gallery.Key.Equals("default", StringComparison.OrdinalIgnoreCase) ? 0 : 1).ThenBy(gallery => gallery.Value.ToLower()).ToDictionary();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{_localizer["Homepage_Load_Error"].Value} - {ex?.Message}");
|
||||
}
|
||||
|
||||
return View(model);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[Route("CookiePolicy")]
|
||||
[Route("Home/CookiePolicy")]
|
||||
public async Task<IActionResult> CookiePolicy()
|
||||
{
|
||||
ViewBag.CompanyName = await _settings.GetOrDefault(Settings.Basic.Title, "WeddingShare");
|
||||
ViewBag.SiteHostname = await _settings.GetOrDefault(Settings.Basic.BaseUrl, "www.wedding-share.org");
|
||||
ViewBag.CustomPolicy = await _settings.GetOrDefault(Settings.Policies.CookiePolicy, string.Empty);
|
||||
|
||||
return View("~/Views/Home/CookiePolicy.cshtml");
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> SetIdentity(string name, string? emailAddress)
|
||||
{
|
||||
try
|
||||
{
|
||||
var emailRequired = await _settings.GetOrDefault(Settings.IdentityCheck.RequireEmail, false);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(name) || HtmlSanitizer.MayContainXss(name))
|
||||
{
|
||||
return Json(new { success = false, reason = 1 });
|
||||
}
|
||||
else if (emailRequired && (string.IsNullOrWhiteSpace(emailAddress) || EmailValidationHelper.IsValid(emailAddress) == false || HtmlSanitizer.MayContainXss(emailAddress)))
|
||||
{
|
||||
return Json(new { success = false, reason = 2 });
|
||||
}
|
||||
else
|
||||
{
|
||||
HttpContext.Session.SetString(SessionKey.ViewerIdentity, name);
|
||||
HttpContext.Session.SetString(SessionKey.ViewerEmailAddress, emailAddress ?? string.Empty);
|
||||
|
||||
return Json(new { success = true });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{_localizer["Identity_Session_Error"].Value}: '{name}'");
|
||||
}
|
||||
|
||||
return Json(new { success = false });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> LogCookieApproval()
|
||||
{
|
||||
try
|
||||
{
|
||||
var ipAddress = Request.HttpContext.TryGetIpAddress();
|
||||
|
||||
return Json(new { success = await _audit.LogAction("Visitor", $"{_localizer["Audit_CookieConsentApproved"].Value}: {ipAddress}") });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{_localizer["Cookie_Audit_Error"].Value}");
|
||||
}
|
||||
|
||||
return Json(new { success = false });
|
||||
}
|
||||
}
|
||||
}
|
||||
81
WeddingShare/Controllers/LanguageController.cs
Normal file
81
WeddingShare/Controllers/LanguageController.cs
Normal file
@@ -0,0 +1,81 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Localization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Models;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
[AllowAnonymous]
|
||||
public class LanguageController : BaseController
|
||||
{
|
||||
private readonly ISettingsHelper _settings;
|
||||
private readonly ILanguageHelper _languageHelper;
|
||||
private readonly ILogger<LanguageController> _logger;
|
||||
|
||||
public LanguageController(ISettingsHelper settings, ILanguageHelper languageHelper, ILogger<LanguageController> logger)
|
||||
: base()
|
||||
{
|
||||
_settings = settings;
|
||||
_languageHelper = languageHelper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var options = new List<SupportedLanguage>();
|
||||
|
||||
try
|
||||
{
|
||||
var defaultLang = HttpContext.Session.GetString(SessionKey.SelectedLanguage);
|
||||
if (string.IsNullOrWhiteSpace(defaultLang))
|
||||
{
|
||||
defaultLang = await _languageHelper.GetOrFallbackCulture(string.Empty, await _settings.GetOrDefault(Settings.Languages.Default, "en-GB"));
|
||||
}
|
||||
|
||||
options = (await _languageHelper.DetectSupportedCulturesAsync())
|
||||
.Select(x => new SupportedLanguage() { Key = x.Name, Value = $"{(x.EnglishName.Contains("(") ? x.EnglishName.Substring(0, x.EnglishName.IndexOf("(")) : x.EnglishName).Trim()} ({x.Name})", Selected = string.Equals(defaultLang, x.Name, StringComparison.OrdinalIgnoreCase) })
|
||||
.OrderBy(x => x.Value.ToLower())
|
||||
.ToList();
|
||||
}
|
||||
catch { }
|
||||
|
||||
return Json(new { supported = options });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> ChangeDisplayLanguage(string culture)
|
||||
{
|
||||
try
|
||||
{
|
||||
culture = await _languageHelper.GetOrFallbackCulture(culture, await _settings.GetOrDefault(Settings.Languages.Default, "en-GB"));
|
||||
|
||||
HttpContext.Session.SetString(SessionKey.SelectedLanguage, culture);
|
||||
Response.Cookies.Append(
|
||||
CookieRequestCultureProvider.DefaultCookieName,
|
||||
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
|
||||
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
|
||||
);
|
||||
|
||||
return Json(new { success = true });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to set display language to '{culture}' - {ex?.Message}");
|
||||
|
||||
culture = "en-GB";
|
||||
|
||||
HttpContext.Session.SetString(SessionKey.SelectedLanguage, culture);
|
||||
Response.Cookies.Append(
|
||||
CookieRequestCultureProvider.DefaultCookieName,
|
||||
CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
|
||||
new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1) }
|
||||
);
|
||||
}
|
||||
|
||||
return Json(new { success = false });
|
||||
}
|
||||
}
|
||||
}
|
||||
256
WeddingShare/Controllers/MediaViewerController.cs
Normal file
256
WeddingShare/Controllers/MediaViewerController.cs
Normal file
@@ -0,0 +1,256 @@
|
||||
using System.Text;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using WeddingShare.Attributes;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Extensions;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Helpers.Notifications;
|
||||
using WeddingShare.Models;
|
||||
using WeddingShare.Models.Database;
|
||||
using WeddingShare.Views.MediaViewer;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
[AllowAnonymous]
|
||||
public class MediaViewerController : BaseController
|
||||
{
|
||||
private readonly IWebHostEnvironment _hostingEnvironment;
|
||||
private readonly ISettingsHelper _settings;
|
||||
private readonly IDatabaseHelper _database;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer;
|
||||
|
||||
private readonly string UploadsDirectory;
|
||||
private readonly string ThumbnailsDirectory;
|
||||
private readonly string CustomResourcesDirectory;
|
||||
|
||||
public MediaViewerController(IWebHostEnvironment hostingEnvironment, ISettingsHelper settings, IDatabaseHelper database, ILogger<MediaViewerController> logger, IStringLocalizer<Lang.Translations> localizer)
|
||||
: base()
|
||||
{
|
||||
_hostingEnvironment = hostingEnvironment;
|
||||
_settings = settings;
|
||||
_database = database;
|
||||
_logger = logger;
|
||||
_localizer = localizer;
|
||||
|
||||
UploadsDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.Uploads);
|
||||
ThumbnailsDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.Thumbnails);
|
||||
CustomResourcesDirectory = Path.Combine(_hostingEnvironment.WebRootPath, Directories.CustomResources);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GalleryItem(int id)
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var galleryItem = await _database.GetGalleryItem(id);
|
||||
if (galleryItem != null)
|
||||
{
|
||||
var gallery = await _database.GetGallery(galleryItem.GalleryId);
|
||||
if (gallery != null)
|
||||
{
|
||||
var user = User?.Identity != null && User.Identity.IsAuthenticated ? User.Identity : null;
|
||||
var identityEnabled = await _settings.GetOrDefault(Settings.IdentityCheck.Enabled, true);
|
||||
var likesEnabled = await _settings.GetOrDefault(Settings.Gallery.Likes, true, galleryItem.GalleryId);
|
||||
|
||||
var author = string.Empty;
|
||||
if (identityEnabled)
|
||||
{
|
||||
var builder = new StringBuilder($"{_localizer["Uploaded_By"].Value}: ");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(galleryItem?.UploadedBy))
|
||||
{
|
||||
builder.Append(galleryItem.UploadedBy);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(galleryItem?.UploaderEmailAddress) && (user?.IsPrivilegedUser() ?? false))
|
||||
{
|
||||
builder.Append($" - {galleryItem?.UploaderEmailAddress?.ToLower()}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("Anonymous");
|
||||
}
|
||||
|
||||
author = builder.ToString();
|
||||
}
|
||||
|
||||
return PartialView("~/Views/MediaViewer/Popup.cshtml", new Popup()
|
||||
{
|
||||
Id = id,
|
||||
Collection = gallery.Name,
|
||||
Source = $"/{Path.Combine(UploadsDirectory, gallery.Identifier).Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{galleryItem.Title}",
|
||||
Thumbnail = $"/{Path.Combine(ThumbnailsDirectory, gallery.Identifier).Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{Path.GetFileNameWithoutExtension(galleryItem.Title)}.webp",
|
||||
Author = author,
|
||||
Type = galleryItem.MediaType.ToString().ToLower(),
|
||||
Likes = new PhotoGalleryImageLikes()
|
||||
{
|
||||
Enabled = likesEnabled,
|
||||
CanUserLike = likesEnabled && user != null,
|
||||
HasUserLiked = user != null ? await _database.CheckUserHasLikedGalleryItem(galleryItem.Id, user.GetUserId()) : false,
|
||||
Count = await _database.GetGalleryItemLikesCount(id)
|
||||
},
|
||||
DownloadEnabled = await _settings.GetOrDefault(Settings.Gallery.Download, true, gallery.Id) || (user?.IsPrivilegedUser() ?? false)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"An unexpected error occurred while getting the details for item '{id}' - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return PartialView("~/Views/MediaViewer/Popup.cshtml", new Popup() { Id = id });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
[RequiresRole(CustomResourcePermission = CustomResourcePermissions.View)]
|
||||
public async Task<IActionResult> CustomResource(int id)
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var resource = await _database.GetCustomResource(id);
|
||||
if (resource != null)
|
||||
{
|
||||
var user = User?.Identity != null && User.Identity.IsAuthenticated ? User.Identity : null;
|
||||
|
||||
return PartialView("~/Views/MediaViewer/Popup.cshtml", new Popup()
|
||||
{
|
||||
Id = id,
|
||||
Collection = "custom_resources",
|
||||
Source = $"/{CustomResourcesDirectory.Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{resource.FileName}",
|
||||
Title = Path.GetFileName(resource.FileName),
|
||||
Author = $"{_localizer["Uploaded_By"].Value}: {(!string.IsNullOrWhiteSpace(resource?.UploadedBy) ? resource.UploadedBy : "Anonymous")}",
|
||||
Type = MediaType.Image.ToString().ToLower(),
|
||||
DownloadEnabled = true
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"An unexpected error occurred while getting the details for item '{id}' - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return PartialView("~/Views/MediaViewer/Popup.cshtml", new Popup() { Id = id });
|
||||
}
|
||||
|
||||
[Authorize]
|
||||
[HttpGet]
|
||||
[RequiresRole(ReviewPermission = ReviewPermissions.View)]
|
||||
public async Task<IActionResult> ReviewItem(int id)
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var galleryItem = await _database.GetGalleryItem(id);
|
||||
if (galleryItem != null)
|
||||
{
|
||||
var gallery = await _database.GetGallery(galleryItem.GalleryId);
|
||||
if (gallery != null)
|
||||
{
|
||||
var user = User?.Identity != null && User.Identity.IsAuthenticated ? User.Identity : null;
|
||||
var identityEnabled = await _settings.GetOrDefault(Settings.IdentityCheck.Enabled, true);
|
||||
var likesEnabled = await _settings.GetOrDefault(Settings.Gallery.Likes, true, galleryItem.GalleryId);
|
||||
|
||||
var author = string.Empty;
|
||||
if (identityEnabled)
|
||||
{
|
||||
var builder = new StringBuilder($"{_localizer["Uploaded_By"].Value}: ");
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(galleryItem?.UploadedBy))
|
||||
{
|
||||
builder.Append(galleryItem.UploadedBy);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(galleryItem?.UploaderEmailAddress) && (user?.IsPrivilegedUser() ?? false))
|
||||
{
|
||||
builder.Append($" - {galleryItem?.UploaderEmailAddress?.ToLower()}");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("Anonymous");
|
||||
}
|
||||
|
||||
author = builder.ToString();
|
||||
}
|
||||
|
||||
return PartialView("~/Views/MediaViewer/Popup.cshtml", new Popup()
|
||||
{
|
||||
Id = id,
|
||||
Collection = gallery.Name,
|
||||
Source = $"/{Path.Combine(UploadsDirectory, gallery.Identifier, "Pending").Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{galleryItem.Title}",
|
||||
Thumbnail = $"/{Path.Combine(ThumbnailsDirectory, gallery.Identifier).Remove(_hostingEnvironment.WebRootPath).Replace('\\', '/').TrimStart('/')}/{Path.GetFileNameWithoutExtension(galleryItem.Title)}.webp",
|
||||
Title = null,
|
||||
Description = null,
|
||||
Author = author,
|
||||
Type = galleryItem.MediaType.ToString().ToLower(),
|
||||
DownloadEnabled = false
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"An unexpected error occurred while getting the details for item '{id}' - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return PartialView("~/Views/MediaViewer/Popup.cshtml", new Popup() { Id = id });
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> Like(int id, string action)
|
||||
{
|
||||
if (id > 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var galleryItem = await _database.GetGalleryItem(id);
|
||||
if (galleryItem != null)
|
||||
{
|
||||
var userId = User?.Identity != null && User.Identity.IsAuthenticated ? User.Identity.GetUserId() : 0;
|
||||
|
||||
long likes = 0;
|
||||
switch (action.ToLower())
|
||||
{
|
||||
case "like":
|
||||
likes = await _database.LikeGalleryItem(new GalleryItemLikeModel()
|
||||
{
|
||||
GalleryItemId = galleryItem.Id,
|
||||
UserId = userId
|
||||
});
|
||||
break;
|
||||
case "unlike":
|
||||
likes = await _database.UnLikeGalleryItem(new GalleryItemLikeModel()
|
||||
{
|
||||
GalleryItemId = galleryItem.Id,
|
||||
UserId = userId
|
||||
});
|
||||
break;
|
||||
}
|
||||
|
||||
return Json(new { success = true, value = likes });
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"An unexpected error occurred while performing action '{action}' on item '{id}' - {ex?.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
return Json(new { success = false });
|
||||
}
|
||||
}
|
||||
}
|
||||
46
WeddingShare/Controllers/SponsorsController.cs
Normal file
46
WeddingShare/Controllers/SponsorsController.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Localization;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers;
|
||||
using WeddingShare.Models;
|
||||
|
||||
namespace WeddingShare.Controllers
|
||||
{
|
||||
[AllowAnonymous]
|
||||
public class SponsorsController : BaseController
|
||||
{
|
||||
private readonly ISettingsHelper _settings;
|
||||
private readonly IHttpClientFactory _clientFactory;
|
||||
private readonly ILogger _logger;
|
||||
private readonly IStringLocalizer<Lang.Translations> _localizer;
|
||||
|
||||
public SponsorsController(ISettingsHelper settings, IHttpClientFactory clientFactory, ILogger<HomeController> logger, IStringLocalizer<Lang.Translations> localizer)
|
||||
: base()
|
||||
{
|
||||
_settings = settings;
|
||||
_clientFactory = clientFactory;
|
||||
_logger = logger;
|
||||
_localizer = localizer;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> Index()
|
||||
{
|
||||
var model = new Views.Sponsors.IndexModel();
|
||||
|
||||
try
|
||||
{
|
||||
var client = _clientFactory.CreateClient("SponsorsClient");
|
||||
var endpoint = await _settings.GetOrDefault(Sponsors.Endpoint, "/sponsors.json");
|
||||
model.SponsorsList = await client.GetFromJsonAsync<SponsorsList>(endpoint);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"{_localizer["Sponsors_Load_Error"].Value} - {ex?.Message}");
|
||||
}
|
||||
|
||||
return PartialView(model);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,22 @@
|
||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
|
||||
WORKDIR /app
|
||||
EXPOSE 5000
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:9.0 AS build
|
||||
ARG TARGETARCH
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["WeddingShare/WeddingShare.csproj", "WeddingShare/"]
|
||||
RUN dotnet restore "./WeddingShare/./WeddingShare.csproj"
|
||||
RUN dotnet restore -a $TARGETARCH "./WeddingShare/./WeddingShare.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/WeddingShare"
|
||||
RUN dotnet build "./WeddingShare.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
RUN dotnet build "./WeddingShare.csproj" -a $TARGETARCH -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./WeddingShare.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
RUN dotnet publish "./WeddingShare.csproj" -a $TARGETARCH -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
|
||||
9
WeddingShare/Enums/AccountState.cs
Normal file
9
WeddingShare/Enums/AccountState.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum AccountState
|
||||
{
|
||||
Active = 0,
|
||||
Frozen = 1,
|
||||
PendingActivation = 3
|
||||
}
|
||||
}
|
||||
13
WeddingShare/Enums/AccountTabs.cs
Normal file
13
WeddingShare/Enums/AccountTabs.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum AccountTabs
|
||||
{
|
||||
Reviews,
|
||||
Galleries,
|
||||
Users,
|
||||
Resources,
|
||||
Settings,
|
||||
Audit,
|
||||
Data
|
||||
}
|
||||
}
|
||||
9
WeddingShare/Enums/DatabaseType.cs
Normal file
9
WeddingShare/Enums/DatabaseType.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum DatabaseType
|
||||
{
|
||||
Unknown,
|
||||
SQLite,
|
||||
MySQL
|
||||
}
|
||||
}
|
||||
10
WeddingShare/Enums/DeviceType.cs
Normal file
10
WeddingShare/Enums/DeviceType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum DeviceType
|
||||
{
|
||||
Unknown,
|
||||
Desktop,
|
||||
Tablet,
|
||||
Mobile
|
||||
}
|
||||
}
|
||||
12
WeddingShare/Enums/GalleryFilter.cs
Normal file
12
WeddingShare/Enums/GalleryFilter.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum GalleryFilter
|
||||
{
|
||||
All,
|
||||
Images,
|
||||
Videos,
|
||||
Landscape,
|
||||
Portrait,
|
||||
Square
|
||||
}
|
||||
}
|
||||
10
WeddingShare/Enums/GalleryGroup.cs
Normal file
10
WeddingShare/Enums/GalleryGroup.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum GalleryGroup
|
||||
{
|
||||
None,
|
||||
Date,
|
||||
MediaType,
|
||||
Uploader
|
||||
}
|
||||
}
|
||||
9
WeddingShare/Enums/GalleryItemState.cs
Normal file
9
WeddingShare/Enums/GalleryItemState.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum GalleryItemState
|
||||
{
|
||||
Pending = 0,
|
||||
Approved = 1,
|
||||
All = 99
|
||||
}
|
||||
}
|
||||
9
WeddingShare/Enums/GalleryOrder.cs
Normal file
9
WeddingShare/Enums/GalleryOrder.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum GalleryOrder
|
||||
{
|
||||
Ascending,
|
||||
Descending,
|
||||
Random
|
||||
}
|
||||
}
|
||||
10
WeddingShare/Enums/ImageOrientation.cs
Normal file
10
WeddingShare/Enums/ImageOrientation.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum ImageOrientation
|
||||
{
|
||||
None,
|
||||
Square,
|
||||
Portrait,
|
||||
Landscape
|
||||
}
|
||||
}
|
||||
10
WeddingShare/Enums/MediaType.cs
Normal file
10
WeddingShare/Enums/MediaType.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum MediaType
|
||||
{
|
||||
Unknown,
|
||||
Image,
|
||||
Video,
|
||||
All
|
||||
}
|
||||
}
|
||||
84
WeddingShare/Enums/PermissionTypes.cs
Normal file
84
WeddingShare/Enums/PermissionTypes.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
[Flags]
|
||||
public enum ReviewPermissions : long
|
||||
{
|
||||
None = 0,
|
||||
View = 1,
|
||||
Approve = 2,
|
||||
Reject = 4,
|
||||
Delete = 8,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum GalleryPermissions : long
|
||||
{
|
||||
None = 0,
|
||||
View = 1,
|
||||
Create = 2,
|
||||
Update = 4,
|
||||
Delete = 8,
|
||||
Upload = 16,
|
||||
Download = 32,
|
||||
Wipe = 64,
|
||||
ViewAllGallery = 128
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum UserPermissions : long
|
||||
{
|
||||
None = 0,
|
||||
Login = 1,
|
||||
View = 2,
|
||||
Create = 4,
|
||||
Update = 8,
|
||||
Delete = 16,
|
||||
Change_Password = 32,
|
||||
Change_Permissions_Level = 64,
|
||||
Reset_MFA = 128,
|
||||
Freeze = 256,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum CustomResourcePermissions : long
|
||||
{
|
||||
None = 0,
|
||||
View = 1,
|
||||
Create = 2,
|
||||
Update = 4,
|
||||
Delete = 8,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum SettingsPermissions : long
|
||||
{
|
||||
None = 0,
|
||||
View = 1,
|
||||
Update = 2,
|
||||
Gallery_Update = 4,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum AuditPermissions : long
|
||||
{
|
||||
None = 0,
|
||||
View = 1,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum DataPermissions : long
|
||||
{
|
||||
None = 0,
|
||||
View = 1,
|
||||
Import = 2,
|
||||
Export = 4,
|
||||
Wipe = 8,
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum FeaturePermissions : long
|
||||
{
|
||||
None = 0,
|
||||
UpgradeToUnlock = 1,
|
||||
}
|
||||
}
|
||||
9
WeddingShare/Enums/ReviewAction.cs
Normal file
9
WeddingShare/Enums/ReviewAction.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum ReviewAction
|
||||
{
|
||||
UNKNOWN = 0,
|
||||
APPROVED = 1,
|
||||
REJECTED = 2
|
||||
}
|
||||
}
|
||||
8
WeddingShare/Enums/Themes.cs
Normal file
8
WeddingShare/Enums/Themes.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum Themes
|
||||
{
|
||||
Default = 1,
|
||||
Dark = 2
|
||||
}
|
||||
}
|
||||
12
WeddingShare/Enums/UserLevel.cs
Normal file
12
WeddingShare/Enums/UserLevel.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum UserLevel
|
||||
{
|
||||
Free = 0,
|
||||
Paid = 1,
|
||||
Reviewer = 2,
|
||||
Moderator = 3,
|
||||
Admin = 4,
|
||||
Owner = 5
|
||||
}
|
||||
}
|
||||
10
WeddingShare/Enums/ViewMode.cs
Normal file
10
WeddingShare/Enums/ViewMode.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace WeddingShare.Enums
|
||||
{
|
||||
public enum ViewMode
|
||||
{
|
||||
Default,
|
||||
Presentation,
|
||||
Slideshow,
|
||||
Single
|
||||
}
|
||||
}
|
||||
19
WeddingShare/Extensions/DictionaryExtensions.cs
Normal file
19
WeddingShare/Extensions/DictionaryExtensions.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
namespace WeddingShare.Extensions
|
||||
{
|
||||
public static class DictionaryExtensions
|
||||
{
|
||||
public static string GetValue(this IDictionary<string, string> value, string key, string defaultValue = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
if (value.ContainsKey(key))
|
||||
{
|
||||
return value[key];
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
50
WeddingShare/Extensions/HttpContextExtensions.cs
Normal file
50
WeddingShare/Extensions/HttpContextExtensions.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
namespace WeddingShare.Extensions
|
||||
{
|
||||
public static class HttpContextExtensions
|
||||
{
|
||||
public static string TryGetIpAddress(this HttpContext ctx)
|
||||
{
|
||||
try
|
||||
{
|
||||
var ipAddress = TryGetHeaderValue(ctx, ["CF-Connecting-IP", "CF-Connecting-IPv6", "X-Forwarded-For", "HTTP_X_FORWARDED_FOR", "REMOTE_ADDR"]);
|
||||
if (string.IsNullOrWhiteSpace(ipAddress) || ipAddress.Equals("Unknown", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return ctx.Connection?.RemoteIpAddress?.ToString() ?? "Unknown";
|
||||
}
|
||||
|
||||
return ipAddress;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
public static string TryGetCountry(this HttpContext ctx)
|
||||
{
|
||||
return ctx.TryGetHeaderValue(["CF-IPCountry"]);
|
||||
}
|
||||
|
||||
public static string TryGetHeaderValue(this HttpContext ctx, string[] headers)
|
||||
{
|
||||
foreach (var header in headers)
|
||||
{
|
||||
try
|
||||
{
|
||||
string? val = ctx.Request.Headers[header];
|
||||
if (!string.IsNullOrWhiteSpace(val))
|
||||
{
|
||||
var vals = val.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
if (vals.Length != 0)
|
||||
{
|
||||
return vals[0];
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return "Unknown";
|
||||
}
|
||||
}
|
||||
}
|
||||
196
WeddingShare/Extensions/UserClaimsExtentions.cs
Normal file
196
WeddingShare/Extensions/UserClaimsExtentions.cs
Normal file
@@ -0,0 +1,196 @@
|
||||
using System.Security.Claims;
|
||||
using System.Security.Principal;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Models;
|
||||
|
||||
namespace WeddingShare.Extensions
|
||||
{
|
||||
public static class UserClaimsExtentions
|
||||
{
|
||||
public static int GetUserId(this IIdentity identity)
|
||||
{
|
||||
try
|
||||
{
|
||||
return int.Parse(((ClaimsIdentity)identity).Claims.FirstOrDefault(x => string.Equals(ClaimTypes.Sid, x.Type, StringComparison.OrdinalIgnoreCase))?.Value ?? "-1");
|
||||
}
|
||||
catch { }
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static UserLevel GetUserLevel(this IIdentity identity)
|
||||
{
|
||||
try
|
||||
{
|
||||
var level = ((ClaimsIdentity)identity).Claims.FirstOrDefault(x => string.Equals(ClaimTypes.Role, x.Type, StringComparison.OrdinalIgnoreCase))?.Value;
|
||||
foreach (UserLevel l in Enum.GetValues(typeof(UserLevel)))
|
||||
{
|
||||
if (l.ToString().Equals(level, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return l;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return UserLevel.Free;
|
||||
}
|
||||
|
||||
public static bool IsPrivilegedUser(this IIdentity identity)
|
||||
{
|
||||
try
|
||||
{
|
||||
var userLevel = identity?.GetUserLevel() ?? UserLevel.Free;
|
||||
|
||||
return userLevel == UserLevel.Reviewer
|
||||
|| userLevel == UserLevel.Moderator
|
||||
|| userLevel == UserLevel.Admin
|
||||
|| userLevel == UserLevel.Owner;
|
||||
}
|
||||
catch { }
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool IsBasicUser(this IIdentity identity)
|
||||
{
|
||||
return identity == null || identity.IsPrivilegedUser() == false;
|
||||
}
|
||||
|
||||
public static Permissions GetUserPermissions(this IIdentity identity)
|
||||
{
|
||||
try
|
||||
{
|
||||
var level = identity.GetUserLevel();
|
||||
switch (level)
|
||||
{
|
||||
case UserLevel.Free:
|
||||
return new FreeUserPermissions();
|
||||
case UserLevel.Paid:
|
||||
return new PaidUserPermissions();
|
||||
case UserLevel.Reviewer:
|
||||
return new ReviewerPermissions();
|
||||
case UserLevel.Moderator:
|
||||
return new ModeratorPermissions();
|
||||
case UserLevel.Admin:
|
||||
return new AdminPermissions();
|
||||
case UserLevel.Owner:
|
||||
return new OwnerPermissions();
|
||||
default:
|
||||
return new Permissions();
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return new Permissions();
|
||||
}
|
||||
|
||||
public static int GetGalleryLimit(this IIdentity identity)
|
||||
{
|
||||
try
|
||||
{
|
||||
switch (identity.GetUserLevel())
|
||||
{
|
||||
case UserLevel.Free:
|
||||
case UserLevel.Reviewer:
|
||||
return 0;
|
||||
case UserLevel.Paid:
|
||||
return 3;
|
||||
case UserLevel.Moderator:
|
||||
case UserLevel.Admin:
|
||||
return 10;
|
||||
case UserLevel.Owner:
|
||||
return int.MaxValue;
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static AccountTabs GetDefaultTab(this IIdentity identity)
|
||||
{
|
||||
var userPermissions = identity?.GetUserPermissions() ?? new Permissions();
|
||||
if (userPermissions.Review.HasFlag(ReviewPermissions.View))
|
||||
{
|
||||
return AccountTabs.Reviews;
|
||||
}
|
||||
else if (userPermissions.Gallery.HasFlag(GalleryPermissions.View))
|
||||
{
|
||||
return AccountTabs.Galleries;
|
||||
}
|
||||
else if (userPermissions.Users.HasFlag(UserPermissions.View))
|
||||
{
|
||||
return AccountTabs.Users;
|
||||
}
|
||||
else if (userPermissions.CustomResources.HasFlag(CustomResourcePermissions.View))
|
||||
{
|
||||
return AccountTabs.Resources;
|
||||
}
|
||||
else if (userPermissions.Settings.HasFlag(SettingsPermissions.View))
|
||||
{
|
||||
return AccountTabs.Settings;
|
||||
}
|
||||
else if (userPermissions.Audit.HasFlag(AuditPermissions.View))
|
||||
{
|
||||
return AccountTabs.Audit;
|
||||
}
|
||||
|
||||
return AccountTabs.Reviews;
|
||||
}
|
||||
|
||||
public static bool CanEdit(this IIdentity identity, Enum type, int? ownerId)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (identity != null)
|
||||
{
|
||||
var level = identity.GetUserLevel();
|
||||
var permissions = identity.GetUserPermissions();
|
||||
|
||||
if (identity.IsPrivilegedUser())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var hasPermissions = false;
|
||||
if (type.GetType() == typeof(ReviewPermissions))
|
||||
{
|
||||
hasPermissions = permissions.Review.HasFlag(type);
|
||||
}
|
||||
else if (type.GetType() == typeof(GalleryPermissions))
|
||||
{
|
||||
hasPermissions = permissions.Gallery.HasFlag(type);
|
||||
}
|
||||
else if (type.GetType() == typeof(UserPermissions))
|
||||
{
|
||||
hasPermissions = permissions.Users.HasFlag(type);
|
||||
}
|
||||
else if (type.GetType() == typeof(CustomResourcePermissions))
|
||||
{
|
||||
hasPermissions = permissions.CustomResources.HasFlag(type);
|
||||
}
|
||||
else if (type.GetType() == typeof(SettingsPermissions))
|
||||
{
|
||||
hasPermissions = permissions.Settings.HasFlag(type);
|
||||
}
|
||||
else if (type.GetType() == typeof(AuditPermissions))
|
||||
{
|
||||
hasPermissions = permissions.Audit.HasFlag(type);
|
||||
}
|
||||
else if (type.GetType() == typeof(DataPermissions))
|
||||
{
|
||||
hasPermissions = permissions.Data.HasFlag(type);
|
||||
}
|
||||
|
||||
return hasPermissions && (ownerId != null && identity.GetUserId() == ownerId);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
32
WeddingShare/Handlers/GlobalExceptionHandler.cs
Normal file
32
WeddingShare/Handlers/GlobalExceptionHandler.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WeddingShare.Middleware
|
||||
{
|
||||
public class GlobalExceptionHandler : IExceptionHandler
|
||||
{
|
||||
private readonly ILogger<GlobalExceptionHandler> _logger;
|
||||
|
||||
public GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.LogError(exception, $"Exception occurred: {exception?.Message}");
|
||||
|
||||
var problemDetails = new ProblemDetails
|
||||
{
|
||||
Status = StatusCodes.Status500InternalServerError,
|
||||
Title = "Server error"
|
||||
};
|
||||
|
||||
httpContext.Response.StatusCode = problemDetails.Status.Value;
|
||||
|
||||
await httpContext.Response.WriteAsJsonAsync(problemDetails, cancellationToken);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
WeddingShare/Helpers/AuditHelper.cs
Normal file
43
WeddingShare/Helpers/AuditHelper.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.Helpers
|
||||
{
|
||||
public interface IAuditHelper
|
||||
{
|
||||
Task<bool> LogAction(string? user, string? action);
|
||||
}
|
||||
|
||||
public class AuditHelper : IAuditHelper
|
||||
{
|
||||
private readonly IDatabaseHelper _databaseHelper;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public AuditHelper(IDatabaseHelper databaseHelper, ILogger<AuditHelper> logger)
|
||||
{
|
||||
_databaseHelper = databaseHelper;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<bool> LogAction(string? user, string? action)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(user) && !string.IsNullOrWhiteSpace(action))
|
||||
{
|
||||
try
|
||||
{
|
||||
return await _databaseHelper.AddAuditLog(new AuditLogModel()
|
||||
{
|
||||
Username = user,
|
||||
Message = action
|
||||
}) != null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError($"Failed to log audit message '{action}' for user '{user}'", ex);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,53 +2,61 @@
|
||||
{
|
||||
public interface IConfigHelper
|
||||
{
|
||||
string? GetEnvironmentVariable(string key);
|
||||
string? GetConfigValue(string section, string key);
|
||||
string? Get(string section, string key);
|
||||
string GetOrDefault(string section, string key, string defaultValue);
|
||||
int GetOrDefault(string section, string key, int defaultValue);
|
||||
long GetOrDefault(string section, string key, long defaultValue);
|
||||
decimal GetOrDefault(string section, string key, decimal defaultValue);
|
||||
double GetOrDefault(string section, string key, double defaultValue);
|
||||
bool GetOrDefault(string section, string key, bool defaultValue);
|
||||
DateTime? GetOrDefault(string section, string key, DateTime? defaultValue);
|
||||
string? GetEnvironmentVariable(string key, string? galleryId = null);
|
||||
string? GetConfigValue(string key);
|
||||
string? Get(string key, string? galleryId = null);
|
||||
string GetOrDefault(string key, string defaultValue);
|
||||
int GetOrDefault(string key, int defaultValue);
|
||||
long GetOrDefault(string key, long defaultValue);
|
||||
decimal GetOrDefault(string key, decimal defaultValue);
|
||||
double GetOrDefault(string key, double defaultValue);
|
||||
bool GetOrDefault(string key, bool defaultValue);
|
||||
DateTime? GetOrDefault(string key, DateTime? defaultValue);
|
||||
}
|
||||
|
||||
public class ConfigHelper : IConfigHelper
|
||||
{
|
||||
private readonly IEnvironmentWrapper _environment;
|
||||
private readonly IConfiguration _configuration;
|
||||
private readonly ILogger<ConfigHelper> _logger;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public ConfigHelper(IConfiguration config, ILogger<ConfigHelper> logger)
|
||||
{
|
||||
public ConfigHelper(IEnvironmentWrapper environment, IConfiguration config, ILogger<ConfigHelper> logger)
|
||||
{
|
||||
_environment = environment;
|
||||
_configuration = config;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string? GetEnvironmentVariable(string key)
|
||||
public string? GetEnvironmentVariable(string key, string? galleryId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = Environment.GetEnvironmentVariable(key.Replace(":", "_").ToUpper());
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var envKey = !string.IsNullOrWhiteSpace(galleryId) ? $"{key}_{galleryId}" : key;
|
||||
if (!this.IsProtectedVariable(envKey))
|
||||
{
|
||||
return value;
|
||||
var keyName = string.Join('_', envKey.Split(':', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries).Skip(1)).Trim('_').ToUpper();
|
||||
|
||||
var value = _environment.GetEnvironmentVariable(keyName);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, $"Failed to get environment variable '{key}'");
|
||||
_logger.LogWarning(ex, $"Failed to get environment variable '{key}' for gallery '{galleryId}'");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public string? GetConfigValue(string section, string key)
|
||||
public string? GetConfigValue(string key)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = _configuration.GetValue<string>(!string.IsNullOrEmpty(section) ? $"{section}:{key}" : key);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = _configuration.GetValue<string>(key);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
@@ -61,20 +69,26 @@
|
||||
return null;
|
||||
}
|
||||
|
||||
public string? Get(string section, string key)
|
||||
public string? Get(string key, string? galleryId = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = !IsProtectedVariable($"{section}_{key}") ? this.GetEnvironmentVariable(key) : string.Empty;
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var envValue = this.GetEnvironmentVariable(key, galleryId);
|
||||
if (!string.IsNullOrWhiteSpace(envValue))
|
||||
{
|
||||
return value;
|
||||
return envValue;
|
||||
}
|
||||
|
||||
value = this.GetConfigValue(section, key);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
envValue = this.GetEnvironmentVariable(key);
|
||||
if (!string.IsNullOrWhiteSpace(envValue))
|
||||
{
|
||||
return value;
|
||||
return envValue;
|
||||
}
|
||||
|
||||
var configValue = this.GetConfigValue(key);
|
||||
if (!string.IsNullOrWhiteSpace(configValue))
|
||||
{
|
||||
return configValue;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -85,12 +99,12 @@
|
||||
return null;
|
||||
}
|
||||
|
||||
public string GetOrDefault(string section, string key, string defaultValue)
|
||||
public string GetOrDefault(string key, string defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.Get(section, key);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.Get(key);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
@@ -100,12 +114,12 @@
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public int GetOrDefault(string section, string key, int defaultValue)
|
||||
public int GetOrDefault(string key, int defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.GetOrDefault(section, key, string.Empty);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.GetOrDefault(key, string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToInt32(value);
|
||||
}
|
||||
@@ -115,12 +129,12 @@
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public long GetOrDefault(string section, string key, long defaultValue)
|
||||
public long GetOrDefault(string key, long defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.GetOrDefault(section, key, string.Empty);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.GetOrDefault(key, string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToInt64(value);
|
||||
}
|
||||
@@ -130,12 +144,12 @@
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public decimal GetOrDefault(string section, string key, decimal defaultValue)
|
||||
public decimal GetOrDefault(string key, decimal defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.GetOrDefault(section, key, string.Empty);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.GetOrDefault(key, string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToDecimal(value);
|
||||
}
|
||||
@@ -145,12 +159,12 @@
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public double GetOrDefault(string section, string key, double defaultValue)
|
||||
public double GetOrDefault(string key, double defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.GetOrDefault(section, key, string.Empty);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.GetOrDefault(key, string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToDouble(value);
|
||||
}
|
||||
@@ -160,12 +174,12 @@
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public bool GetOrDefault(string section, string key, bool defaultValue)
|
||||
public bool GetOrDefault(string key, bool defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.GetOrDefault(section, key, string.Empty);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.GetOrDefault(key, string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToBoolean(value);
|
||||
}
|
||||
@@ -175,12 +189,12 @@
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
public DateTime? GetOrDefault(string section, string key, DateTime? defaultValue)
|
||||
public DateTime? GetOrDefault(string key, DateTime? defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
var value = this.GetOrDefault(section, key, string.Empty);
|
||||
if (!string.IsNullOrEmpty(value))
|
||||
var value = this.GetOrDefault(key, string.Empty);
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToDateTime(value);
|
||||
}
|
||||
|
||||
101
WeddingShare/Helpers/Database/IDatabaseHelper.cs
Normal file
101
WeddingShare/Helpers/Database/IDatabaseHelper.cs
Normal file
@@ -0,0 +1,101 @@
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.Helpers.Database
|
||||
{
|
||||
public interface IDatabaseHelper
|
||||
{
|
||||
#region Gallery
|
||||
Task<int> GetGalleryCount();
|
||||
Task<IDictionary<string, string>> GetGalleryNames();
|
||||
Task<List<GalleryModel>> GetAllGalleries();
|
||||
Task<List<GalleryModel>> GetUserGalleries(int userId);
|
||||
Task<int?> GetGalleryId(string identifier);
|
||||
Task<int?> GetGalleryIdByName(string name);
|
||||
Task<string?> GetGalleryIdentifier(int id);
|
||||
Task<string?> GetGalleryName(int id);
|
||||
Task<GalleryModel?> GetAllGallery();
|
||||
Task<GalleryModel?> GetGallery(int id);
|
||||
Task<GalleryModel?> AddGallery(GalleryModel model);
|
||||
Task<GalleryModel?> EditGallery(GalleryModel model);
|
||||
Task<bool> WipeGallery(GalleryModel model);
|
||||
Task<bool> WipeAllGalleries();
|
||||
Task<bool> DeleteGallery(GalleryModel model);
|
||||
#endregion
|
||||
|
||||
#region Gallery Items
|
||||
Task<IDictionary<string, long>> GetGalleryItemCount(int? galleryId, GalleryItemState state = GalleryItemState.All, MediaType type = MediaType.All, ImageOrientation orientation = ImageOrientation.None);
|
||||
Task<List<GalleryItemModel>> GetAllGalleryItems(int? galleryId, GalleryItemState state = GalleryItemState.All, MediaType type = MediaType.All, ImageOrientation orientation = ImageOrientation.None, GalleryGroup group = GalleryGroup.None, GalleryOrder order = GalleryOrder.Descending, int limit = int.MaxValue, int page = 1);
|
||||
Task<long> GetPendingGalleryItemCount(int? galleryId = null);
|
||||
Task<List<GalleryItemModel>> GetPendingGalleryItems(int? galleryId = null);
|
||||
Task<List<GalleryItemModel>> GetUserPendingGalleryItems(int userId, int? galleryId = null);
|
||||
Task<GalleryItemModel?> GetPendingGalleryItem(int id);
|
||||
Task<GalleryItemModel?> GetGalleryItem(int id);
|
||||
Task<GalleryItemModel?> GetGalleryItemByChecksum(int galleryId, string checksum);
|
||||
Task<GalleryItemModel?> AddGalleryItem(GalleryItemModel model);
|
||||
Task<GalleryItemModel?> EditGalleryItem(GalleryItemModel model);
|
||||
Task<bool> DeleteGalleryItem(GalleryItemModel model);
|
||||
#endregion
|
||||
|
||||
#region Gallery Item Likes
|
||||
Task<long> GetGalleryItemLikesCount(int galleryItemId);
|
||||
Task<IEnumerable<GalleryItemLikeModel>> GetGalleryItemLikes(int galleryItemId);
|
||||
Task<IEnumerable<GalleryItemLikeModel>> GetUsersGalleryItemLikes(int userId);
|
||||
Task<bool> CheckUserHasLikedGalleryItem(int galleryItemId, int userId);
|
||||
Task<long> LikeGalleryItem(GalleryItemLikeModel model);
|
||||
Task<long> UnLikeGalleryItem(GalleryItemLikeModel model);
|
||||
#endregion
|
||||
|
||||
#region Users
|
||||
Task<bool> InitOwnerAccount(UserModel model);
|
||||
Task<bool> ValidateCredentials(string username, string password);
|
||||
Task<List<UserModel>?> GetAllUsers();
|
||||
Task<UserModel?> GetUser(int id);
|
||||
Task<UserModel?> GetUserByUsername(string name);
|
||||
Task<UserModel?> GetUserByEmail(string email);
|
||||
Task<UserModel?> AddUser(UserModel model);
|
||||
Task<UserModel?> EditUser(UserModel model);
|
||||
Task<bool> DeleteUser(UserModel model);
|
||||
Task<bool> ChangePassword(UserModel model);
|
||||
Task<string> SetUserSecret(int id, string secretCode);
|
||||
Task<bool> VerifyUserSecret(int id, string secretCode);
|
||||
Task<int> IncrementLockoutCount(int id);
|
||||
Task<bool> SetLockout(int id, DateTime? datetime);
|
||||
Task<bool> ResetLockoutCount(int id);
|
||||
Task<bool> SetMultiFactorToken(int id, string token);
|
||||
Task<bool> ResetMultiFactorToDefault();
|
||||
#endregion
|
||||
|
||||
#region Backups
|
||||
Task<bool> Import(string path);
|
||||
Task<bool> Export(string path);
|
||||
#endregion
|
||||
|
||||
#region Settings
|
||||
Task<IEnumerable<SettingModel>?> GetAllSettings(int? galleryId = null);
|
||||
Task<SettingModel?> GetSetting(string id);
|
||||
Task<SettingModel?> GetSetting(string id, int gallery);
|
||||
Task<SettingModel?> GetGallerySpecificSetting(string id, int galleryId);
|
||||
Task<SettingModel?> AddSetting(SettingModel model, int? galleryId = null);
|
||||
Task<SettingModel?> EditSetting(SettingModel model, int? galleryId = null);
|
||||
Task<SettingModel?> SetSetting(SettingModel model, int? galleryId = null);
|
||||
Task<bool> DeleteSetting(SettingModel model, int? galleryId = null);
|
||||
Task<bool> DeleteAllSettings(int? galleryId = null);
|
||||
#endregion
|
||||
|
||||
#region Custom Resources
|
||||
Task<CustomResourceModel?> GetCustomResource(int id);
|
||||
Task<List<CustomResourceModel>> GetAllCustomResources();
|
||||
Task<List<CustomResourceModel>> GetUserCustomResources(int userId);
|
||||
Task<CustomResourceModel?> AddCustomResource(CustomResourceModel model);
|
||||
Task<CustomResourceModel?> EditCustomResource(CustomResourceModel model);
|
||||
Task<bool> DeleteCustomResource(CustomResourceModel model);
|
||||
#endregion
|
||||
|
||||
#region Audit
|
||||
Task<IEnumerable<AuditLogModel>?> GetAuditLogs(string term = "", int limit = 100);
|
||||
Task<IEnumerable<AuditLogModel>?> GetUserAuditLogs(int userId, string term = "", int limit = 100);
|
||||
Task<AuditLogModel?> AddAuditLog(AuditLogModel model);
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
2306
WeddingShare/Helpers/Database/MySqlDatabaseHelper.cs
Normal file
2306
WeddingShare/Helpers/Database/MySqlDatabaseHelper.cs
Normal file
File diff suppressed because it is too large
Load Diff
2315
WeddingShare/Helpers/Database/SQLiteDatabaseHelper.cs
Normal file
2315
WeddingShare/Helpers/Database/SQLiteDatabaseHelper.cs
Normal file
File diff suppressed because it is too large
Load Diff
138
WeddingShare/Helpers/Dbup/DbupHelper.cs
Normal file
138
WeddingShare/Helpers/Dbup/DbupHelper.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System.Reflection;
|
||||
using System.Text.RegularExpressions;
|
||||
using DbUp;
|
||||
using DbUp.Engine;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Enums;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.Helpers.Dbup
|
||||
{
|
||||
public sealed class DbupMigrator(IEnvironmentWrapper environment, IConfiguration configuration, IDatabaseHelper database, IFileHelper fileHelper, IEncryptionHelper encryption, ILoggerFactory loggerFactory) : BackgroundService
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var logger = loggerFactory.CreateLogger<DbupMigrator>();
|
||||
|
||||
fileHelper.CreateDirectoryIfNotExists("config");
|
||||
|
||||
var config = new ConfigHelper(environment, configuration, loggerFactory.CreateLogger<ConfigHelper>());
|
||||
|
||||
var connString = config.GetOrDefault(Settings.Database.ConnectionString, "Data Source=./config/wedding-share.db");
|
||||
if (!string.IsNullOrWhiteSpace(connString))
|
||||
{
|
||||
DatabaseUpgradeResult? dbupResult;
|
||||
|
||||
var dbType = config.GetOrDefault(Settings.Database.Type, "sqlite")?.ToLower();
|
||||
switch (dbType)
|
||||
{
|
||||
case "sqlite":
|
||||
dbupResult = new DbupSqliteHelper().Migrate(connString);
|
||||
break;
|
||||
case "mysql":
|
||||
var databaseName = config.GetOrDefault(Settings.Database.DatabaseName, "weddingshare");
|
||||
dbupResult = new DbupMySqlHelper().Migrate(connString, databaseName);
|
||||
break;
|
||||
default:
|
||||
var error = $"Database type '{dbType}' is not yet supported by this application";
|
||||
logger.LogWarning(error);
|
||||
throw new NotImplementedException(error);
|
||||
}
|
||||
|
||||
if (dbupResult != null && !dbupResult.Successful)
|
||||
{
|
||||
var message = $"DBUP failed with error: '{dbupResult?.Error?.Message}' - '{dbupResult?.Error?.ToString()}'";
|
||||
logger.LogCritical(message);
|
||||
Environment.FailFast(message);
|
||||
}
|
||||
|
||||
if (config.GetOrDefault(Settings.Database.SyncFromConfig, false))
|
||||
{
|
||||
logger.LogWarning($"Sync_From_Config set to true, wiping settings database and re-pulling values from config");
|
||||
await database.DeleteAllSettings();
|
||||
}
|
||||
|
||||
var isDemoMode = config.GetOrDefault(Settings.IsDemoMode, false);
|
||||
await database.SetSetting(new SettingModel()
|
||||
{
|
||||
Id = Settings.IsDemoMode.ToUpper(),
|
||||
Value = isDemoMode.ToString()
|
||||
});
|
||||
|
||||
var username = !isDemoMode ? config.GetOrDefault(Settings.Account.Admin.Username, config.GetOrDefault(Settings.Account.Owner.Username, "admin")).ToLower() : "demo";
|
||||
var ownerAccount = new UserModel()
|
||||
{
|
||||
Username = username,
|
||||
Password = encryption.Encrypt(!isDemoMode ? config.GetOrDefault(Settings.Account.Admin.Password, config.GetOrDefault(Settings.Account.Owner.Password, "admin")) : "demo", username)
|
||||
};
|
||||
await database.InitOwnerAccount(ownerAccount);
|
||||
|
||||
await new DbupImporter(config, database, loggerFactory.CreateLogger<DbupImporter>()).ImportSettings();
|
||||
|
||||
if (config.GetOrDefault(Settings.Account.Owner.LogPassword, false))
|
||||
{
|
||||
logger.LogInformation($"Password: {ownerAccount.Password}");
|
||||
}
|
||||
|
||||
if (config.GetOrDefault(Security.MultiFactor.ResetToDefault, false))
|
||||
{
|
||||
await database.ResetMultiFactorToDefault();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError($"DBUP failed with error: 'Connection string was null or empty'");
|
||||
throw new ArgumentNullException("Please specify a valid database connection string");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DbupSqliteHelper
|
||||
{
|
||||
public DatabaseUpgradeResult Migrate(string connectionString)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dbupBuilder = DeployChanges.To
|
||||
.SqliteDatabase(connectionString)
|
||||
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
|
||||
.WithScriptNameComparer(new DbupScriptComparer())
|
||||
.WithFilter(new DbupScriptFilter(DatabaseType.SQLite))
|
||||
.LogToConsole();
|
||||
dbupBuilder.Configure(c => c.Journal = new DbupSQLiteTableJournal(() => c.ConnectionManager, () => c.Log, "schemaversions"));
|
||||
|
||||
return dbupBuilder.Build().PerformUpgrade();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new DatabaseUpgradeResult(null, false, ex, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class DbupMySqlHelper
|
||||
{
|
||||
public DatabaseUpgradeResult Migrate(string connectionString, string database)
|
||||
{
|
||||
try
|
||||
{
|
||||
database = !string.IsNullOrWhiteSpace(database) ? database : Regex.Match(connectionString, "Database\\=(.+?)(;|$)", RegexOptions.IgnoreCase | RegexOptions.Multiline).Groups[1].Value;
|
||||
|
||||
var dbupBuilder = DeployChanges.To
|
||||
.MySqlDatabase(connectionString)
|
||||
.WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
|
||||
.WithScriptNameComparer(new DbupScriptComparer())
|
||||
.WithFilter(new DbupScriptFilter(DatabaseType.MySQL))
|
||||
.LogToConsole();
|
||||
dbupBuilder.Configure(c => c.Journal = new DbupMySqlTableJournal(() => c.ConnectionManager, () => c.Log, database, "schemaversions"));
|
||||
|
||||
return dbupBuilder.Build().PerformUpgrade();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new DatabaseUpgradeResult(null, false, ex, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
163
WeddingShare/Helpers/Dbup/DbupImporter.cs
Normal file
163
WeddingShare/Helpers/Dbup/DbupImporter.cs
Normal file
@@ -0,0 +1,163 @@
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using WeddingShare.Constants;
|
||||
using WeddingShare.Helpers.Database;
|
||||
using WeddingShare.Models.Database;
|
||||
|
||||
namespace WeddingShare.Helpers.Dbup
|
||||
{
|
||||
public class DbupImporter(IConfigHelper config, IDatabaseHelper database, ILogger<DbupImporter> logger)
|
||||
{
|
||||
public async Task ImportSettings()
|
||||
{
|
||||
try
|
||||
{
|
||||
var galleries = (await database.GetAllGalleries())?.Where(x => !x.Identifier.Equals("All", StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
var settings = await database.GetAllSettings();
|
||||
if (settings == null || !settings.Any(setting => setting.Id.StartsWith(Settings.Basic.BaseKey, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
var systemKeys = GetAllKeys();
|
||||
foreach (var key in systemKeys)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (settings == null || !settings.Any(setting => setting.Id.Equals(key, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
var configVal = config.Get(key);
|
||||
if (!string.IsNullOrWhiteSpace(configVal))
|
||||
{
|
||||
await database.AddSetting(new SettingModel()
|
||||
{
|
||||
Id = key,
|
||||
Value = configVal
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
if (galleries != null && galleries.Any())
|
||||
{
|
||||
var galleryKeys = GetKeys<Constants.Settings.Gallery>();
|
||||
foreach (var gallery in galleries)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(gallery?.Name))
|
||||
{
|
||||
foreach (var key in galleryKeys)
|
||||
{
|
||||
try
|
||||
{
|
||||
var galleryOverride = config.GetEnvironmentVariable(key, gallery.Name);
|
||||
if (!string.IsNullOrWhiteSpace(galleryOverride))
|
||||
{
|
||||
await database.AddSetting(new SettingModel()
|
||||
{
|
||||
Id = key,
|
||||
Value = galleryOverride
|
||||
}, gallery.Id);
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Protect any galleries without a secret key by forcing a new one
|
||||
if (galleries != null && galleries.Any())
|
||||
{
|
||||
foreach (var gallery in galleries.Where(gallery => string.IsNullOrWhiteSpace(gallery.SecretKey)))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (gallery.Identifier.Equals("default", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
gallery.SecretKey = config.GetOrDefault(Settings.Basic.DefaultGallerySecretKey, PasswordHelper.GenerateGallerySecretKey());
|
||||
}
|
||||
else
|
||||
{
|
||||
gallery.SecretKey = PasswordHelper.GenerateGallerySecretKey();
|
||||
}
|
||||
|
||||
await database.EditGallery(gallery);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError($"Failed to import settings at startup - {ex?.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetAllKeys()
|
||||
{
|
||||
var keys = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
keys.AddRange(GetKeys<Constants.BackgroundServices>());
|
||||
keys.AddRange(GetKeys<Constants.Notifications>());
|
||||
keys.AddRange(GetKeys<Constants.Security>());
|
||||
keys.AddRange(GetKeys<Constants.Settings>());
|
||||
}
|
||||
catch { }
|
||||
|
||||
return keys.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct();
|
||||
}
|
||||
|
||||
private IEnumerable<string> GetKeys<T>(bool includeNesteted = true)
|
||||
{
|
||||
var keys = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
var obj = Activator.CreateInstance<T>();
|
||||
foreach (var val in GetConstants(typeof(T), includeNesteted))
|
||||
{
|
||||
keys.Add((string)(val.GetValue(obj) ?? string.Empty));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return keys.Where(x => !string.IsNullOrWhiteSpace(x));
|
||||
}
|
||||
|
||||
private FieldInfo[] GetConstants(Type type, bool includeNesteted)
|
||||
{
|
||||
var constants = new ArrayList();
|
||||
|
||||
try
|
||||
{
|
||||
if (includeNesteted)
|
||||
{
|
||||
var classInfos = type.GetNestedTypes(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
|
||||
foreach (var ci in classInfos)
|
||||
{
|
||||
var consts = GetConstants(ci, includeNesteted);
|
||||
if (consts != null && consts.Length > 0)
|
||||
{
|
||||
constants.AddRange(consts);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var fieldInfos = type.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy);
|
||||
foreach (var fi in fieldInfos)
|
||||
{
|
||||
if (fi.IsLiteral && !fi.IsInitOnly)
|
||||
{
|
||||
constants.Add(fi);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return (FieldInfo[])constants.ToArray(typeof(FieldInfo));
|
||||
}
|
||||
}
|
||||
}
|
||||
33
WeddingShare/Helpers/Dbup/DbupMySqlTableJournal.cs
Normal file
33
WeddingShare/Helpers/Dbup/DbupMySqlTableJournal.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Data;
|
||||
using DbUp.Engine;
|
||||
using DbUp.Engine.Output;
|
||||
using DbUp.Engine.Transactions;
|
||||
using DbUp.MySql;
|
||||
|
||||
namespace WeddingShare.Helpers.Dbup
|
||||
{
|
||||
public class DbupMySqlTableJournal : MySqlTableJournal
|
||||
{
|
||||
public DbupMySqlTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string scheme, string table)
|
||||
: base(connectionManager, logger, scheme, table)
|
||||
{
|
||||
}
|
||||
|
||||
public override void StoreExecutedScript(SqlScript script, Func<IDbCommand> dbCommandFactory)
|
||||
{
|
||||
var scriptName = script.Name;
|
||||
|
||||
try
|
||||
{
|
||||
var parts = script.Name.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts != null && parts.Length >= 2)
|
||||
{
|
||||
scriptName = string.Join(".", parts.Skip(parts.Length - 2).Take(2));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
base.StoreExecutedScript(new SqlScript(scriptName, script.Contents), dbCommandFactory);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
WeddingShare/Helpers/Dbup/DbupSQLiteTableJournal.cs
Normal file
33
WeddingShare/Helpers/Dbup/DbupSQLiteTableJournal.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System.Data;
|
||||
using DbUp.Engine;
|
||||
using DbUp.Engine.Output;
|
||||
using DbUp.Engine.Transactions;
|
||||
using DbUp.Sqlite;
|
||||
|
||||
namespace WeddingShare.Helpers.Dbup
|
||||
{
|
||||
public class DbupSQLiteTableJournal : SqliteTableJournal
|
||||
{
|
||||
public DbupSQLiteTableJournal(Func<IConnectionManager> connectionManager, Func<IUpgradeLog> logger, string table)
|
||||
: base(connectionManager, logger, table)
|
||||
{
|
||||
}
|
||||
|
||||
public override void StoreExecutedScript(SqlScript script, Func<IDbCommand> dbCommandFactory)
|
||||
{
|
||||
var scriptName = script.Name;
|
||||
|
||||
try
|
||||
{
|
||||
var parts = script.Name.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts != null && parts.Length >= 2)
|
||||
{
|
||||
scriptName = string.Join(".", parts.Skip(parts.Length - 2).Take(2));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
base.StoreExecutedScript(new SqlScript(scriptName, script.Contents), dbCommandFactory);
|
||||
}
|
||||
}
|
||||
}
|
||||
31
WeddingShare/Helpers/Dbup/DbupScriptComparer.cs
Normal file
31
WeddingShare/Helpers/Dbup/DbupScriptComparer.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace WeddingShare.Helpers.Dbup
|
||||
{
|
||||
public class DbupScriptComparer : IComparer<string>
|
||||
{
|
||||
public DbupScriptComparer()
|
||||
{
|
||||
}
|
||||
|
||||
public int Compare([AllowNull] string x, [AllowNull] string y)
|
||||
{
|
||||
return GetFileName(x).ToLower().CompareTo(GetFileName(y).ToLower());
|
||||
}
|
||||
|
||||
private string GetFileName(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parts = name.Split('.', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (parts != null && parts.Length >= 2)
|
||||
{
|
||||
return string.Join(".", parts.Skip(parts.Length - 2).Take(2));
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
WeddingShare/Helpers/Dbup/DbupScriptFilter.cs
Normal file
30
WeddingShare/Helpers/Dbup/DbupScriptFilter.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using DbUp.Engine;
|
||||
using DbUp.Support;
|
||||
using WeddingShare.Enums;
|
||||
|
||||
namespace WeddingShare.Helpers.Dbup
|
||||
{
|
||||
public class DbupScriptFilter : IScriptFilter
|
||||
{
|
||||
private readonly DatabaseType _dbType;
|
||||
|
||||
public DbupScriptFilter(DatabaseType dbType)
|
||||
{
|
||||
_dbType = dbType;
|
||||
}
|
||||
|
||||
public IEnumerable<SqlScript> Filter(IEnumerable<SqlScript> sorted, HashSet<string> executedScriptNames, ScriptNameComparer comparer)
|
||||
{
|
||||
var scripts = sorted.Where(s => s.SqlScriptOptions.ScriptType == ScriptType.RunAlways || !executedScriptNames.Contains(s.Name, comparer));
|
||||
switch (_dbType)
|
||||
{
|
||||
case DatabaseType.SQLite:
|
||||
return scripts.Where(s => s.Name.ToLower().Contains(".sqlscripts.sqlite."));
|
||||
case DatabaseType.MySQL:
|
||||
return scripts.Where(s => s.Name.ToLower().Contains(".sqlscripts.mysql."));
|
||||
default:
|
||||
return new List<SqlScript>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
36
WeddingShare/Helpers/DeviceDetector.cs
Normal file
36
WeddingShare/Helpers/DeviceDetector.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System.Text.RegularExpressions;
|
||||
using WeddingShare.Enums;
|
||||
|
||||
namespace WeddingShare.Helpers
|
||||
{
|
||||
public interface IDeviceDetector
|
||||
{
|
||||
Task<DeviceType> ParseDeviceType(string userAgent);
|
||||
}
|
||||
|
||||
public class DeviceDetector : IDeviceDetector
|
||||
{
|
||||
public async Task<DeviceType> ParseDeviceType(string userAgent)
|
||||
{
|
||||
return await Task.Run(() =>
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(userAgent))
|
||||
{
|
||||
return DeviceType.Unknown;
|
||||
}
|
||||
|
||||
if (Regex.IsMatch(userAgent, "(tablet|ipad|playbook|silk)|(android(?!.*mobile))", RegexOptions.IgnoreCase))
|
||||
{
|
||||
return DeviceType.Tablet;
|
||||
}
|
||||
|
||||
if (Regex.IsMatch(userAgent, "blackberry|iphone|mobile|windows ce|opera mini|htc|sony|palm|symbianos|ipad|ipod|blackberry|bada|kindle|symbian|sonyericsson|android|samsung|nokia|wap|motor", RegexOptions.IgnoreCase))
|
||||
{
|
||||
return DeviceType.Mobile;
|
||||
}
|
||||
|
||||
return DeviceType.Desktop;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
23
WeddingShare/Helpers/EmailValidationHelper.cs
Normal file
23
WeddingShare/Helpers/EmailValidationHelper.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Net.Mail;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace WeddingShare.Helpers
|
||||
{
|
||||
public class EmailValidationHelper
|
||||
{
|
||||
public static bool IsValid(string? email)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(email) && email.Length < 100)
|
||||
{
|
||||
try
|
||||
{
|
||||
var mail = new MailAddress(email);
|
||||
return Regex.IsMatch(mail.Address, @"^.+?\@.+?\..+?$", RegexOptions.Compiled);
|
||||
}
|
||||
catch { }
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
27
WeddingShare/Helpers/EncodingHelper.cs
Normal file
27
WeddingShare/Helpers/EncodingHelper.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System.Text;
|
||||
|
||||
namespace WeddingShare.Helpers
|
||||
{
|
||||
public class EncodingHelper
|
||||
{
|
||||
public static string Base64Encode(string value)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Convert.ToBase64String(Encoding.UTF8.GetBytes(value));
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
public static string Base64Decode(string value)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(value))
|
||||
{
|
||||
return Encoding.UTF8.GetString(Convert.FromBase64String(value));
|
||||
}
|
||||
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user