Compare commits
33
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
157f03b655 | ||
|
|
9b7ca3d692 | ||
|
|
e51a59a829 | ||
|
|
62cadbcfa0 | ||
|
|
d7aefc4110 | ||
|
|
e972a8dddb | ||
|
|
88b9375a4e | ||
|
|
f7a3e3f0f8 | ||
|
|
72df3dec3b | ||
|
|
b80ec5c6ac | ||
|
|
fbd3aff6d8 | ||
|
|
0ad79c9bc1 | ||
|
|
e175d28c2c | ||
|
|
8e3f09774a | ||
|
|
dbc54032f7 | ||
|
|
047b99e2aa | ||
|
|
1859078cf4 | ||
|
|
c44ba91ec7 | ||
|
|
96a6d0ee5d | ||
|
|
5619bb27a1 | ||
|
|
dfc99c7d15 | ||
|
|
fb82f505b6 | ||
|
|
5970340a17 | ||
|
|
9a77e76ff4 | ||
|
|
3591415d46 | ||
|
|
ab06205744 | ||
|
|
0689e680fa | ||
|
|
297d5aca1f | ||
|
|
0a023c1556 | ||
|
|
aa600af7b2 | ||
|
|
fc4aae8c0c | ||
|
|
24c0b5fa52 | ||
|
|
2af2af3460 |
@@ -0,0 +1,53 @@
|
|||||||
|
name: ci
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- main
|
||||||
|
- staging
|
||||||
|
tags:
|
||||||
|
- '*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
docker:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- service: frontend
|
||||||
|
context: frontend
|
||||||
|
dockerfile: frontend/Dockerfile
|
||||||
|
- service: backend
|
||||||
|
context: backend
|
||||||
|
dockerfile: backend/Dockerfile
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout with submodules
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
|
- name: Login to Docker Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ${{ vars.REGISTRY_URL }}
|
||||||
|
username: ${{ vars.REGISTRY_USER }}
|
||||||
|
password: ${{ secrets.REGISTRY_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v3
|
||||||
|
with:
|
||||||
|
driver: remote
|
||||||
|
endpoint: ${{ env.BUILDKIT_ARM64_ENDPOINT }}
|
||||||
|
|
||||||
|
- name: Build and Push ${{ matrix.service }} Image
|
||||||
|
uses: docker/build-push-action@v6
|
||||||
|
with:
|
||||||
|
context: ${{ matrix.context }}
|
||||||
|
file: ${{ matrix.dockerfile }}
|
||||||
|
platforms: linux/arm64
|
||||||
|
push: true
|
||||||
|
provenance: false
|
||||||
|
tags: |
|
||||||
|
${{ vars.REGISTRY_URL }}/${{ gitea.repository }}-${{ matrix.service }}:${{ gitea.ref_type == 'tag' && gitea.ref_name || (gitea.ref_name == 'main' && 'latest' || gitea.ref_name) }}
|
||||||
|
${{ vars.REGISTRY_URL }}/${{ gitea.repository }}-${{ matrix.service }}:${{ gitea.sha }}
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
/backend/.env
|
/backend/.env
|
||||||
/backend/.env.*
|
/backend/.env.*
|
||||||
/backend/.cargo/
|
/backend/.cargo/
|
||||||
|
backend/libpdfium.*
|
||||||
|
|
||||||
# Node/Frontend
|
# Node/Frontend
|
||||||
/frontend/node_modules/
|
/frontend/node_modules/
|
||||||
|
|||||||
@@ -0,0 +1,661 @@
|
|||||||
|
GNU AFFERO GENERAL PUBLIC LICENSE
|
||||||
|
Version 3, 19 November 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 Affero General Public License is a free, copyleft license for
|
||||||
|
software and other kinds of works, specifically designed to ensure
|
||||||
|
cooperation with the community in the case of network server software.
|
||||||
|
|
||||||
|
The licenses for most software and other practical works are designed
|
||||||
|
to take away your freedom to share and change the works. By contrast,
|
||||||
|
our General Public Licenses are 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.
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
Developers that use our General Public Licenses protect your rights
|
||||||
|
with two steps: (1) assert copyright on the software, and (2) offer
|
||||||
|
you this License which gives you legal permission to copy, distribute
|
||||||
|
and/or modify the software.
|
||||||
|
|
||||||
|
A secondary benefit of defending all users' freedom is that
|
||||||
|
improvements made in alternate versions of the program, if they
|
||||||
|
receive widespread use, become available for other developers to
|
||||||
|
incorporate. Many developers of free software are heartened and
|
||||||
|
encouraged by the resulting cooperation. However, in the case of
|
||||||
|
software used on network servers, this result may fail to come about.
|
||||||
|
The GNU General Public License permits making a modified version and
|
||||||
|
letting the public access it on a server without ever releasing its
|
||||||
|
source code to the public.
|
||||||
|
|
||||||
|
The GNU Affero General Public License is designed specifically to
|
||||||
|
ensure that, in such cases, the modified source code becomes available
|
||||||
|
to the community. It requires the operator of a network server to
|
||||||
|
provide the source code of the modified version running there to the
|
||||||
|
users of that server. Therefore, public use of a modified version, on
|
||||||
|
a publicly accessible server, gives the public access to the source
|
||||||
|
code of the modified version.
|
||||||
|
|
||||||
|
An older license, called the Affero General Public License and
|
||||||
|
published by Affero, was designed to accomplish similar goals. This is
|
||||||
|
a different license, not a version of the Affero GPL, but Affero has
|
||||||
|
released a new version of the Affero GPL which permits relicensing under
|
||||||
|
this license.
|
||||||
|
|
||||||
|
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 Affero 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. Remote Network Interaction; Use with the GNU General Public License.
|
||||||
|
|
||||||
|
Notwithstanding any other provision of this License, if you modify the
|
||||||
|
Program, your modified version must prominently offer all users
|
||||||
|
interacting with it remotely through a computer network (if your version
|
||||||
|
supports such interaction) an opportunity to receive the Corresponding
|
||||||
|
Source of your version by providing access to the Corresponding Source
|
||||||
|
from a network server at no charge, through some standard or customary
|
||||||
|
means of facilitating copying of software. This Corresponding Source
|
||||||
|
shall include the Corresponding Source for any work covered by version 3
|
||||||
|
of the GNU General Public License that is incorporated pursuant to the
|
||||||
|
following paragraph.
|
||||||
|
|
||||||
|
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 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 work with which it is combined will remain governed by version
|
||||||
|
3 of the GNU General Public License.
|
||||||
|
|
||||||
|
14. Revised Versions of this License.
|
||||||
|
|
||||||
|
The Free Software Foundation may publish revised and/or new versions of
|
||||||
|
the GNU Affero 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 Affero 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 Affero 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 Affero 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 Affero 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 Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero 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 your software can interact with users remotely through a computer
|
||||||
|
network, you should also make sure that it provides a way for users to
|
||||||
|
get its source. For example, if your program is a web application, its
|
||||||
|
interface could display a "Source" link that leads users to an archive
|
||||||
|
of the code. There are many ways you could offer source, and different
|
||||||
|
solutions will be better for different programs; see section 13 for the
|
||||||
|
specific requirements.
|
||||||
|
|
||||||
|
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 AGPL, see
|
||||||
|
<https://www.gnu.org/licenses/>.
|
||||||
@@ -1,12 +1,25 @@
|
|||||||
# Paperless-NEO
|
# Papercrate
|
||||||
|
|
||||||
|
## Local Development
|
||||||
|
|
||||||
|
Use the provided `papercrate.tmux` to spin up the full stack in one tmux session:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
tmux -f papercrate.tmux attach
|
||||||
|
```
|
||||||
|
|
||||||
|
This creates windows for the compose stack, frontend dev server, backend API, and background worker using the repository-relative paths defined in the tmux file. Detach with `Ctrl+b d` and reattach later with the same command.
|
||||||
|
|
||||||
## Backend Integration Tests
|
## Backend Integration Tests
|
||||||
|
|
||||||
Integration tests require a running Postgres instance. The repository includes a lightweight compose file for local runs:
|
Integration tests require a running Postgres instance (and, optionally, Quickwit for OCR indexing). The repository includes a lightweight compose file for local runs:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker compose -f docker-compose.test.yml up -d
|
docker compose -f docker-compose.test.yml up -d
|
||||||
export TEST_DATABASE_URL=postgres://paperless:paperless_test@localhost:5433/paperless_test
|
export TEST_DATABASE_URL=postgres://papercrate:papercrate_test@localhost:5433/papercrate_test
|
||||||
|
# optional, enables Quickwit indexing jobs
|
||||||
|
export QUICKWIT_ENDPOINT=http://localhost:7280
|
||||||
|
export QUICKWIT_INDEX=documents
|
||||||
cargo test
|
cargo test
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -17,3 +30,44 @@ docker compose -f docker-compose.test.yml down
|
|||||||
```
|
```
|
||||||
|
|
||||||
The compose service uses tmpfs storage, giving each test run a clean database.
|
The compose service uses tmpfs storage, giving each test run a clean database.
|
||||||
|
|
||||||
|
## Runtime Dependencies
|
||||||
|
|
||||||
|
- `ocrmypdf` (optional but recommended): Used by the OCR worker to extract text from PDFs when no embedded text layer is available. Ensure it is installed and available on the worker hosts if OCR is desired.
|
||||||
|
- Quickwit (optional): The Quickwit indexer is used to ingest extracted text for search. Set `QUICKWIT_ENDPOINT` and `QUICKWIT_INDEX` in the environment when running workers if you want indexing jobs to run. The local compose file starts a Quickwit instance on `http://localhost:7280` and seeds the `documents` index automatically.
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
The backend reads its settings from environment variables (see `backend/.env` for local defaults). In particular:
|
||||||
|
|
||||||
|
- `DATABASE_URL` – connection string for the primary Postgres database (required).
|
||||||
|
- `DATABASE_MAX_POOL_SIZE` – optional override for the r2d2 connection pool size. Defaults to `2`; increase it in staging/production to match expected concurrency.
|
||||||
|
|
||||||
|
On startup each binary logs the effective configuration with secrets redacted (for example, the database password is masked). This makes it easier to confirm the runtime settings in staging without exposing credentials.
|
||||||
|
|
||||||
|
## Running Migrations in Kubernetes
|
||||||
|
|
||||||
|
The backend container image ships the `diesel` CLI, so schema migrations can be executed as a short-lived Job (or Helm hook) before rolling out new pods. Example manifest:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
apiVersion: batch/v1
|
||||||
|
kind: Job
|
||||||
|
metadata:
|
||||||
|
name: papercrate-migrate
|
||||||
|
spec:
|
||||||
|
template:
|
||||||
|
spec:
|
||||||
|
restartPolicy: OnFailure
|
||||||
|
containers:
|
||||||
|
- name: migrate
|
||||||
|
image: ghcr.io/example/papercrate-backend:<TAG>
|
||||||
|
command: ["/usr/local/bin/diesel", "migration", "run"]
|
||||||
|
env:
|
||||||
|
- name: DATABASE_URL
|
||||||
|
valueFrom:
|
||||||
|
secretKeyRef:
|
||||||
|
name: papercrate-db
|
||||||
|
key: DATABASE_URL
|
||||||
|
```
|
||||||
|
|
||||||
|
Run the Job manually (`kubectl apply -f migrate-job.yaml`) or configure it as a Helm pre-install/pre-upgrade hook so migrations run automatically on each deployment. Once the Job succeeds, deploy/update the backend `Deployment` as usual.
|
||||||
|
|||||||
Generated
+382
-65
@@ -67,7 +67,7 @@ checksum = "9035ad2d096bed7955a320ee7e2230574d28fd3c3a0f186cbea1ff3c7eed5dbb"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -593,6 +593,52 @@ dependencies = [
|
|||||||
"tower-service",
|
"tower-service",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "backend"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"argon2",
|
||||||
|
"async-trait",
|
||||||
|
"aws-config",
|
||||||
|
"aws-credential-types",
|
||||||
|
"aws-sdk-s3",
|
||||||
|
"axum",
|
||||||
|
"axum-extra",
|
||||||
|
"base64 0.21.7",
|
||||||
|
"bytes",
|
||||||
|
"chrono",
|
||||||
|
"diesel",
|
||||||
|
"diesel_migrations",
|
||||||
|
"dotenv",
|
||||||
|
"futures-util",
|
||||||
|
"hex",
|
||||||
|
"http-body-util",
|
||||||
|
"hyper 1.7.0",
|
||||||
|
"image",
|
||||||
|
"jsonwebtoken",
|
||||||
|
"mime_guess",
|
||||||
|
"once_cell",
|
||||||
|
"pdfium-render",
|
||||||
|
"percent-encoding",
|
||||||
|
"quick-xml",
|
||||||
|
"rand 0.8.5",
|
||||||
|
"reqwest",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"sha2",
|
||||||
|
"tempfile",
|
||||||
|
"thiserror 1.0.69",
|
||||||
|
"tokio",
|
||||||
|
"tower 0.4.13",
|
||||||
|
"tower-http",
|
||||||
|
"tracing",
|
||||||
|
"tracing-subscriber",
|
||||||
|
"url",
|
||||||
|
"utoipa",
|
||||||
|
"uuid",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "backtrace"
|
name = "backtrace"
|
||||||
version = "0.3.76"
|
version = "0.3.76"
|
||||||
@@ -659,7 +705,7 @@ dependencies = [
|
|||||||
"regex",
|
"regex",
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"shlex",
|
"shlex",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -753,6 +799,12 @@ version = "1.0.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
|
checksum = "2fd1289c04a9ea8cb22300a459a72a385d7c73d3259e2ed7dcb2af674838cfa9"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cfg_aliases"
|
||||||
|
version = "0.2.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "chrono"
|
name = "chrono"
|
||||||
version = "0.4.42"
|
version = "0.4.42"
|
||||||
@@ -938,7 +990,7 @@ dependencies = [
|
|||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"strsim",
|
"strsim",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -949,7 +1001,7 @@ checksum = "d38308df82d1080de0afee5d069fa14b0326a88c14f15c5ccda35b4a6c414c81"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"darling_core",
|
"darling_core",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -999,7 +1051,7 @@ dependencies = [
|
|||||||
"dsl_auto_type",
|
"dsl_auto_type",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1019,7 +1071,7 @@ version = "0.3.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "fe2444076b48641147115697648dc743c2c00b61adade0f01ce67133c7babe8c"
|
checksum = "fe2444076b48641147115697648dc743c2c00b61adade0f01ce67133c7babe8c"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1041,7 +1093,7 @@ checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1067,7 +1119,7 @@ dependencies = [
|
|||||||
"heck",
|
"heck",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1129,6 +1181,16 @@ version = "1.0.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "errno"
|
||||||
|
version = "0.3.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "fastrand"
|
name = "fastrand"
|
||||||
version = "2.3.0"
|
version = "2.3.0"
|
||||||
@@ -1212,6 +1274,23 @@ version = "0.3.31"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
checksum = "05f29059c0c2090612e8d742178b0580d2dc940c837851ad723096f87af6663e"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-io"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9e5c1b78ca4aae1ac06c48a526a655760685149f0d465d21f37abfe57ce075c6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "futures-macro"
|
||||||
|
version = "0.3.31"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "162ee34ebcb7c64a8abebc059ce0fee27c2262618d7b60ed8faf72fef13c3650"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.106",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "futures-sink"
|
name = "futures-sink"
|
||||||
version = "0.3.31"
|
version = "0.3.31"
|
||||||
@@ -1231,9 +1310,14 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
checksum = "9fa08315bb612088cc391249efdc3bc77536f16c91f6cf495e6fbe85b20a4a81"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"futures-core",
|
"futures-core",
|
||||||
|
"futures-io",
|
||||||
|
"futures-macro",
|
||||||
|
"futures-sink",
|
||||||
"futures-task",
|
"futures-task",
|
||||||
|
"memchr",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"pin-utils",
|
"pin-utils",
|
||||||
|
"slab",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1266,9 +1350,11 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
checksum = "26145e563e54f2cadc477553f1ec5ee650b00862f0a58bcd12cbdc5f0ea2d2f4"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
|
"js-sys",
|
||||||
"libc",
|
"libc",
|
||||||
"r-efi",
|
"r-efi",
|
||||||
"wasi 0.14.7+wasi-0.2.4",
|
"wasi 0.14.7+wasi-0.2.4",
|
||||||
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1540,6 +1626,7 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
"tokio-rustls 0.26.4",
|
"tokio-rustls 0.26.4",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
|
"webpki-roots",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1726,6 +1813,8 @@ checksum = "4b0f83760fb341a774ed326568e19f5a863af4a952def8c39f9ab92fd95b88e5"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"equivalent",
|
"equivalent",
|
||||||
"hashbrown 0.16.0",
|
"hashbrown 0.16.0",
|
||||||
|
"serde",
|
||||||
|
"serde_core",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -1745,6 +1834,16 @@ version = "2.11.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
|
checksum = "469fb0b9cefa57e3ef31275ee7cacb78f2fdca44e4765491884a2b119d4eb130"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "iri-string"
|
||||||
|
version = "0.7.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dbc5ebe9c3a1a7a5127f920a418f7585e9e758e911d0466ed004f393b0e380b2"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.13.0"
|
version = "0.13.0"
|
||||||
@@ -1817,6 +1916,12 @@ dependencies = [
|
|||||||
"windows-targets 0.53.5",
|
"windows-targets 0.53.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linux-raw-sys"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "df1d3c3b53da64cf5760482273a98e575c651a67eec7f77df96b5b642de8f039"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "litemap"
|
name = "litemap"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
@@ -1847,6 +1952,12 @@ dependencies = [
|
|||||||
"hashbrown 0.15.5",
|
"hashbrown 0.15.5",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lru-slab"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "112b39cec0b298b6c1999fee3e31427f74f676e4cb9879ed1a121b43661a4154"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "matchers"
|
name = "matchers"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
@@ -2066,44 +2177,6 @@ dependencies = [
|
|||||||
"sha2",
|
"sha2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "paperless-backend"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"anyhow",
|
|
||||||
"argon2",
|
|
||||||
"async-trait",
|
|
||||||
"aws-config",
|
|
||||||
"aws-credential-types",
|
|
||||||
"aws-sdk-s3",
|
|
||||||
"axum",
|
|
||||||
"axum-extra",
|
|
||||||
"bytes",
|
|
||||||
"chrono",
|
|
||||||
"diesel",
|
|
||||||
"diesel_migrations",
|
|
||||||
"dotenv",
|
|
||||||
"hex",
|
|
||||||
"http-body-util",
|
|
||||||
"hyper 1.7.0",
|
|
||||||
"image",
|
|
||||||
"jsonwebtoken",
|
|
||||||
"mime_guess",
|
|
||||||
"once_cell",
|
|
||||||
"pdfium-render",
|
|
||||||
"rand 0.8.5",
|
|
||||||
"serde",
|
|
||||||
"serde_json",
|
|
||||||
"sha2",
|
|
||||||
"thiserror 1.0.69",
|
|
||||||
"tokio",
|
|
||||||
"tower 0.4.13",
|
|
||||||
"tower-http",
|
|
||||||
"tracing",
|
|
||||||
"tracing-subscriber",
|
|
||||||
"uuid",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parking_lot"
|
name = "parking_lot"
|
||||||
version = "0.12.5"
|
version = "0.12.5"
|
||||||
@@ -2197,7 +2270,7 @@ checksum = "6e918e4ff8c4549eb882f14b3a4bc8c8bc93de829416eacf579f1207a8fbf861"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2289,7 +2362,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
|
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-error"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro-error-attr",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 1.0.109",
|
||||||
|
"version_check",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "proc-macro-error-attr"
|
||||||
|
version = "1.0.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2310,6 +2407,70 @@ dependencies = [
|
|||||||
"num-traits",
|
"num-traits",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quick-xml"
|
||||||
|
version = "0.32.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1d3a6e5838b60e0e8fa7a43f22ade549a37d61f8bdbe636d0d7816191de969c2"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quinn"
|
||||||
|
version = "0.11.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "b9e20a958963c291dc322d98411f541009df2ced7b5a4f2bd52337638cfccf20"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"cfg_aliases",
|
||||||
|
"pin-project-lite",
|
||||||
|
"quinn-proto",
|
||||||
|
"quinn-udp",
|
||||||
|
"rustc-hash",
|
||||||
|
"rustls 0.23.32",
|
||||||
|
"socket2 0.6.0",
|
||||||
|
"thiserror 2.0.17",
|
||||||
|
"tokio",
|
||||||
|
"tracing",
|
||||||
|
"web-time",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quinn-proto"
|
||||||
|
version = "0.11.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f1906b49b0c3bc04b5fe5d86a77925ae6524a19b816ae38ce1e426255f1d8a31"
|
||||||
|
dependencies = [
|
||||||
|
"bytes",
|
||||||
|
"getrandom 0.3.3",
|
||||||
|
"lru-slab",
|
||||||
|
"rand 0.9.2",
|
||||||
|
"ring",
|
||||||
|
"rustc-hash",
|
||||||
|
"rustls 0.23.32",
|
||||||
|
"rustls-pki-types",
|
||||||
|
"slab",
|
||||||
|
"thiserror 2.0.17",
|
||||||
|
"tinyvec",
|
||||||
|
"tracing",
|
||||||
|
"web-time",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "quinn-udp"
|
||||||
|
version = "0.5.14"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "addec6a0dcad8a8d96a771f815f0eaf55f9d1805756410b39f5fa81332574cbd"
|
||||||
|
dependencies = [
|
||||||
|
"cfg_aliases",
|
||||||
|
"libc",
|
||||||
|
"once_cell",
|
||||||
|
"socket2 0.6.0",
|
||||||
|
"tracing",
|
||||||
|
"windows-sys 0.59.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.41"
|
version = "1.0.41"
|
||||||
@@ -2439,6 +2600,47 @@ version = "0.8.6"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001"
|
checksum = "caf4aa5b0f434c91fe5c7f1ecb6a5ece2130b02ad2a590589dda5146df959001"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "reqwest"
|
||||||
|
version = "0.12.23"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d429f34c8092b2d42c7c93cec323bb4adeb7c67698f70839adec842ec10c7ceb"
|
||||||
|
dependencies = [
|
||||||
|
"base64 0.22.1",
|
||||||
|
"bytes",
|
||||||
|
"futures-core",
|
||||||
|
"futures-util",
|
||||||
|
"http 1.3.1",
|
||||||
|
"http-body 1.0.1",
|
||||||
|
"http-body-util",
|
||||||
|
"hyper 1.7.0",
|
||||||
|
"hyper-rustls 0.27.7",
|
||||||
|
"hyper-util",
|
||||||
|
"js-sys",
|
||||||
|
"log",
|
||||||
|
"percent-encoding",
|
||||||
|
"pin-project-lite",
|
||||||
|
"quinn",
|
||||||
|
"rustls 0.23.32",
|
||||||
|
"rustls-pki-types",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"serde_urlencoded",
|
||||||
|
"sync_wrapper",
|
||||||
|
"tokio",
|
||||||
|
"tokio-rustls 0.26.4",
|
||||||
|
"tokio-util",
|
||||||
|
"tower 0.5.2",
|
||||||
|
"tower-http",
|
||||||
|
"tower-service",
|
||||||
|
"url",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"wasm-bindgen-futures",
|
||||||
|
"wasm-streams",
|
||||||
|
"web-sys",
|
||||||
|
"webpki-roots",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rfc6979"
|
name = "rfc6979"
|
||||||
version = "0.3.1"
|
version = "0.3.1"
|
||||||
@@ -2485,6 +2687,19 @@ dependencies = [
|
|||||||
"semver",
|
"semver",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustix"
|
||||||
|
version = "1.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cd15f8a2c5551a84d56efdc1cd049089e409ac19a3072d5037a17fd70719ff3e"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"errno",
|
||||||
|
"libc",
|
||||||
|
"linux-raw-sys",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustls"
|
name = "rustls"
|
||||||
version = "0.21.12"
|
version = "0.21.12"
|
||||||
@@ -2505,6 +2720,7 @@ checksum = "cd3c25631629d034ce7cd9940adc9d45762d46de2b0f57193c4443b92c6d4d40"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"aws-lc-rs",
|
"aws-lc-rs",
|
||||||
"once_cell",
|
"once_cell",
|
||||||
|
"ring",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
"rustls-webpki 0.103.7",
|
"rustls-webpki 0.103.7",
|
||||||
"subtle",
|
"subtle",
|
||||||
@@ -2550,6 +2766,7 @@ version = "1.12.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "229a4a4c221013e7e1f1a043678c5cc39fe5171437c88fb47151a21e6f5b5c79"
|
checksum = "229a4a4c221013e7e1f1a043678c5cc39fe5171437c88fb47151a21e6f5b5c79"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"web-time",
|
||||||
"zeroize",
|
"zeroize",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -2704,7 +2921,7 @@ checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2892,6 +3109,16 @@ version = "2.6.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "syn"
|
||||||
|
version = "1.0.109"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.106"
|
version = "2.0.106"
|
||||||
@@ -2908,6 +3135,9 @@ name = "sync_wrapper"
|
|||||||
version = "1.0.2"
|
version = "1.0.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263"
|
||||||
|
dependencies = [
|
||||||
|
"futures-core",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "synstructure"
|
name = "synstructure"
|
||||||
@@ -2917,7 +3147,20 @@ checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tempfile"
|
||||||
|
version = "3.23.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2d31c77bdf42a745371d260a26ca7163f1e0924b64afa0b688e61b5a9fa02f16"
|
||||||
|
dependencies = [
|
||||||
|
"fastrand",
|
||||||
|
"getrandom 0.3.3",
|
||||||
|
"once_cell",
|
||||||
|
"rustix",
|
||||||
|
"windows-sys 0.61.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2946,7 +3189,7 @@ checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -2957,7 +3200,7 @@ checksum = "3ff15c8ecd7de3849db632e14d18d2571fa09dfc5ed93479bc4485c7a517c913"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3010,6 +3253,21 @@ dependencies = [
|
|||||||
"zerovec",
|
"zerovec",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tinyvec"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bfa5fdc3bce6191a1dbc8c02d5c8bffcf557bafa17c124c5264a458f1b0613fa"
|
||||||
|
dependencies = [
|
||||||
|
"tinyvec_macros",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tinyvec_macros"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tokio"
|
name = "tokio"
|
||||||
version = "1.47.1"
|
version = "1.47.1"
|
||||||
@@ -3038,7 +3296,7 @@ checksum = "6e06d43f1345a3bcd39f6a56dbb7dcab2ba47e68e8ac134855e7e2bdbaf8cab8"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3139,16 +3397,18 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tower-http"
|
name = "tower-http"
|
||||||
version = "0.5.2"
|
version = "0.6.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5"
|
checksum = "adc82fd73de2a9722ac5da747f12383d2bfdb93591ee6c58486e0097890f05f2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags",
|
"bitflags",
|
||||||
"bytes",
|
"bytes",
|
||||||
|
"futures-util",
|
||||||
"http 1.3.1",
|
"http 1.3.1",
|
||||||
"http-body 1.0.1",
|
"http-body 1.0.1",
|
||||||
"http-body-util",
|
"iri-string",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
|
"tower 0.5.2",
|
||||||
"tower-layer",
|
"tower-layer",
|
||||||
"tower-service",
|
"tower-service",
|
||||||
"tracing",
|
"tracing",
|
||||||
@@ -3186,7 +3446,7 @@ checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3291,6 +3551,31 @@ version = "1.0.4"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utoipa"
|
||||||
|
version = "4.2.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c5afb1a60e207dca502682537fefcfd9921e71d0b83e9576060f09abc6efab23"
|
||||||
|
dependencies = [
|
||||||
|
"indexmap",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"utoipa-gen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "utoipa-gen"
|
||||||
|
version = "4.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "20c24e8ab68ff9ee746aad22d39b5535601e6416d1b0feeabf78be986a5c4392"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro-error",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn 2.0.106",
|
||||||
|
"uuid",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uuid"
|
name = "uuid"
|
||||||
version = "1.18.1"
|
version = "1.18.1"
|
||||||
@@ -3392,7 +3677,7 @@ dependencies = [
|
|||||||
"log",
|
"log",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3427,7 +3712,7 @@ checksum = "9f07d2f20d4da7b26400c9f4a0511e6e0345b040694e8a75bd41d578fa4421d7"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
"wasm-bindgen-backend",
|
"wasm-bindgen-backend",
|
||||||
"wasm-bindgen-shared",
|
"wasm-bindgen-shared",
|
||||||
]
|
]
|
||||||
@@ -3441,6 +3726,19 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "wasm-streams"
|
||||||
|
version = "0.4.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "15053d8d85c7eccdbefef60f06769760a563c7f0a9d6902a13d35c7800b0ad65"
|
||||||
|
dependencies = [
|
||||||
|
"futures-util",
|
||||||
|
"js-sys",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"wasm-bindgen-futures",
|
||||||
|
"web-sys",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "web-sys"
|
name = "web-sys"
|
||||||
version = "0.3.81"
|
version = "0.3.81"
|
||||||
@@ -3451,6 +3749,25 @@ dependencies = [
|
|||||||
"wasm-bindgen",
|
"wasm-bindgen",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "web-time"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a6580f308b1fad9207618087a65c04e7a10bc77e02c8e84e9b00dd4b12fa0bb"
|
||||||
|
dependencies = [
|
||||||
|
"js-sys",
|
||||||
|
"wasm-bindgen",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "webpki-roots"
|
||||||
|
version = "1.0.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "32b130c0d2d49f8b6889abc456e795e82525204f27c42cf767cf0d7734e089b8"
|
||||||
|
dependencies = [
|
||||||
|
"rustls-pki-types",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-core"
|
name = "windows-core"
|
||||||
version = "0.62.2"
|
version = "0.62.2"
|
||||||
@@ -3472,7 +3789,7 @@ checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3483,7 +3800,7 @@ checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3710,7 +4027,7 @@ checksum = "38da3c9736e16c5d3c8c597a9aaa5d1fa565d0532ae05e27c24aa62fb32c0ab6"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
"synstructure",
|
"synstructure",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3731,7 +4048,7 @@ checksum = "88d2b8d9c68ad2b9e4340d7832716a4d21a22a1154777ad56ea55c51a9cf3831"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -3751,7 +4068,7 @@ checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
"synstructure",
|
"synstructure",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -3791,7 +4108,7 @@ checksum = "5b96237efa0c878c64bd89c436f661be4e46b2f3eff1ebb976f7ef2321d2f58f"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
"syn",
|
"syn 2.0.106",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
+11
-2
@@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "paperless-backend"
|
name = "backend"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ edition = "2021"
|
|||||||
axum = { version = "0.7", features = ["multipart"] }
|
axum = { version = "0.7", features = ["multipart"] }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
tower = { version = "0.4", features = ["make", "util"] }
|
tower = { version = "0.4", features = ["make", "util"] }
|
||||||
tower-http = { version = "0.5", features = ["cors", "trace"] }
|
tower-http = { version = "0.6", features = ["cors", "trace"] }
|
||||||
axum-extra = { version = "0.9", features = ["typed-header"] }
|
axum-extra = { version = "0.9", features = ["typed-header"] }
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
@@ -37,6 +37,15 @@ async-trait = "0.1"
|
|||||||
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
|
image = { version = "0.25", default-features = false, features = ["png", "jpeg"] }
|
||||||
pdfium-render = "0.8"
|
pdfium-render = "0.8"
|
||||||
mime_guess = "2.0"
|
mime_guess = "2.0"
|
||||||
|
tempfile = "3.10"
|
||||||
|
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls", "stream"] }
|
||||||
|
percent-encoding = "2.3"
|
||||||
|
base64 = "0.21"
|
||||||
|
quick-xml = "0.32"
|
||||||
|
futures-util = "0.3"
|
||||||
|
url = "2.5"
|
||||||
|
once_cell = "1.19"
|
||||||
|
utoipa = { version = "4.2", default-features = false, features = ["chrono", "uuid", "preserve_order"] }
|
||||||
|
|
||||||
# Error handling
|
# Error handling
|
||||||
thiserror = "1.0"
|
thiserror = "1.0"
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
# syntax=docker/dockerfile:1
|
||||||
|
|
||||||
|
FROM rust:1-slim AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends \
|
||||||
|
build-essential \
|
||||||
|
pkg-config \
|
||||||
|
libssl-dev \
|
||||||
|
libpq-dev \
|
||||||
|
libjpeg-dev \
|
||||||
|
libpng-dev \
|
||||||
|
curl \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
COPY Cargo.toml Cargo.lock ./
|
||||||
|
COPY src ./src
|
||||||
|
COPY migrations ./migrations
|
||||||
|
COPY tests ./tests
|
||||||
|
COPY diesel.toml ./
|
||||||
|
|
||||||
|
RUN cargo build --release --bin backend --bin worker --bin webdav --bin admin
|
||||||
|
RUN cargo install diesel_cli --no-default-features --features postgres
|
||||||
|
|
||||||
|
FROM debian:trixie-slim AS runtime
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y --no-install-recommends \
|
||||||
|
ca-certificates \
|
||||||
|
curl \
|
||||||
|
libssl3 \
|
||||||
|
libpq5 \
|
||||||
|
libjpeg62-turbo \
|
||||||
|
libpng16-16 \
|
||||||
|
ocrmypdf \
|
||||||
|
tesseract-ocr \
|
||||||
|
ghostscript \
|
||||||
|
qpdf \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* \
|
||||||
|
&& mkdir -p /usr/local/lib \
|
||||||
|
&& curl -fsSL https://github.com/bblanchon/pdfium-binaries/releases/latest/download/pdfium-linux-arm64.tgz -o /tmp/pdfium.tgz \
|
||||||
|
&& mkdir -p /tmp/pdfium \
|
||||||
|
&& tar -xzf /tmp/pdfium.tgz -C /tmp/pdfium --strip-components=1 \
|
||||||
|
&& pdfium_so="$(find /tmp/pdfium -name libpdfium.so -type f | head -n1)" \
|
||||||
|
&& [ -n "${pdfium_so}" ] \
|
||||||
|
&& mv "${pdfium_so}" /usr/local/lib/libpdfium.so \
|
||||||
|
&& ldconfig \
|
||||||
|
&& rm -rf /tmp/pdfium.tgz /tmp/pdfium \
|
||||||
|
&& useradd --system --create-home --uid 10001 appuser
|
||||||
|
|
||||||
|
COPY --from=builder /app/target/release/backend /usr/local/bin/papercrate-backend
|
||||||
|
COPY --from=builder /app/target/release/worker /usr/local/bin/papercrate-worker
|
||||||
|
COPY --from=builder /app/target/release/webdav /usr/local/bin/papercrate-webdav
|
||||||
|
COPY --from=builder /app/target/release/admin /usr/local/bin/papercrate-admin
|
||||||
|
COPY --from=builder /usr/local/cargo/bin/diesel /usr/local/bin/diesel
|
||||||
|
COPY migrations ./migrations
|
||||||
|
COPY diesel.toml ./
|
||||||
|
|
||||||
|
ENV RUST_LOG=info
|
||||||
|
USER appuser
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
ENTRYPOINT ["/usr/local/bin/papercrate-backend"]
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
use argon2::{
|
|
||||||
password_hash::{PasswordHasher, SaltString},
|
|
||||||
Argon2,
|
|
||||||
};
|
|
||||||
use rand::thread_rng;
|
|
||||||
use std::env;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
let password = env::args()
|
|
||||||
.nth(1)
|
|
||||||
.expect("Usage: cargo run --example hash_password <password>");
|
|
||||||
let salt = SaltString::generate(&mut thread_rng());
|
|
||||||
let argon2 = Argon2::default();
|
|
||||||
let hash = argon2
|
|
||||||
.hash_password(password.as_bytes(), &salt)
|
|
||||||
.expect("hashing failed")
|
|
||||||
.to_string();
|
|
||||||
println!("{}", hash);
|
|
||||||
}
|
|
||||||
Binary file not shown.
@@ -1,11 +0,0 @@
|
|||||||
DROP INDEX IF EXISTS idx_document_tags_tag;
|
|
||||||
DROP TABLE IF EXISTS document_tags;
|
|
||||||
DROP INDEX IF EXISTS idx_document_versions_document;
|
|
||||||
DROP TABLE IF EXISTS document_versions;
|
|
||||||
DROP INDEX IF EXISTS idx_documents_deleted_at;
|
|
||||||
DROP INDEX IF EXISTS idx_documents_folder;
|
|
||||||
DROP TABLE IF EXISTS documents;
|
|
||||||
DROP INDEX IF EXISTS idx_folders_parent;
|
|
||||||
DROP TABLE IF EXISTS folders;
|
|
||||||
DROP TABLE IF EXISTS tags;
|
|
||||||
DROP TABLE IF EXISTS users;
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
|
||||||
|
|
||||||
CREATE TABLE users (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
username VARCHAR(100) NOT NULL UNIQUE,
|
|
||||||
password_hash VARCHAR(255) NOT NULL,
|
|
||||||
role VARCHAR(16) NOT NULL,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE folders (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
name VARCHAR(255) NOT NULL,
|
|
||||||
parent_id UUID REFERENCES folders(id) ON DELETE SET NULL,
|
|
||||||
path_cache VARCHAR(1000),
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
CONSTRAINT folders_parent_name_unique UNIQUE (parent_id, name)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idx_folders_parent ON folders(parent_id);
|
|
||||||
|
|
||||||
CREATE TABLE documents (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
filename VARCHAR(255) NOT NULL,
|
|
||||||
original_name VARCHAR(255) NOT NULL,
|
|
||||||
content_type VARCHAR(100),
|
|
||||||
folder_id UUID REFERENCES folders(id) ON DELETE SET NULL,
|
|
||||||
current_version INTEGER NOT NULL,
|
|
||||||
uploaded_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
deleted_at TIMESTAMPTZ,
|
|
||||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idx_documents_folder ON documents(folder_id);
|
|
||||||
CREATE INDEX idx_documents_deleted_at ON documents(deleted_at);
|
|
||||||
|
|
||||||
CREATE TABLE document_versions (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
|
||||||
version_number INTEGER NOT NULL,
|
|
||||||
s3_key VARCHAR(500) NOT NULL,
|
|
||||||
size_bytes BIGINT NOT NULL,
|
|
||||||
checksum VARCHAR(64) NOT NULL,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
operations_summary JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
||||||
CONSTRAINT document_versions_unique_version UNIQUE (document_id, version_number)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idx_document_versions_document ON document_versions(document_id);
|
|
||||||
|
|
||||||
CREATE TABLE tags (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
label VARCHAR(100) NOT NULL UNIQUE,
|
|
||||||
color VARCHAR(7),
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE document_tags (
|
|
||||||
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
|
||||||
tag_id UUID NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
|
|
||||||
assigned_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
assigned_by UUID REFERENCES users(id),
|
|
||||||
PRIMARY KEY (document_id, tag_id)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idx_document_tags_tag ON document_tags(tag_id);
|
|
||||||
|
|
||||||
INSERT INTO users (id, username, password_hash, role)
|
|
||||||
VALUES (
|
|
||||||
gen_random_uuid(),
|
|
||||||
'admin',
|
|
||||||
'$argon2id$v=19$m=19456,t=2,p=1$UMkfsNut028fmZupy9JoQg$/YFvGQoEZ2hhMiDCyv68ZROF97GcwAxxRwRgwSbpX5U',
|
|
||||||
'admin'
|
|
||||||
);
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
DROP TRIGGER IF EXISTS trg_jobs_updated_at ON jobs;
|
|
||||||
DROP FUNCTION IF EXISTS touch_jobs_updated_at;
|
|
||||||
DROP INDEX IF EXISTS idx_jobs_job_type;
|
|
||||||
DROP INDEX IF EXISTS idx_jobs_status_run_after;
|
|
||||||
DROP TABLE IF EXISTS jobs;
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
CREATE TABLE jobs (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
job_type TEXT NOT NULL,
|
|
||||||
payload JSONB NOT NULL,
|
|
||||||
status TEXT NOT NULL DEFAULT 'queued',
|
|
||||||
attempts INTEGER NOT NULL DEFAULT 0,
|
|
||||||
run_after TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
last_error TEXT,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
CONSTRAINT jobs_status_check CHECK (status IN ('queued', 'processing', 'succeeded', 'failed'))
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idx_jobs_status_run_after ON jobs (status, run_after);
|
|
||||||
CREATE INDEX idx_jobs_job_type ON jobs (job_type);
|
|
||||||
|
|
||||||
CREATE OR REPLACE FUNCTION touch_jobs_updated_at()
|
|
||||||
RETURNS TRIGGER AS $$
|
|
||||||
BEGIN
|
|
||||||
NEW.updated_at = now();
|
|
||||||
RETURN NEW;
|
|
||||||
END;
|
|
||||||
$$ LANGUAGE plpgsql;
|
|
||||||
|
|
||||||
CREATE TRIGGER trg_jobs_updated_at
|
|
||||||
BEFORE UPDATE ON jobs
|
|
||||||
FOR EACH ROW
|
|
||||||
EXECUTE FUNCTION touch_jobs_updated_at();
|
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
DROP INDEX IF EXISTS idx_document_assets_type;
|
|
||||||
DROP INDEX IF EXISTS idx_document_assets_version;
|
|
||||||
DROP TABLE IF EXISTS document_assets;
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
CREATE TABLE document_assets (
|
|
||||||
id UUID PRIMARY KEY,
|
|
||||||
document_version_id UUID NOT NULL REFERENCES document_versions(id) ON DELETE CASCADE,
|
|
||||||
asset_type TEXT NOT NULL,
|
|
||||||
s3_key TEXT NOT NULL,
|
|
||||||
mime_type TEXT NOT NULL,
|
|
||||||
width INTEGER,
|
|
||||||
height INTEGER,
|
|
||||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
||||||
CONSTRAINT document_assets_unique UNIQUE (document_version_id, asset_type)
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE INDEX idx_document_assets_version ON document_assets(document_version_id);
|
|
||||||
CREATE INDEX idx_document_assets_type ON document_assets(asset_type);
|
|
||||||
@@ -1,4 +0,0 @@
|
|||||||
DROP INDEX IF EXISTS folders_parent_name_unique_idx;
|
|
||||||
|
|
||||||
ALTER TABLE folders
|
|
||||||
ADD CONSTRAINT folders_parent_name_unique UNIQUE (parent_id, name);
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
ALTER TABLE folders
|
|
||||||
DROP CONSTRAINT IF EXISTS folders_parent_name_unique;
|
|
||||||
|
|
||||||
CREATE UNIQUE INDEX folders_parent_name_unique_idx
|
|
||||||
ON folders (COALESCE(parent_id, '00000000-0000-0000-0000-000000000000'::uuid), name);
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE documents
|
|
||||||
DROP COLUMN issued_at;
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE documents
|
|
||||||
ADD COLUMN issued_at TIMESTAMPTZ;
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE documents
|
|
||||||
DROP COLUMN name;
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
ALTER TABLE documents
|
|
||||||
ADD COLUMN name VARCHAR(255);
|
|
||||||
|
|
||||||
UPDATE documents
|
|
||||||
SET name = CASE
|
|
||||||
WHEN filename ~ '\\.[^./]+$' THEN regexp_replace(filename, '\\.[^./]+$', '')
|
|
||||||
ELSE filename
|
|
||||||
END;
|
|
||||||
|
|
||||||
ALTER TABLE documents
|
|
||||||
ALTER COLUMN name SET NOT NULL;
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE documents
|
|
||||||
RENAME COLUMN title TO name;
|
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
ALTER TABLE documents
|
|
||||||
RENAME COLUMN name TO title;
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
DROP TRIGGER IF EXISTS trg_jobs_updated_at ON jobs;
|
||||||
|
DROP FUNCTION IF EXISTS touch_jobs_updated_at();
|
||||||
|
|
||||||
|
ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_current_version_fk;
|
||||||
|
|
||||||
|
DROP TABLE IF EXISTS document_asset_objects;
|
||||||
|
DROP TABLE IF EXISTS document_assets;
|
||||||
|
DROP TABLE IF EXISTS document_versions;
|
||||||
|
DROP TABLE IF EXISTS document_tags;
|
||||||
|
DROP TABLE IF EXISTS document_correspondents;
|
||||||
|
DROP TABLE IF EXISTS correspondents;
|
||||||
|
DROP TABLE IF EXISTS documents;
|
||||||
|
DROP TABLE IF EXISTS folders;
|
||||||
|
DROP TABLE IF EXISTS tags;
|
||||||
|
DROP TABLE IF EXISTS jobs;
|
||||||
|
DROP TABLE IF EXISTS refresh_tokens;
|
||||||
|
DROP TABLE IF EXISTS user_memberships;
|
||||||
|
DROP TABLE IF EXISTS users;
|
||||||
|
DROP TABLE IF EXISTS tenants;
|
||||||
|
|
||||||
|
DROP EXTENSION IF EXISTS "pgcrypto";
|
||||||
@@ -0,0 +1,244 @@
|
|||||||
|
CREATE EXTENSION IF NOT EXISTS "pgcrypto";
|
||||||
|
|
||||||
|
CREATE TABLE tenants (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
slug TEXT NOT NULL UNIQUE,
|
||||||
|
storage_root TEXT,
|
||||||
|
quickwit_index TEXT,
|
||||||
|
status TEXT NOT NULL DEFAULT 'active',
|
||||||
|
config JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX tenants_storage_root_unique
|
||||||
|
ON tenants (storage_root)
|
||||||
|
WHERE storage_root IS NOT NULL;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX tenants_quickwit_index_unique
|
||||||
|
ON tenants (quickwit_index)
|
||||||
|
WHERE quickwit_index IS NOT NULL;
|
||||||
|
|
||||||
|
CREATE TABLE users (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
username VARCHAR(100) NOT NULL UNIQUE,
|
||||||
|
password_hash VARCHAR(255) NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE TABLE user_memberships (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
||||||
|
role TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
UNIQUE (user_id, tenant_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX user_memberships_tenant_id_idx ON user_memberships (tenant_id);
|
||||||
|
CREATE INDEX user_memberships_user_id_idx ON user_memberships (user_id);
|
||||||
|
|
||||||
|
CREATE TABLE folders (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
parent_id UUID REFERENCES folders(id) ON DELETE SET NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_folders_parent ON folders(parent_id);
|
||||||
|
CREATE INDEX folders_tenant_id_idx ON folders(tenant_id);
|
||||||
|
CREATE UNIQUE INDEX folders_parent_name_unique_idx
|
||||||
|
ON folders (COALESCE(parent_id, '00000000-0000-0000-0000-000000000000'::uuid), name);
|
||||||
|
|
||||||
|
CREATE TABLE documents (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
filename VARCHAR(255) NOT NULL,
|
||||||
|
original_name VARCHAR(255) NOT NULL,
|
||||||
|
content_type VARCHAR(100),
|
||||||
|
folder_id UUID REFERENCES folders(id) ON DELETE SET NULL,
|
||||||
|
uploaded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
deleted_at TIMESTAMPTZ,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
issued_at TIMESTAMPTZ,
|
||||||
|
title VARCHAR(255) NOT NULL,
|
||||||
|
current_version_id UUID NOT NULL,
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_documents_folder ON documents (folder_id);
|
||||||
|
CREATE INDEX idx_documents_deleted_at ON documents (deleted_at);
|
||||||
|
CREATE INDEX documents_tenant_id_idx ON documents (tenant_id);
|
||||||
|
CREATE INDEX idx_documents_current_version_id ON documents (current_version_id);
|
||||||
|
|
||||||
|
CREATE INDEX idx_documents_folder_title
|
||||||
|
ON documents (
|
||||||
|
COALESCE(folder_id, '00000000-0000-0000-0000-000000000000'::uuid),
|
||||||
|
title
|
||||||
|
)
|
||||||
|
WHERE deleted_at IS NULL;
|
||||||
|
|
||||||
|
CREATE UNIQUE INDEX documents_unique_folder_filename
|
||||||
|
ON documents (
|
||||||
|
COALESCE(folder_id, '00000000-0000-0000-0000-000000000000'::uuid),
|
||||||
|
filename
|
||||||
|
)
|
||||||
|
WHERE deleted_at IS NULL;
|
||||||
|
|
||||||
|
CREATE TABLE document_versions (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
||||||
|
version_number INT NOT NULL,
|
||||||
|
s3_key VARCHAR(500) NOT NULL,
|
||||||
|
size_bytes BIGINT NOT NULL,
|
||||||
|
checksum VARCHAR(64) NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
operations_summary JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
CONSTRAINT document_versions_unique_version UNIQUE (document_id, version_number)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_document_versions_document ON document_versions (document_id);
|
||||||
|
CREATE INDEX document_versions_tenant_id_idx ON document_versions (tenant_id);
|
||||||
|
|
||||||
|
ALTER TABLE documents
|
||||||
|
ADD CONSTRAINT documents_current_version_fk
|
||||||
|
FOREIGN KEY (current_version_id)
|
||||||
|
REFERENCES document_versions(id)
|
||||||
|
DEFERRABLE INITIALLY DEFERRED;
|
||||||
|
|
||||||
|
CREATE TABLE tags (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
label VARCHAR(100) NOT NULL UNIQUE,
|
||||||
|
color VARCHAR(7),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX tags_tenant_id_idx ON tags (tenant_id);
|
||||||
|
|
||||||
|
CREATE TABLE document_tags (
|
||||||
|
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
||||||
|
tag_id UUID NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
|
||||||
|
assigned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
assigned_by UUID REFERENCES users(id),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
PRIMARY KEY (document_id, tag_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_document_tags_tag ON document_tags (tag_id);
|
||||||
|
CREATE INDEX document_tags_tenant_id_idx ON document_tags (tenant_id);
|
||||||
|
|
||||||
|
CREATE TABLE correspondents (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
name VARCHAR(255) NOT NULL,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
CONSTRAINT correspondents_name_unique UNIQUE (name)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX correspondents_tenant_id_idx ON correspondents (tenant_id);
|
||||||
|
|
||||||
|
CREATE TABLE document_correspondents (
|
||||||
|
document_id UUID NOT NULL REFERENCES documents(id) ON DELETE CASCADE,
|
||||||
|
correspondent_id UUID NOT NULL REFERENCES correspondents(id) ON DELETE CASCADE,
|
||||||
|
role VARCHAR(32) NOT NULL,
|
||||||
|
assigned_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
assigned_by UUID REFERENCES users(id),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
PRIMARY KEY (document_id, correspondent_id, role),
|
||||||
|
CONSTRAINT document_correspondents_role_check CHECK (role IN ('sender', 'receiver', 'other'))
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_document_correspondents_document ON document_correspondents (document_id);
|
||||||
|
CREATE INDEX idx_document_correspondents_correspondent ON document_correspondents (correspondent_id);
|
||||||
|
CREATE INDEX idx_document_correspondents_role ON document_correspondents (role);
|
||||||
|
CREATE INDEX document_correspondents_tenant_id_idx ON document_correspondents (tenant_id);
|
||||||
|
|
||||||
|
CREATE TABLE document_assets (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
document_version_id UUID NOT NULL REFERENCES document_versions(id) ON DELETE CASCADE,
|
||||||
|
asset_type TEXT NOT NULL,
|
||||||
|
mime_type TEXT NOT NULL,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
cardinality INTEGER,
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
CONSTRAINT document_assets_unique UNIQUE (document_version_id, asset_type),
|
||||||
|
CONSTRAINT document_assets_cardinality_positive CHECK (cardinality IS NULL OR cardinality >= 1)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_document_assets_version ON document_assets (document_version_id);
|
||||||
|
CREATE INDEX idx_document_assets_type ON document_assets (asset_type);
|
||||||
|
CREATE INDEX document_assets_tenant_id_idx ON document_assets (tenant_id);
|
||||||
|
|
||||||
|
CREATE TABLE document_asset_objects (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
asset_id UUID NOT NULL REFERENCES document_assets(id) ON DELETE CASCADE,
|
||||||
|
ordinal INTEGER NOT NULL,
|
||||||
|
s3_key TEXT NOT NULL,
|
||||||
|
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
CONSTRAINT document_asset_objects_ordinal_positive CHECK (ordinal >= 1),
|
||||||
|
CONSTRAINT document_asset_objects_asset_ordinal_unique UNIQUE (asset_id, ordinal)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_document_asset_objects_asset_ordinal
|
||||||
|
ON document_asset_objects (asset_id, ordinal);
|
||||||
|
|
||||||
|
CREATE INDEX document_asset_objects_tenant_id_idx
|
||||||
|
ON document_asset_objects (tenant_id);
|
||||||
|
|
||||||
|
CREATE TABLE jobs (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
job_type TEXT NOT NULL,
|
||||||
|
payload JSONB NOT NULL,
|
||||||
|
status TEXT NOT NULL DEFAULT 'queued',
|
||||||
|
attempts INTEGER NOT NULL DEFAULT 0,
|
||||||
|
run_after TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
last_error TEXT,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id),
|
||||||
|
CONSTRAINT jobs_status_check CHECK (status IN ('queued', 'processing', 'succeeded', 'failed'))
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_jobs_status_run_after ON jobs (status, run_after);
|
||||||
|
CREATE INDEX idx_jobs_job_type ON jobs (job_type);
|
||||||
|
CREATE INDEX jobs_tenant_id_idx ON jobs (tenant_id);
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION touch_jobs_updated_at()
|
||||||
|
RETURNS TRIGGER AS $$
|
||||||
|
BEGIN
|
||||||
|
NEW.updated_at = NOW();
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE TRIGGER trg_jobs_updated_at
|
||||||
|
BEFORE UPDATE ON jobs
|
||||||
|
FOR EACH ROW
|
||||||
|
EXECUTE FUNCTION touch_jobs_updated_at();
|
||||||
|
|
||||||
|
CREATE TABLE refresh_tokens (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
token_hash TEXT NOT NULL,
|
||||||
|
issued_at TIMESTAMPTZ NOT NULL,
|
||||||
|
expires_at TIMESTAMPTZ NOT NULL,
|
||||||
|
revoked_at TIMESTAMPTZ,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
tenant_id UUID NOT NULL REFERENCES tenants(id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX idx_refresh_tokens_user_id ON refresh_tokens (user_id);
|
||||||
|
CREATE INDEX idx_refresh_tokens_token_hash ON refresh_tokens (token_hash);
|
||||||
|
CREATE INDEX refresh_tokens_tenant_id_idx ON refresh_tokens (tenant_id);
|
||||||
+82
-3
@@ -13,6 +13,10 @@ pub struct JwtService {
|
|||||||
issuer: String,
|
issuer: String,
|
||||||
audience: String,
|
audience: String,
|
||||||
expiry: Duration,
|
expiry: Duration,
|
||||||
|
download_audience: String,
|
||||||
|
download_expiry: Duration,
|
||||||
|
selector_audience: String,
|
||||||
|
selector_expiry: Duration,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl JwtService {
|
impl JwtService {
|
||||||
@@ -23,16 +27,20 @@ impl JwtService {
|
|||||||
issuer: config.jwt_issuer.clone(),
|
issuer: config.jwt_issuer.clone(),
|
||||||
audience: config.jwt_audience.clone(),
|
audience: config.jwt_audience.clone(),
|
||||||
expiry: Duration::minutes(config.jwt_expiry_minutes),
|
expiry: Duration::minutes(config.jwt_expiry_minutes),
|
||||||
|
download_audience: config.download_token_audience.clone(),
|
||||||
|
download_expiry: Duration::minutes(config.download_token_expiry_minutes),
|
||||||
|
selector_audience: format!("{}:tenant-selector", config.jwt_audience),
|
||||||
|
selector_expiry: Duration::minutes(15),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn generate_token(&self, user_id: Uuid, username: &str, role: &str) -> Result<String> {
|
pub fn generate_token(&self, user_id: Uuid, tenant_id: Uuid, username: &str) -> Result<String> {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
let exp = now + self.expiry;
|
let exp = now + self.expiry;
|
||||||
let claims = Claims {
|
let claims = Claims {
|
||||||
sub: user_id,
|
sub: user_id,
|
||||||
|
tenant_id,
|
||||||
username: username.to_owned(),
|
username: username.to_owned(),
|
||||||
role: role.to_owned(),
|
|
||||||
iss: self.issuer.clone(),
|
iss: self.issuer.clone(),
|
||||||
aud: self.audience.clone(),
|
aud: self.audience.clone(),
|
||||||
iat: now.timestamp() as usize,
|
iat: now.timestamp() as usize,
|
||||||
@@ -49,13 +57,84 @@ impl JwtService {
|
|||||||
let data = decode::<Claims>(token, &self.decoding, &validation)?;
|
let data = decode::<Claims>(token, &self.decoding, &validation)?;
|
||||||
Ok(data.claims)
|
Ok(data.claims)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn generate_download_token(
|
||||||
|
&self,
|
||||||
|
document_id: Uuid,
|
||||||
|
user_id: Uuid,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
) -> Result<String> {
|
||||||
|
let now = Utc::now();
|
||||||
|
let exp = now + self.download_expiry;
|
||||||
|
let claims = DownloadClaims {
|
||||||
|
doc_id: document_id,
|
||||||
|
user_id,
|
||||||
|
tenant_id,
|
||||||
|
iss: self.issuer.clone(),
|
||||||
|
aud: self.download_audience.clone(),
|
||||||
|
iat: now.timestamp() as usize,
|
||||||
|
exp: exp.timestamp() as usize,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(encode(&Header::default(), &claims, &self.encoding)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn verify_download_token(&self, token: &str) -> Result<DownloadClaims> {
|
||||||
|
let mut validation = Validation::default();
|
||||||
|
validation.set_audience(&[self.download_audience.clone()]);
|
||||||
|
validation.set_issuer(&[self.issuer.clone()]);
|
||||||
|
let data = decode::<DownloadClaims>(token, &self.decoding, &validation)?;
|
||||||
|
Ok(data.claims)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn generate_tenant_selector_token(&self, user_id: Uuid) -> Result<String> {
|
||||||
|
let now = Utc::now();
|
||||||
|
let exp = now + self.selector_expiry;
|
||||||
|
let claims = TenantSelectionClaims {
|
||||||
|
sub: user_id,
|
||||||
|
iss: self.issuer.clone(),
|
||||||
|
aud: self.selector_audience.clone(),
|
||||||
|
iat: now.timestamp() as usize,
|
||||||
|
exp: exp.timestamp() as usize,
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(encode(&Header::default(), &claims, &self.encoding)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn verify_tenant_selector_token(&self, token: &str) -> Result<TenantSelectionClaims> {
|
||||||
|
let mut validation = Validation::default();
|
||||||
|
validation.set_audience(&[self.selector_audience.clone()]);
|
||||||
|
validation.set_issuer(&[self.issuer.clone()]);
|
||||||
|
let data = decode::<TenantSelectionClaims>(token, &self.decoding, &validation)?;
|
||||||
|
Ok(data.claims)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct Claims {
|
pub struct Claims {
|
||||||
pub sub: Uuid,
|
pub sub: Uuid,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub role: String,
|
pub iss: String,
|
||||||
|
pub aud: String,
|
||||||
|
pub iat: usize,
|
||||||
|
pub exp: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct DownloadClaims {
|
||||||
|
pub doc_id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
pub iss: String,
|
||||||
|
pub aud: String,
|
||||||
|
pub iat: usize,
|
||||||
|
pub exp: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct TenantSelectionClaims {
|
||||||
|
pub sub: Uuid,
|
||||||
pub iss: String,
|
pub iss: String,
|
||||||
pub aud: String,
|
pub aud: String,
|
||||||
pub iat: usize,
|
pub iat: usize,
|
||||||
|
|||||||
+50
-4
@@ -6,13 +6,17 @@ use axum_extra::headers::{authorization::Bearer, Authorization};
|
|||||||
use axum_extra::TypedHeader;
|
use axum_extra::TypedHeader;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::{error::AppError, state::AppState};
|
use crate::{
|
||||||
|
error::AppError,
|
||||||
|
state::{AppState, PgPooledConnection},
|
||||||
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
pub struct AuthenticatedUser {
|
pub struct AuthenticatedUser {
|
||||||
pub user_id: uuid::Uuid,
|
pub user_id: uuid::Uuid,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub role: String,
|
pub tenant_id: uuid::Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
@@ -23,6 +27,10 @@ impl FromRequestParts<AppState> for AuthenticatedUser {
|
|||||||
parts: &mut Parts,
|
parts: &mut Parts,
|
||||||
state: &AppState,
|
state: &AppState,
|
||||||
) -> Result<Self, Self::Rejection> {
|
) -> Result<Self, Self::Rejection> {
|
||||||
|
if let Some(user) = parts.extensions.get::<AuthenticatedUser>() {
|
||||||
|
return Ok(user.clone());
|
||||||
|
}
|
||||||
|
|
||||||
let TypedHeader(Authorization(bearer)) =
|
let TypedHeader(Authorization(bearer)) =
|
||||||
TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state)
|
TypedHeader::<Authorization<Bearer>>::from_request_parts(parts, state)
|
||||||
.await
|
.await
|
||||||
@@ -33,10 +41,48 @@ impl FromRequestParts<AppState> for AuthenticatedUser {
|
|||||||
.verify_token(bearer.token())
|
.verify_token(bearer.token())
|
||||||
.map_err(|_| AppError::unauthorized())?;
|
.map_err(|_| AppError::unauthorized())?;
|
||||||
|
|
||||||
Ok(AuthenticatedUser {
|
let user = AuthenticatedUser {
|
||||||
user_id: claims.sub,
|
user_id: claims.sub,
|
||||||
username: claims.username,
|
username: claims.username,
|
||||||
role: claims.role,
|
tenant_id: claims.tenant_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
parts.extensions.insert(user.clone());
|
||||||
|
|
||||||
|
Ok(user)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TenantScopedConn {
|
||||||
|
pub conn: PgPooledConnection,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub user: AuthenticatedUser,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TenantScopedConn {
|
||||||
|
pub fn conn(&mut self) -> &mut PgPooledConnection {
|
||||||
|
&mut self.conn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl FromRequestParts<AppState> for TenantScopedConn {
|
||||||
|
type Rejection = AppError;
|
||||||
|
|
||||||
|
async fn from_request_parts(
|
||||||
|
parts: &mut Parts,
|
||||||
|
state: &AppState,
|
||||||
|
) -> Result<Self, Self::Rejection> {
|
||||||
|
let user = AuthenticatedUser::from_request_parts(parts, state).await?;
|
||||||
|
let tenant_id = user.tenant_id;
|
||||||
|
let conn = state.db_for_tenant(tenant_id)?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
conn,
|
||||||
|
tenant_id,
|
||||||
|
user_id: user.user_id,
|
||||||
|
user,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,707 @@
|
|||||||
|
use std::env;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, bail, Context, Result};
|
||||||
|
use argon2::{
|
||||||
|
password_hash::{PasswordHasher, SaltString},
|
||||||
|
Argon2,
|
||||||
|
};
|
||||||
|
use diesel::{dsl::exists, prelude::*, select};
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use reqwest::{Client, Method, StatusCode};
|
||||||
|
use serde_json::json;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use backend::{
|
||||||
|
config::AppConfig,
|
||||||
|
db::{self, PgPool},
|
||||||
|
jobs::{enqueue_job, JOB_ANALYZE_DOCUMENT},
|
||||||
|
models::{DocumentAsset, DocumentAssetObject, NewUser, NewUserMembership, Tenant, User},
|
||||||
|
s3,
|
||||||
|
schema::{
|
||||||
|
document_asset_objects, document_assets, documents, tenants, user_memberships, users,
|
||||||
|
},
|
||||||
|
storage::{ObjectStorage, S3Storage, TenantStorage},
|
||||||
|
utils::tracing::init_tracing,
|
||||||
|
};
|
||||||
|
|
||||||
|
use rand::rngs::OsRng;
|
||||||
|
|
||||||
|
static QUICKWIT_INDEX_TEMPLATE: Lazy<serde_json::Value> = Lazy::new(|| {
|
||||||
|
json!({
|
||||||
|
"version": "0.8",
|
||||||
|
"index_id": "documents",
|
||||||
|
"doc_mapping": {
|
||||||
|
"tokenizers": [
|
||||||
|
{
|
||||||
|
"name": "substring",
|
||||||
|
"type": "ngram",
|
||||||
|
"min_gram": 2,
|
||||||
|
"max_gram": 20,
|
||||||
|
"prefix_only": false
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"field_mappings": [
|
||||||
|
{ "name": "tenant_id", "type": "text", "stored": true },
|
||||||
|
{ "name": "document_id", "type": "text", "stored": true },
|
||||||
|
{ "name": "version_id", "type": "text", "stored": true },
|
||||||
|
{ "name": "title", "type": "text", "tokenizer": "substring", "stored": true },
|
||||||
|
{ "name": "text", "type": "text", "tokenizer": "substring", "record": "position" }
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"search_settings": {
|
||||||
|
"default_search_fields": ["title", "text"]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum Command {
|
||||||
|
CreateUser {
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
},
|
||||||
|
SetPassword {
|
||||||
|
username: String,
|
||||||
|
password: String,
|
||||||
|
},
|
||||||
|
ListUsers,
|
||||||
|
DeleteUser {
|
||||||
|
username: String,
|
||||||
|
},
|
||||||
|
CreateTenant {
|
||||||
|
slug: String,
|
||||||
|
storage_root: Option<String>,
|
||||||
|
quickwit_index: Option<String>,
|
||||||
|
},
|
||||||
|
DeleteTenant {
|
||||||
|
slug: String,
|
||||||
|
},
|
||||||
|
AddUserToTenant {
|
||||||
|
username: String,
|
||||||
|
slug: String,
|
||||||
|
role: Option<String>,
|
||||||
|
},
|
||||||
|
RemoveUserFromTenant {
|
||||||
|
username: String,
|
||||||
|
slug: String,
|
||||||
|
},
|
||||||
|
ReanalyzeDocuments {
|
||||||
|
slug: String,
|
||||||
|
},
|
||||||
|
ListTenants,
|
||||||
|
DeleteAssets(String),
|
||||||
|
QuickwitCreate(String),
|
||||||
|
QuickwitDelete(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Command {
|
||||||
|
fn usage() -> &'static str {
|
||||||
|
"Usage: admin\n\
|
||||||
|
create-user <username> <password>\n\
|
||||||
|
set-password <username> <password>\n\
|
||||||
|
list-users\n\
|
||||||
|
delete-user <username>\n\
|
||||||
|
create-tenant <slug> [storage_root] [quickwit_index]\n\
|
||||||
|
delete-tenant <slug>\n\
|
||||||
|
add-user-to-tenant <username> <slug> [role]\n\
|
||||||
|
remove-user-from-tenant <username> <slug>\n\
|
||||||
|
reanalyze-documents <slug>\n\
|
||||||
|
list-tenants\n\
|
||||||
|
delete-assets <slug>\n\
|
||||||
|
quickwit-create-index <slug>\n\
|
||||||
|
quickwit-delete-index <slug>"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse() -> Result<Self> {
|
||||||
|
let mut args = env::args().skip(1);
|
||||||
|
match args.next().as_deref() {
|
||||||
|
Some("create-user") => Ok(Self::CreateUser {
|
||||||
|
username: args.next().ok_or_else(|| anyhow!("username required"))?,
|
||||||
|
password: args.next().ok_or_else(|| anyhow!("password required"))?,
|
||||||
|
}),
|
||||||
|
Some("set-password") => Ok(Self::SetPassword {
|
||||||
|
username: args.next().ok_or_else(|| anyhow!("username required"))?,
|
||||||
|
password: args.next().ok_or_else(|| anyhow!("password required"))?,
|
||||||
|
}),
|
||||||
|
Some("list-users") => Ok(Self::ListUsers),
|
||||||
|
Some("delete-user") => Ok(Self::DeleteUser {
|
||||||
|
username: args.next().ok_or_else(|| anyhow!("username required"))?,
|
||||||
|
}),
|
||||||
|
Some("create-tenant") => Ok(Self::CreateTenant {
|
||||||
|
slug: args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
storage_root: args.next(),
|
||||||
|
quickwit_index: args.next(),
|
||||||
|
}),
|
||||||
|
Some("delete-tenant") => Ok(Self::DeleteTenant {
|
||||||
|
slug: args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
}),
|
||||||
|
Some("add-user-to-tenant") => Ok(Self::AddUserToTenant {
|
||||||
|
username: args.next().ok_or_else(|| anyhow!("username required"))?,
|
||||||
|
slug: args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
role: args.next(),
|
||||||
|
}),
|
||||||
|
Some("remove-user-from-tenant") => Ok(Self::RemoveUserFromTenant {
|
||||||
|
username: args.next().ok_or_else(|| anyhow!("username required"))?,
|
||||||
|
slug: args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
}),
|
||||||
|
Some("reanalyze-documents") => Ok(Self::ReanalyzeDocuments {
|
||||||
|
slug: args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
}),
|
||||||
|
Some("list-tenants") => Ok(Self::ListTenants),
|
||||||
|
Some("delete-assets") => Ok(Self::DeleteAssets(
|
||||||
|
args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
)),
|
||||||
|
Some("quickwit-create-index") => Ok(Self::QuickwitCreate(
|
||||||
|
args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
)),
|
||||||
|
Some("quickwit-delete-index") => Ok(Self::QuickwitDelete(
|
||||||
|
args.next().ok_or_else(|| anyhow!("tenant slug required"))?,
|
||||||
|
)),
|
||||||
|
_ => Err(anyhow!(Self::usage())),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
init_tracing("info");
|
||||||
|
let command = Command::parse()?;
|
||||||
|
let config = AppConfig::load_and_log("admin")?;
|
||||||
|
let pool = db::init_pool_with_size(&config.database_url, config.database_max_pool_size)?;
|
||||||
|
|
||||||
|
match command {
|
||||||
|
Command::CreateUser { username, password } => create_user(&pool, &username, &password)?,
|
||||||
|
Command::SetPassword { username, password } => set_password(&pool, &username, &password)?,
|
||||||
|
Command::ListUsers => list_users(&pool)?,
|
||||||
|
Command::DeleteUser { username } => delete_user(&pool, &username)?,
|
||||||
|
Command::CreateTenant {
|
||||||
|
slug,
|
||||||
|
storage_root,
|
||||||
|
quickwit_index,
|
||||||
|
} => create_tenant(&pool, &slug, storage_root, quickwit_index)?,
|
||||||
|
Command::DeleteTenant { slug } => delete_tenant(&pool, &slug)?,
|
||||||
|
Command::AddUserToTenant {
|
||||||
|
username,
|
||||||
|
slug,
|
||||||
|
role,
|
||||||
|
} => add_user_to_tenant(&pool, &username, &slug, role.as_deref())?,
|
||||||
|
Command::RemoveUserFromTenant { username, slug } => {
|
||||||
|
remove_user_from_tenant(&pool, &username, &slug)?
|
||||||
|
}
|
||||||
|
Command::ReanalyzeDocuments { slug } => reanalyze_documents(&pool, &slug)?,
|
||||||
|
Command::ListTenants => list_tenants(&pool)?,
|
||||||
|
Command::DeleteAssets(slug) => delete_assets_for_tenant(&config, &pool, &slug).await?,
|
||||||
|
Command::QuickwitCreate(slug) => {
|
||||||
|
quickwit_index(&config, &pool, &slug, Method::POST).await?
|
||||||
|
}
|
||||||
|
Command::QuickwitDelete(slug) => {
|
||||||
|
quickwit_index(&config, &pool, &slug, Method::DELETE).await?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_user(pool: &PgPool, username: &str, password: &str) -> Result<()> {
|
||||||
|
if username.trim().is_empty() {
|
||||||
|
bail!("username must not be empty");
|
||||||
|
}
|
||||||
|
if password.is_empty() {
|
||||||
|
bail!("password must not be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
let exists: bool =
|
||||||
|
select(exists(users::table.filter(users::username.eq(username)))).get_result(&mut conn)?;
|
||||||
|
if exists {
|
||||||
|
bail!("user '{}' already exists", username);
|
||||||
|
}
|
||||||
|
|
||||||
|
let password_hash = hash_password(password)?;
|
||||||
|
let new_user = NewUser {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
username: username.to_string(),
|
||||||
|
password_hash,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(users::table)
|
||||||
|
.values(&new_user)
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
println!("created user '{}' (id: {})", username, new_user.id);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_password(pool: &PgPool, username: &str, password: &str) -> Result<()> {
|
||||||
|
if password.is_empty() {
|
||||||
|
bail!("password must not be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
let password_hash = hash_password(password)?;
|
||||||
|
|
||||||
|
let updated = diesel::update(users::table.filter(users::username.eq(username)))
|
||||||
|
.set(users::password_hash.eq(password_hash))
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
if updated == 0 {
|
||||||
|
bail!("user '{}' not found", username);
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("updated password for '{}'", username);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_password(password: &str) -> Result<String> {
|
||||||
|
let salt = SaltString::generate(&mut OsRng);
|
||||||
|
let hash = Argon2::default()
|
||||||
|
.hash_password(password.as_bytes(), &salt)
|
||||||
|
.map_err(|err| anyhow!(err))?;
|
||||||
|
Ok(hash.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_users(pool: &PgPool) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
|
||||||
|
let users_list: Vec<User> = users::table.order(users::username.asc()).load(&mut conn)?;
|
||||||
|
if users_list.is_empty() {
|
||||||
|
println!("No users found.");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
for user in users_list {
|
||||||
|
let memberships: Vec<(Uuid, String, String)> = user_memberships::table
|
||||||
|
.inner_join(tenants::table)
|
||||||
|
.filter(user_memberships::user_id.eq(user.id))
|
||||||
|
.select((tenants::id, tenants::slug, user_memberships::role))
|
||||||
|
.order((tenants::slug.asc(), user_memberships::role.asc()))
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
if memberships.is_empty() {
|
||||||
|
println!("{} ({})", user.username, user.id);
|
||||||
|
} else {
|
||||||
|
let details: Vec<String> = memberships
|
||||||
|
.into_iter()
|
||||||
|
.map(|(_, slug, role)| format!("{}: {}", slug, role))
|
||||||
|
.collect();
|
||||||
|
println!("{} ({}) -> {}", user.username, user.id, details.join(", "));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_user(pool: &PgPool, username: &str) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
|
||||||
|
let user: User = users::table
|
||||||
|
.filter(users::username.eq(username))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("user '{}' not found", username))?;
|
||||||
|
|
||||||
|
diesel::delete(user_memberships::table.filter(user_memberships::user_id.eq(user.id)))
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
diesel::delete(users::table.filter(users::id.eq(user.id))).execute(&mut conn)?;
|
||||||
|
|
||||||
|
println!("deleted user '{}'", username);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_tenant(
|
||||||
|
pool: &PgPool,
|
||||||
|
slug: &str,
|
||||||
|
storage_root_arg: Option<String>,
|
||||||
|
quickwit_index_arg: Option<String>,
|
||||||
|
) -> Result<()> {
|
||||||
|
if slug.trim().is_empty() {
|
||||||
|
bail!("tenant slug must not be empty");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
let exists: bool =
|
||||||
|
select(exists(tenants::table.filter(tenants::slug.eq(slug)))).get_result(&mut conn)?;
|
||||||
|
if exists {
|
||||||
|
bail!("tenant '{}' already exists", slug);
|
||||||
|
}
|
||||||
|
|
||||||
|
let id = Uuid::new_v4();
|
||||||
|
let storage_root = storage_root_arg
|
||||||
|
.map(|mut s| {
|
||||||
|
if s.is_empty() {
|
||||||
|
format!("tenants/{}/", id)
|
||||||
|
} else {
|
||||||
|
if !s.ends_with('/') {
|
||||||
|
s.push('/');
|
||||||
|
}
|
||||||
|
s
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| format!("tenants/{}/", id));
|
||||||
|
let quickwit_index = quickwit_index_arg.unwrap_or_else(|| format!("documents-{}", id));
|
||||||
|
|
||||||
|
diesel::insert_into(tenants::table)
|
||||||
|
.values((
|
||||||
|
tenants::id.eq(id),
|
||||||
|
tenants::slug.eq(slug),
|
||||||
|
tenants::storage_root.eq(Some(storage_root.clone())),
|
||||||
|
tenants::quickwit_index.eq(Some(quickwit_index.clone())),
|
||||||
|
tenants::status.eq("active"),
|
||||||
|
tenants::config.eq(serde_json::json!({})),
|
||||||
|
))
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"created tenant '{}' with id {}, storage_root '{}', quickwit_index '{}'",
|
||||||
|
slug, id, storage_root, quickwit_index
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn delete_tenant(pool: &PgPool, slug: &str) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
|
||||||
|
let tenant: Tenant = tenants::table
|
||||||
|
.filter(tenants::slug.eq(slug))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("tenant '{}' not found", slug))?;
|
||||||
|
|
||||||
|
let member_exists: bool = select(exists(
|
||||||
|
user_memberships::table.filter(user_memberships::tenant_id.eq(tenant.id)),
|
||||||
|
))
|
||||||
|
.get_result(&mut conn)?;
|
||||||
|
if member_exists {
|
||||||
|
bail!("tenant '{}' still has user memberships", slug);
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::delete(tenants::table.filter(tenants::id.eq(tenant.id))).execute(&mut conn)?;
|
||||||
|
println!("deleted tenant '{}'", slug);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_user_to_tenant(pool: &PgPool, username: &str, slug: &str, role: Option<&str>) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
|
||||||
|
let user: User = users::table
|
||||||
|
.filter(users::username.eq(username))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("user '{}' not found", username))?;
|
||||||
|
|
||||||
|
let tenant: Tenant = tenants::table
|
||||||
|
.filter(tenants::slug.eq(slug))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("tenant '{}' not found", slug))?;
|
||||||
|
|
||||||
|
let membership = NewUserMembership {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
user_id: user.id,
|
||||||
|
tenant_id: tenant.id,
|
||||||
|
role: role.unwrap_or("user").to_string(),
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(user_memberships::table)
|
||||||
|
.values(&membership)
|
||||||
|
.on_conflict((user_memberships::user_id, user_memberships::tenant_id))
|
||||||
|
.do_update()
|
||||||
|
.set(user_memberships::role.eq(&membership.role))
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"added user '{}' to tenant '{}' with role '{}'",
|
||||||
|
username, slug, membership.role
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_user_from_tenant(pool: &PgPool, username: &str, slug: &str) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
|
||||||
|
let user: User = users::table
|
||||||
|
.filter(users::username.eq(username))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("user '{}' not found", username))?;
|
||||||
|
|
||||||
|
let tenant: Tenant = tenants::table
|
||||||
|
.filter(tenants::slug.eq(slug))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("tenant '{}' not found", slug))?;
|
||||||
|
|
||||||
|
let removed = diesel::delete(
|
||||||
|
user_memberships::table
|
||||||
|
.filter(user_memberships::user_id.eq(user.id))
|
||||||
|
.filter(user_memberships::tenant_id.eq(tenant.id)),
|
||||||
|
)
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
if removed == 0 {
|
||||||
|
println!("user '{}' was not a member of tenant '{}'", username, slug);
|
||||||
|
} else {
|
||||||
|
println!("removed user '{}' from tenant '{}'", username, slug);
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn reanalyze_documents(pool: &PgPool, slug: &str) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
|
||||||
|
let tenant: Tenant = tenants::table
|
||||||
|
.filter(tenants::slug.eq(slug))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()?
|
||||||
|
.ok_or_else(|| anyhow!("tenant '{}' not found", slug))?;
|
||||||
|
|
||||||
|
let targets: Vec<(Uuid, Uuid)> = documents::table
|
||||||
|
.filter(documents::tenant_id.eq(tenant.id))
|
||||||
|
.filter(documents::deleted_at.is_null())
|
||||||
|
.select((documents::id, documents::current_version_id))
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
if targets.is_empty() {
|
||||||
|
println!("tenant '{}' has no active documents", slug);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut queued = 0usize;
|
||||||
|
for (document_id, version_id) in targets {
|
||||||
|
enqueue_job(
|
||||||
|
&mut conn,
|
||||||
|
tenant.id,
|
||||||
|
JOB_ANALYZE_DOCUMENT,
|
||||||
|
serde_json::json!({
|
||||||
|
"document_id": document_id,
|
||||||
|
"document_version_id": version_id,
|
||||||
|
"force": true,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map_err(|err| anyhow!("failed to enqueue analyze job: {}", err))?;
|
||||||
|
queued += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"queued {} documents for re-analysis in tenant '{}'",
|
||||||
|
queued, slug
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn list_tenants(pool: &PgPool) -> Result<()> {
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
let tenants: Vec<Tenant> = tenants::table
|
||||||
|
.order(tenants::slug.asc())
|
||||||
|
.load(&mut conn)
|
||||||
|
.context("failed to load tenants")?;
|
||||||
|
|
||||||
|
if tenants.is_empty() {
|
||||||
|
println!("No tenants found.");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
for tenant in tenants {
|
||||||
|
println!("{} ({})", tenant.slug, tenant.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn delete_assets_for_tenant(
|
||||||
|
config: &AppConfig,
|
||||||
|
pool: &PgPool,
|
||||||
|
tenant_slug: &str,
|
||||||
|
) -> Result<()> {
|
||||||
|
let s3_client = s3::build_client(config).await?;
|
||||||
|
let storage: Arc<dyn ObjectStorage> =
|
||||||
|
Arc::new(S3Storage::new(s3_client, config.s3_bucket.clone()));
|
||||||
|
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
let tenant: Tenant = tenants::table
|
||||||
|
.filter(tenants::slug.eq(tenant_slug))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()
|
||||||
|
.context("failed to load tenant")?
|
||||||
|
.ok_or_else(|| anyhow!("tenant '{}' not found", tenant_slug))?;
|
||||||
|
|
||||||
|
let tenant_storage = TenantStorage::new(Arc::clone(&storage), &tenant)
|
||||||
|
.with_context(|| format!("missing storage root for tenant {}", tenant.slug))?;
|
||||||
|
|
||||||
|
let assets: Vec<DocumentAsset> = document_assets::table
|
||||||
|
.filter(document_assets::tenant_id.eq(tenant.id))
|
||||||
|
.load(&mut conn)
|
||||||
|
.with_context(|| format!("failed to load assets for tenant {}", tenant.slug))?;
|
||||||
|
|
||||||
|
if assets.is_empty() {
|
||||||
|
println!("Tenant {}: no assets", tenant.slug);
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Tenant {} ({}): deleting {} assets…",
|
||||||
|
tenant.slug,
|
||||||
|
tenant.id,
|
||||||
|
assets.len()
|
||||||
|
);
|
||||||
|
|
||||||
|
let asset_ids: Vec<Uuid> = assets.iter().map(|asset| asset.id).collect();
|
||||||
|
|
||||||
|
let objects: Vec<DocumentAssetObject> = document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant.id))
|
||||||
|
.filter(document_asset_objects::asset_id.eq_any(&asset_ids))
|
||||||
|
.load(&mut conn)
|
||||||
|
.with_context(|| format!("failed to load asset objects for tenant {}", tenant.slug))?;
|
||||||
|
|
||||||
|
for object in &objects {
|
||||||
|
if let Err(err) = tenant_storage.delete_object(&object.s3_key).await {
|
||||||
|
eprintln!(
|
||||||
|
"Failed to delete object {} (tenant {}): {err}",
|
||||||
|
object.s3_key, tenant.slug
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::delete(
|
||||||
|
document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant.id))
|
||||||
|
.filter(document_asset_objects::asset_id.eq_any(&asset_ids)),
|
||||||
|
)
|
||||||
|
.execute(&mut conn)
|
||||||
|
.with_context(|| format!("failed to remove asset objects for tenant {}", tenant.slug))?;
|
||||||
|
|
||||||
|
diesel::delete(document_assets::table.filter(document_assets::tenant_id.eq(tenant.id)))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.with_context(|| format!("failed to remove asset records for tenant {}", tenant.slug))?;
|
||||||
|
|
||||||
|
println!("Tenant {}: asset records deleted.", tenant.slug);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn quickwit_index(
|
||||||
|
config: &AppConfig,
|
||||||
|
pool: &PgPool,
|
||||||
|
slug: &str,
|
||||||
|
method: Method,
|
||||||
|
) -> Result<()> {
|
||||||
|
let endpoint = config
|
||||||
|
.quickwit_endpoint
|
||||||
|
.as_ref()
|
||||||
|
.ok_or_else(|| anyhow!("quickwit endpoint not configured"))?;
|
||||||
|
|
||||||
|
let mut conn = pool.get().context("failed to get database connection")?;
|
||||||
|
let tenant: Tenant = tenants::table
|
||||||
|
.filter(tenants::slug.eq(slug))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()
|
||||||
|
.context("failed to query tenants")?
|
||||||
|
.ok_or_else(|| anyhow!("tenant '{}' not found", slug))?;
|
||||||
|
|
||||||
|
let client = Client::new();
|
||||||
|
let index_id = format!("documents-{}", tenant.id);
|
||||||
|
let base_endpoint = endpoint.trim_end_matches('/');
|
||||||
|
|
||||||
|
match method {
|
||||||
|
Method::POST => {
|
||||||
|
let payload = render_index_template(&index_id);
|
||||||
|
let response = client
|
||||||
|
.post(format!("{}/api/v1/indexes", base_endpoint))
|
||||||
|
.header("content-type", "application/json")
|
||||||
|
.body(payload)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("failed to send create index request")?;
|
||||||
|
|
||||||
|
match response.status() {
|
||||||
|
status if status.is_success() => {
|
||||||
|
diesel::update(tenants::table.filter(tenants::id.eq(tenant.id)))
|
||||||
|
.set(tenants::quickwit_index.eq(Some(index_id.clone())))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.context("failed to update tenant quickwit_index")?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Tenant '{}' quickwit index set to '{}'.",
|
||||||
|
tenant.slug, index_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
StatusCode::CONFLICT => {
|
||||||
|
let lookup = client
|
||||||
|
.get(format!("{}/api/v1/indexes/{}", base_endpoint, index_id))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("failed to verify existing quickwit index")?;
|
||||||
|
|
||||||
|
let lookup_status = lookup.status();
|
||||||
|
if !lookup_status.is_success() {
|
||||||
|
let body = lookup.text().await.unwrap_or_default();
|
||||||
|
bail!(
|
||||||
|
"quickwit reported conflict but index lookup failed with status {}: {}",
|
||||||
|
lookup_status,
|
||||||
|
body
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::update(tenants::table.filter(tenants::id.eq(tenant.id)))
|
||||||
|
.set(tenants::quickwit_index.eq(Some(index_id.clone())))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.context("failed to update tenant quickwit_index")?;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"Tenant '{}' quickwit index set to '{}'.",
|
||||||
|
tenant.slug, index_id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
status => {
|
||||||
|
let body = response.text().await.unwrap_or_default();
|
||||||
|
bail!(
|
||||||
|
"quickwit create index failed with status {}: {}",
|
||||||
|
status,
|
||||||
|
body
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Method::DELETE => {
|
||||||
|
let response = client
|
||||||
|
.delete(format!("{}/api/v1/indexes/{}", base_endpoint, index_id))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("failed to send delete index request")?;
|
||||||
|
|
||||||
|
match response.status() {
|
||||||
|
status if status.is_success() || status == StatusCode::NOT_FOUND => {
|
||||||
|
diesel::update(tenants::table.filter(tenants::id.eq(tenant.id)))
|
||||||
|
.set(tenants::quickwit_index.eq::<Option<String>>(None))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.context("failed to clear tenant quickwit_index")?;
|
||||||
|
|
||||||
|
println!("Tenant '{}' quickwit index cleared.", tenant.slug);
|
||||||
|
}
|
||||||
|
status => {
|
||||||
|
let body = response.text().await.unwrap_or_default();
|
||||||
|
bail!(
|
||||||
|
"quickwit delete index failed with status {}: {}",
|
||||||
|
status,
|
||||||
|
body
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_index_template(index_id: &str) -> String {
|
||||||
|
let mut template = QUICKWIT_INDEX_TEMPLATE.clone();
|
||||||
|
if let Some(obj) = template.as_object_mut() {
|
||||||
|
obj.insert(
|
||||||
|
"index_id".to_string(),
|
||||||
|
serde_json::Value::String(index_id.to_string()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
template.to_string()
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
use backend::openapi::ApiDoc;
|
||||||
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let spec = ApiDoc::openapi();
|
||||||
|
let json = serde_json::to_string_pretty(&spec).expect("serialize openapi");
|
||||||
|
println!("{}", json);
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
use std::net::SocketAddr;
|
||||||
|
|
||||||
|
use tokio::net::TcpListener;
|
||||||
|
use tower::make::Shared;
|
||||||
|
|
||||||
|
use backend::{routes::webdav, utils::bootstrap::init_component};
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> anyhow::Result<()> {
|
||||||
|
let state = init_component("webdav", None).await?;
|
||||||
|
let webdav_host = state.config.webdav_host.clone();
|
||||||
|
let webdav_port = state.config.webdav_port;
|
||||||
|
tracing::info!(
|
||||||
|
component = "webdav",
|
||||||
|
webdav_host = %webdav_host,
|
||||||
|
webdav_port,
|
||||||
|
"starting webdav server"
|
||||||
|
);
|
||||||
|
|
||||||
|
let listen_addr: SocketAddr = format!("{}:{}", webdav_host, webdav_port).parse()?;
|
||||||
|
let router = webdav::create_router().with_state(state.as_ref().clone());
|
||||||
|
|
||||||
|
let listener = TcpListener::bind(listen_addr).await?;
|
||||||
|
tracing::info!("listening for WebDAV on {}", listen_addr);
|
||||||
|
|
||||||
|
axum::serve(listener, Shared::new(router)).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,25 +1,13 @@
|
|||||||
use std::{sync::Arc, time::Duration};
|
use std::time::Duration;
|
||||||
|
|
||||||
use tokio::signal;
|
use tokio::signal;
|
||||||
use tracing_subscriber::EnvFilter;
|
|
||||||
|
|
||||||
use paperless_backend::{
|
use backend::{default_handlers, utils::bootstrap::init_component, Worker};
|
||||||
auth::jwt::JwtService, config::AppConfig, db, default_handlers, s3::build_client,
|
|
||||||
state::AppState, storage::S3Storage, Worker,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
dotenv::dotenv().ok();
|
let state = init_component("worker", Some(1)).await?;
|
||||||
init_tracing();
|
tracing::info!(component = "worker", "starting worker process");
|
||||||
|
|
||||||
let config = AppConfig::from_env()?;
|
|
||||||
let pool = db::init_pool(&config.database_url)?;
|
|
||||||
let s3_client = build_client(&config).await?;
|
|
||||||
let storage = Arc::new(S3Storage::new(s3_client, config.s3_bucket.clone()));
|
|
||||||
let jwt = JwtService::from_config(&config)?;
|
|
||||||
|
|
||||||
let state = Arc::new(AppState::new(pool, config, storage, jwt));
|
|
||||||
let worker = Worker::new(state, default_handlers(), Duration::from_secs(2));
|
let worker = Worker::new(state, default_handlers(), Duration::from_secs(2));
|
||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
@@ -31,12 +19,3 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_tracing() {
|
|
||||||
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
|
||||||
tracing_subscriber::fmt()
|
|
||||||
.with_env_filter(filter)
|
|
||||||
.with_target(false)
|
|
||||||
.compact()
|
|
||||||
.init();
|
|
||||||
}
|
|
||||||
|
|||||||
+113
-2
@@ -1,58 +1,169 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
use crate::db::DEFAULT_MAX_POOL_SIZE;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct AppConfig {
|
pub struct AppConfig {
|
||||||
pub database_url: String,
|
pub database_url: String,
|
||||||
|
pub database_max_pool_size: u32,
|
||||||
pub server_host: String,
|
pub server_host: String,
|
||||||
pub server_port: u16,
|
pub server_port: u16,
|
||||||
|
pub webdav_host: String,
|
||||||
|
pub webdav_port: u16,
|
||||||
pub jwt_secret: String,
|
pub jwt_secret: String,
|
||||||
pub jwt_issuer: String,
|
pub jwt_issuer: String,
|
||||||
pub jwt_audience: String,
|
pub jwt_audience: String,
|
||||||
pub jwt_expiry_minutes: i64,
|
pub jwt_expiry_minutes: i64,
|
||||||
|
pub download_token_audience: String,
|
||||||
|
pub download_token_expiry_minutes: i64,
|
||||||
|
pub refresh_token_expiry_days: i64,
|
||||||
|
pub refresh_cookie_secure: bool,
|
||||||
|
pub refresh_cookie_domain: Option<String>,
|
||||||
|
pub cors_allowed_origin: Option<String>,
|
||||||
pub aws_endpoint_url: Option<String>,
|
pub aws_endpoint_url: Option<String>,
|
||||||
pub aws_access_key_id: Option<String>,
|
pub aws_access_key_id: Option<String>,
|
||||||
pub aws_secret_access_key: Option<String>,
|
pub aws_secret_access_key: Option<String>,
|
||||||
pub aws_region: String,
|
pub aws_region: String,
|
||||||
pub s3_bucket: String,
|
pub s3_bucket: String,
|
||||||
|
pub quickwit_endpoint: Option<String>,
|
||||||
|
pub quickwit_index: Option<String>,
|
||||||
|
pub default_tenant_slug: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppConfig {
|
impl AppConfig {
|
||||||
|
pub fn load_and_log(component: &str) -> Result<Self> {
|
||||||
|
dotenv::dotenv().ok();
|
||||||
|
let config = Self::from_env()?;
|
||||||
|
tracing::info!(
|
||||||
|
component,
|
||||||
|
database_url = %config.redacted_database_url(),
|
||||||
|
pool_size = config.database_max_pool_size,
|
||||||
|
quickwit_enabled = config.quickwit_endpoint.is_some(),
|
||||||
|
s3_bucket = %config.s3_bucket,
|
||||||
|
"loaded backend configuration"
|
||||||
|
);
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_env() -> Result<Self> {
|
pub fn from_env() -> Result<Self> {
|
||||||
let database_url = env::var("DATABASE_URL").context("DATABASE_URL must be set")?;
|
let database_url = env::var("DATABASE_URL").context("DATABASE_URL must be set")?;
|
||||||
|
let database_max_pool_size = env::var("DATABASE_MAX_POOL_SIZE")
|
||||||
|
.ok()
|
||||||
|
.and_then(|value| value.parse().ok())
|
||||||
|
.unwrap_or(DEFAULT_MAX_POOL_SIZE);
|
||||||
let server_host = env::var("SERVER_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
|
let server_host = env::var("SERVER_HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
|
||||||
let server_port = env::var("SERVER_PORT")
|
let server_port = env::var("SERVER_PORT")
|
||||||
.unwrap_or_else(|_| "3000".to_string())
|
.unwrap_or_else(|_| "3000".to_string())
|
||||||
.parse()
|
.parse()
|
||||||
.context("SERVER_PORT must be a valid u16")?;
|
.context("SERVER_PORT must be a valid u16")?;
|
||||||
|
let webdav_host = env::var("WEBDAV_HOST").unwrap_or_else(|_| server_host.clone());
|
||||||
|
let webdav_port = env::var("WEBDAV_PORT")
|
||||||
|
.unwrap_or_else(|_| "3001".to_string())
|
||||||
|
.parse()
|
||||||
|
.context("WEBDAV_PORT must be a valid u16")?;
|
||||||
let jwt_secret = env::var("JWT_SECRET").context("JWT_SECRET must be set")?;
|
let jwt_secret = env::var("JWT_SECRET").context("JWT_SECRET must be set")?;
|
||||||
let jwt_issuer = env::var("JWT_ISSUER").unwrap_or_else(|_| "paperless-neo".to_string());
|
let jwt_issuer = env::var("JWT_ISSUER").unwrap_or_else(|_| "papercrate".to_string());
|
||||||
let jwt_audience =
|
let jwt_audience =
|
||||||
env::var("JWT_AUDIENCE").unwrap_or_else(|_| "paperless-neo-clients".to_string());
|
env::var("JWT_AUDIENCE").unwrap_or_else(|_| "papercrate-clients".to_string());
|
||||||
let jwt_expiry_minutes = env::var("JWT_EXPIRY_MINUTES")
|
let jwt_expiry_minutes = env::var("JWT_EXPIRY_MINUTES")
|
||||||
.unwrap_or_else(|_| "60".to_string())
|
.unwrap_or_else(|_| "60".to_string())
|
||||||
.parse()
|
.parse()
|
||||||
.context("JWT_EXPIRY_MINUTES must be an integer")?;
|
.context("JWT_EXPIRY_MINUTES must be an integer")?;
|
||||||
|
let download_token_audience = env::var("DOWNLOAD_TOKEN_AUDIENCE")
|
||||||
|
.unwrap_or_else(|_| "papercrate-download".to_string());
|
||||||
|
let download_token_expiry_minutes = env::var("DOWNLOAD_TOKEN_EXPIRY_MINUTES")
|
||||||
|
.unwrap_or_else(|_| "60".to_string())
|
||||||
|
.parse()
|
||||||
|
.context("DOWNLOAD_TOKEN_EXPIRY_MINUTES must be an integer")?;
|
||||||
|
let refresh_token_expiry_days = env::var("REFRESH_TOKEN_EXPIRY_DAYS")
|
||||||
|
.unwrap_or_else(|_| "30".to_string())
|
||||||
|
.parse()
|
||||||
|
.context("REFRESH_TOKEN_EXPIRY_DAYS must be an integer")?;
|
||||||
|
let refresh_cookie_secure = env::var("REFRESH_COOKIE_SECURE")
|
||||||
|
.map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
|
||||||
|
.unwrap_or(false);
|
||||||
|
let refresh_cookie_domain = env::var("REFRESH_COOKIE_DOMAIN").ok();
|
||||||
|
let cors_allowed_origin = env::var("CORS_ALLOWED_ORIGIN").ok();
|
||||||
let aws_endpoint_url = env::var("AWS_ENDPOINT_URL").ok();
|
let aws_endpoint_url = env::var("AWS_ENDPOINT_URL").ok();
|
||||||
let aws_access_key_id = env::var("AWS_ACCESS_KEY_ID").ok();
|
let aws_access_key_id = env::var("AWS_ACCESS_KEY_ID").ok();
|
||||||
let aws_secret_access_key = env::var("AWS_SECRET_ACCESS_KEY").ok();
|
let aws_secret_access_key = env::var("AWS_SECRET_ACCESS_KEY").ok();
|
||||||
let aws_region = env::var("AWS_REGION").unwrap_or_else(|_| "us-east-1".to_string());
|
let aws_region = env::var("AWS_REGION").unwrap_or_else(|_| "us-east-1".to_string());
|
||||||
let s3_bucket = env::var("S3_BUCKET").context("S3_BUCKET must be set")?;
|
let s3_bucket = env::var("S3_BUCKET").context("S3_BUCKET must be set")?;
|
||||||
|
let quickwit_endpoint = env::var("QUICKWIT_ENDPOINT").ok();
|
||||||
|
let quickwit_index = env::var("QUICKWIT_INDEX").ok();
|
||||||
|
let default_tenant_slug =
|
||||||
|
env::var("DEFAULT_TENANT_SLUG").unwrap_or_else(|_| "admin".to_string());
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
database_url,
|
database_url,
|
||||||
|
database_max_pool_size,
|
||||||
server_host,
|
server_host,
|
||||||
server_port,
|
server_port,
|
||||||
|
webdav_host,
|
||||||
|
webdav_port,
|
||||||
jwt_secret,
|
jwt_secret,
|
||||||
jwt_issuer,
|
jwt_issuer,
|
||||||
jwt_audience,
|
jwt_audience,
|
||||||
jwt_expiry_minutes,
|
jwt_expiry_minutes,
|
||||||
|
download_token_audience,
|
||||||
|
download_token_expiry_minutes,
|
||||||
|
refresh_token_expiry_days,
|
||||||
|
refresh_cookie_secure,
|
||||||
|
refresh_cookie_domain,
|
||||||
|
cors_allowed_origin,
|
||||||
aws_endpoint_url,
|
aws_endpoint_url,
|
||||||
aws_access_key_id,
|
aws_access_key_id,
|
||||||
aws_secret_access_key,
|
aws_secret_access_key,
|
||||||
aws_region,
|
aws_region,
|
||||||
s3_bucket,
|
s3_bucket,
|
||||||
|
quickwit_endpoint,
|
||||||
|
quickwit_index,
|
||||||
|
default_tenant_slug,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn redacted_database_url(&self) -> String {
|
||||||
|
redact_database_url(&self.database_url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn redact_database_url(raw: &str) -> String {
|
||||||
|
match Url::parse(raw) {
|
||||||
|
Ok(mut parsed) => {
|
||||||
|
if parsed.password().is_some() {
|
||||||
|
let _ = parsed.set_password(Some("*****"));
|
||||||
|
parsed.to_string()
|
||||||
|
} else {
|
||||||
|
raw.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(_) => "***".to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::redact_database_url;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn redacts_password_in_database_url() {
|
||||||
|
let redacted = redact_database_url("postgres://user:secret@localhost/db");
|
||||||
|
assert!(redacted.contains("postgres://user:*****@"));
|
||||||
|
assert!(!redacted.contains("secret"));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn handles_url_without_password() {
|
||||||
|
let redacted = redact_database_url("postgres://localhost/db");
|
||||||
|
assert_eq!(redacted, "postgres://localhost/db");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn falls_back_when_parse_fails() {
|
||||||
|
let redacted = redact_database_url("not a url");
|
||||||
|
assert_eq!(redacted, "***");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-1
@@ -5,10 +5,17 @@ use diesel::r2d2::{ConnectionManager, Pool};
|
|||||||
|
|
||||||
pub type PgPool = Pool<ConnectionManager<PgConnection>>;
|
pub type PgPool = Pool<ConnectionManager<PgConnection>>;
|
||||||
|
|
||||||
|
pub const DEFAULT_MAX_POOL_SIZE: u32 = 2;
|
||||||
|
|
||||||
pub fn init_pool(database_url: &str) -> anyhow::Result<PgPool> {
|
pub fn init_pool(database_url: &str) -> anyhow::Result<PgPool> {
|
||||||
|
init_pool_with_size(database_url, DEFAULT_MAX_POOL_SIZE)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init_pool_with_size(database_url: &str, max_size: u32) -> anyhow::Result<PgPool> {
|
||||||
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||||
|
let pool_size = max_size.max(1);
|
||||||
let pool = Pool::builder()
|
let pool = Pool::builder()
|
||||||
.max_size(16)
|
.max_size(pool_size)
|
||||||
.connection_timeout(Duration::from_secs(10))
|
.connection_timeout(Duration::from_secs(10))
|
||||||
.build(manager)?;
|
.build(manager)?;
|
||||||
Ok(pool)
|
Ok(pool)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub type AppResult<T> = Result<T, AppError>;
|
|||||||
pub struct AppError {
|
pub struct AppError {
|
||||||
status: StatusCode,
|
status: StatusCode,
|
||||||
message: String,
|
message: String,
|
||||||
|
code: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppError {
|
impl AppError {
|
||||||
@@ -19,6 +20,7 @@ impl AppError {
|
|||||||
Self {
|
Self {
|
||||||
status,
|
status,
|
||||||
message: message.into(),
|
message: message.into(),
|
||||||
|
code: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,6 +28,10 @@ impl AppError {
|
|||||||
Self::new(StatusCode::BAD_REQUEST, message)
|
Self::new(StatusCode::BAD_REQUEST, message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn conflict(message: impl Into<String>) -> Self {
|
||||||
|
Self::new(StatusCode::CONFLICT, message)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn unauthorized() -> Self {
|
pub fn unauthorized() -> Self {
|
||||||
Self::new(StatusCode::UNAUTHORIZED, "unauthorized")
|
Self::new(StatusCode::UNAUTHORIZED, "unauthorized")
|
||||||
}
|
}
|
||||||
@@ -37,6 +43,11 @@ impl AppError {
|
|||||||
pub fn internal<E: Display>(error: E) -> Self {
|
pub fn internal<E: Display>(error: E) -> Self {
|
||||||
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.to_string())
|
Self::new(StatusCode::INTERNAL_SERVER_ERROR, error.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn with_code(mut self, code: impl Into<String>) -> Self {
|
||||||
|
self.code = Some(code.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IntoResponse for AppError {
|
impl IntoResponse for AppError {
|
||||||
@@ -44,6 +55,7 @@ impl IntoResponse for AppError {
|
|||||||
let status = self.status;
|
let status = self.status;
|
||||||
let body = Json(ErrorResponse {
|
let body = Json(ErrorResponse {
|
||||||
error: self.message,
|
error: self.message,
|
||||||
|
code: self.code,
|
||||||
});
|
});
|
||||||
(status, body).into_response()
|
(status, body).into_response()
|
||||||
}
|
}
|
||||||
@@ -52,6 +64,8 @@ impl IntoResponse for AppError {
|
|||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct ErrorResponse {
|
struct ErrorResponse {
|
||||||
error: String,
|
error: String,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
code: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<diesel::result::Error> for AppError {
|
impl From<diesel::result::Error> for AppError {
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ pub const STATUS_FAILED: &str = "failed";
|
|||||||
|
|
||||||
pub const JOB_ANALYZE_DOCUMENT: &str = "analyze-document";
|
pub const JOB_ANALYZE_DOCUMENT: &str = "analyze-document";
|
||||||
pub const JOB_GENERATE_THUMBNAILS: &str = "generate-thumbnails";
|
pub const JOB_GENERATE_THUMBNAILS: &str = "generate-thumbnails";
|
||||||
|
pub const JOB_GENERATE_OCR_TEXT: &str = "generate-ocr-text";
|
||||||
|
pub const JOB_INDEX_DOCUMENT_TEXT: &str = "index-document-text";
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
#[derive(Debug, Error)]
|
||||||
pub enum JobQueueError {
|
pub enum JobQueueError {
|
||||||
@@ -28,6 +30,7 @@ pub type JobQueueResult<T> = Result<T, JobQueueError>;
|
|||||||
|
|
||||||
pub fn enqueue_job(
|
pub fn enqueue_job(
|
||||||
conn: &mut PgConnection,
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
job_type: &str,
|
job_type: &str,
|
||||||
payload: Value,
|
payload: Value,
|
||||||
run_after: Option<NaiveDateTime>,
|
run_after: Option<NaiveDateTime>,
|
||||||
@@ -38,6 +41,7 @@ pub fn enqueue_job(
|
|||||||
payload,
|
payload,
|
||||||
status: STATUS_QUEUED.to_string(),
|
status: STATUS_QUEUED.to_string(),
|
||||||
run_after: run_after.unwrap_or_else(|| Utc::now().naive_utc()),
|
run_after: run_after.unwrap_or_else(|| Utc::now().naive_utc()),
|
||||||
|
tenant_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
diesel::insert_into(jobs::table)
|
diesel::insert_into(jobs::table)
|
||||||
|
|||||||
@@ -4,10 +4,13 @@ pub mod db;
|
|||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod jobs;
|
pub mod jobs;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
|
pub mod openapi;
|
||||||
pub mod routes;
|
pub mod routes;
|
||||||
pub mod s3;
|
pub mod s3;
|
||||||
pub mod schema;
|
pub mod schema;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod storage;
|
pub mod storage;
|
||||||
|
pub mod tenants;
|
||||||
|
pub mod utils;
|
||||||
pub mod workers;
|
pub mod workers;
|
||||||
pub use workers::{default_handlers, Worker};
|
pub use workers::{default_handlers, Worker};
|
||||||
|
|||||||
+12
-31
@@ -1,47 +1,28 @@
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use tokio::net::TcpListener;
|
use tokio::net::TcpListener;
|
||||||
use tower::make::Shared;
|
use tower::make::Shared;
|
||||||
use tracing_subscriber::EnvFilter;
|
|
||||||
|
|
||||||
use paperless_backend::auth::jwt::JwtService;
|
use backend::{routes, utils::bootstrap::init_component};
|
||||||
use paperless_backend::config::AppConfig;
|
|
||||||
use paperless_backend::db;
|
|
||||||
use paperless_backend::routes;
|
|
||||||
use paperless_backend::s3::build_client;
|
|
||||||
use paperless_backend::state::AppState;
|
|
||||||
use paperless_backend::storage::S3Storage;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
dotenv::dotenv().ok();
|
let state = init_component("api", None).await?;
|
||||||
init_tracing();
|
let server_host = state.config.server_host.clone();
|
||||||
|
let server_port = state.config.server_port;
|
||||||
|
tracing::info!(
|
||||||
|
component = "api",
|
||||||
|
server_host = %server_host,
|
||||||
|
server_port,
|
||||||
|
"starting api server"
|
||||||
|
);
|
||||||
|
|
||||||
let config = AppConfig::from_env()?;
|
let router = routes::create_router(state.as_ref().clone());
|
||||||
let pool = db::init_pool(&config.database_url)?;
|
|
||||||
let s3_client = build_client(&config).await?;
|
|
||||||
let storage = Arc::new(S3Storage::new(s3_client, config.s3_bucket.clone()));
|
|
||||||
let jwt = JwtService::from_config(&config)?;
|
|
||||||
|
|
||||||
let state = AppState::new(pool, config, storage, jwt);
|
let addr: SocketAddr = format!("{}:{}", server_host, server_port).parse()?;
|
||||||
|
|
||||||
let router = routes::create_router(state.clone());
|
|
||||||
|
|
||||||
let addr: SocketAddr =
|
|
||||||
format!("{}:{}", state.config.server_host, state.config.server_port).parse()?;
|
|
||||||
let listener = TcpListener::bind(addr).await?;
|
let listener = TcpListener::bind(addr).await?;
|
||||||
tracing::info!("listening on {}", addr);
|
tracing::info!("listening on {}", addr);
|
||||||
|
|
||||||
axum::serve(listener, Shared::new(router)).await?;
|
axum::serve(listener, Shared::new(router)).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_tracing() {
|
|
||||||
let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info"));
|
|
||||||
tracing_subscriber::fmt()
|
|
||||||
.with_env_filter(filter)
|
|
||||||
.with_target(false)
|
|
||||||
.compact()
|
|
||||||
.init();
|
|
||||||
}
|
|
||||||
|
|||||||
+149
-12
@@ -4,13 +4,48 @@ use uuid::Uuid;
|
|||||||
|
|
||||||
use crate::schema::*;
|
use crate::schema::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||||
|
#[diesel(table_name = user_memberships)]
|
||||||
|
#[diesel(belongs_to(User, foreign_key = user_id))]
|
||||||
|
#[diesel(belongs_to(Tenant, foreign_key = tenant_id))]
|
||||||
|
pub struct UserMembership {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
pub role: String,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
|
pub updated_at: NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Insertable)]
|
||||||
|
#[diesel(table_name = user_memberships)]
|
||||||
|
pub struct NewUserMembership {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||||
|
#[diesel(table_name = tenants)]
|
||||||
|
#[diesel(primary_key(id))]
|
||||||
|
pub struct Tenant {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub slug: String,
|
||||||
|
pub storage_root: Option<String>,
|
||||||
|
pub quickwit_index: Option<String>,
|
||||||
|
pub status: String,
|
||||||
|
pub config: serde_json::Value,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
|
pub updated_at: NaiveDateTime,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable)]
|
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||||
#[diesel(table_name = users)]
|
#[diesel(table_name = users)]
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password_hash: String,
|
pub password_hash: String,
|
||||||
pub role: String,
|
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
pub updated_at: NaiveDateTime,
|
pub updated_at: NaiveDateTime,
|
||||||
}
|
}
|
||||||
@@ -21,7 +56,6 @@ pub struct NewUser {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password_hash: String,
|
pub password_hash: String,
|
||||||
pub role: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable)]
|
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||||
@@ -30,9 +64,9 @@ pub struct Folder {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub parent_id: Option<Uuid>,
|
pub parent_id: Option<Uuid>,
|
||||||
pub path_cache: Option<String>,
|
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
pub updated_at: NaiveDateTime,
|
pub updated_at: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -41,7 +75,7 @@ pub struct NewFolder {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub parent_id: Option<Uuid>,
|
pub parent_id: Option<Uuid>,
|
||||||
pub path_cache: Option<String>,
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||||
@@ -53,13 +87,14 @@ pub struct Document {
|
|||||||
pub original_name: String,
|
pub original_name: String,
|
||||||
pub content_type: Option<String>,
|
pub content_type: Option<String>,
|
||||||
pub folder_id: Option<Uuid>,
|
pub folder_id: Option<Uuid>,
|
||||||
pub current_version: i32,
|
|
||||||
pub uploaded_at: NaiveDateTime,
|
pub uploaded_at: NaiveDateTime,
|
||||||
pub updated_at: NaiveDateTime,
|
pub updated_at: NaiveDateTime,
|
||||||
pub deleted_at: Option<NaiveDateTime>,
|
pub deleted_at: Option<NaiveDateTime>,
|
||||||
pub metadata: serde_json::Value,
|
pub metadata: serde_json::Value,
|
||||||
pub issued_at: Option<NaiveDateTime>,
|
pub issued_at: Option<NaiveDateTime>,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
pub current_version_id: Uuid,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -70,10 +105,11 @@ pub struct NewDocument {
|
|||||||
pub original_name: String,
|
pub original_name: String,
|
||||||
pub content_type: Option<String>,
|
pub content_type: Option<String>,
|
||||||
pub folder_id: Option<Uuid>,
|
pub folder_id: Option<Uuid>,
|
||||||
pub current_version: i32,
|
pub current_version_id: Uuid,
|
||||||
pub metadata: serde_json::Value,
|
pub metadata: serde_json::Value,
|
||||||
pub issued_at: Option<NaiveDateTime>,
|
pub issued_at: Option<NaiveDateTime>,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||||
@@ -88,6 +124,8 @@ pub struct DocumentVersion {
|
|||||||
pub checksum: String,
|
pub checksum: String,
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
pub operations_summary: serde_json::Value,
|
pub operations_summary: serde_json::Value,
|
||||||
|
pub metadata: serde_json::Value,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -100,6 +138,8 @@ pub struct NewDocumentVersion {
|
|||||||
pub size_bytes: i64,
|
pub size_bytes: i64,
|
||||||
pub checksum: String,
|
pub checksum: String,
|
||||||
pub operations_summary: serde_json::Value,
|
pub operations_summary: serde_json::Value,
|
||||||
|
pub metadata: serde_json::Value,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||||
@@ -109,12 +149,11 @@ pub struct DocumentAsset {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub document_version_id: Uuid,
|
pub document_version_id: Uuid,
|
||||||
pub asset_type: String,
|
pub asset_type: String,
|
||||||
pub s3_key: String,
|
|
||||||
pub mime_type: String,
|
pub mime_type: String,
|
||||||
pub width: Option<i32>,
|
|
||||||
pub height: Option<i32>,
|
|
||||||
pub metadata: serde_json::Value,
|
pub metadata: serde_json::Value,
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
|
pub cardinality: Option<i32>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -123,11 +162,33 @@ pub struct NewDocumentAsset {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub document_version_id: Uuid,
|
pub document_version_id: Uuid,
|
||||||
pub asset_type: String,
|
pub asset_type: String,
|
||||||
pub s3_key: String,
|
|
||||||
pub mime_type: String,
|
pub mime_type: String,
|
||||||
pub width: Option<i32>,
|
|
||||||
pub height: Option<i32>,
|
|
||||||
pub metadata: serde_json::Value,
|
pub metadata: serde_json::Value,
|
||||||
|
pub cardinality: Option<i32>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||||
|
#[diesel(table_name = document_asset_objects)]
|
||||||
|
#[diesel(belongs_to(DocumentAsset, foreign_key = asset_id))]
|
||||||
|
pub struct DocumentAssetObject {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub asset_id: Uuid,
|
||||||
|
pub ordinal: i32,
|
||||||
|
pub s3_key: String,
|
||||||
|
pub metadata: serde_json::Value,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Insertable)]
|
||||||
|
#[diesel(table_name = document_asset_objects)]
|
||||||
|
pub struct NewDocumentAssetObject {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub asset_id: Uuid,
|
||||||
|
pub ordinal: i32,
|
||||||
|
pub s3_key: String,
|
||||||
|
pub metadata: serde_json::Value,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable)]
|
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||||
@@ -142,6 +203,7 @@ pub struct Job {
|
|||||||
pub last_error: Option<String>,
|
pub last_error: Option<String>,
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
pub updated_at: NaiveDateTime,
|
pub updated_at: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -152,6 +214,7 @@ pub struct NewJob {
|
|||||||
pub payload: serde_json::Value,
|
pub payload: serde_json::Value,
|
||||||
pub status: String,
|
pub status: String,
|
||||||
pub run_after: NaiveDateTime,
|
pub run_after: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Queryable, Identifiable)]
|
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||||
@@ -161,6 +224,7 @@ pub struct Tag {
|
|||||||
pub label: String,
|
pub label: String,
|
||||||
pub color: Option<String>,
|
pub color: Option<String>,
|
||||||
pub created_at: NaiveDateTime,
|
pub created_at: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -169,6 +233,7 @@ pub struct NewTag {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
pub color: Option<String>,
|
pub color: Option<String>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -182,6 +247,7 @@ pub struct DocumentTag {
|
|||||||
pub tag_id: Uuid,
|
pub tag_id: Uuid,
|
||||||
pub assigned_at: NaiveDateTime,
|
pub assigned_at: NaiveDateTime,
|
||||||
pub assigned_by: Option<Uuid>,
|
pub assigned_by: Option<Uuid>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Insertable)]
|
#[derive(Debug, Insertable)]
|
||||||
@@ -190,4 +256,75 @@ pub struct NewDocumentTag {
|
|||||||
pub document_id: Uuid,
|
pub document_id: Uuid,
|
||||||
pub tag_id: Uuid,
|
pub tag_id: Uuid,
|
||||||
pub assigned_by: Option<Uuid>,
|
pub assigned_by: Option<Uuid>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Queryable, Identifiable)]
|
||||||
|
#[diesel(table_name = correspondents)]
|
||||||
|
pub struct Correspondent {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub metadata: serde_json::Value,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
|
pub updated_at: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Insertable)]
|
||||||
|
#[diesel(table_name = correspondents)]
|
||||||
|
pub struct NewCorrespondent {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub metadata: serde_json::Value,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Queryable, Associations)]
|
||||||
|
#[diesel(table_name = document_correspondents)]
|
||||||
|
#[diesel(belongs_to(Document))]
|
||||||
|
#[diesel(belongs_to(Correspondent))]
|
||||||
|
#[diesel(primary_key(document_id, correspondent_id, role))]
|
||||||
|
pub struct DocumentCorrespondent {
|
||||||
|
pub document_id: Uuid,
|
||||||
|
pub correspondent_id: Uuid,
|
||||||
|
pub role: String,
|
||||||
|
pub assigned_at: NaiveDateTime,
|
||||||
|
pub assigned_by: Option<Uuid>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Insertable)]
|
||||||
|
#[diesel(table_name = document_correspondents)]
|
||||||
|
pub struct NewDocumentCorrespondent {
|
||||||
|
pub document_id: Uuid,
|
||||||
|
pub correspondent_id: Uuid,
|
||||||
|
pub role: String,
|
||||||
|
pub assigned_by: Option<Uuid>,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Queryable, Identifiable, Associations)]
|
||||||
|
#[diesel(table_name = refresh_tokens)]
|
||||||
|
#[diesel(belongs_to(User))]
|
||||||
|
pub struct RefreshToken {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub token_hash: String,
|
||||||
|
pub issued_at: NaiveDateTime,
|
||||||
|
pub expires_at: NaiveDateTime,
|
||||||
|
pub revoked_at: Option<NaiveDateTime>,
|
||||||
|
pub created_at: NaiveDateTime,
|
||||||
|
pub updated_at: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Insertable)]
|
||||||
|
#[diesel(table_name = refresh_tokens)]
|
||||||
|
pub struct NewRefreshToken {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub user_id: Uuid,
|
||||||
|
pub token_hash: String,
|
||||||
|
pub issued_at: NaiveDateTime,
|
||||||
|
pub expires_at: NaiveDateTime,
|
||||||
|
pub tenant_id: Uuid,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,951 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
|
use utoipa::{IntoParams, OpenApi, ToSchema};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[derive(OpenApi)]
|
||||||
|
#[openapi(
|
||||||
|
paths(
|
||||||
|
doc::health_check,
|
||||||
|
doc::login,
|
||||||
|
doc::refresh,
|
||||||
|
doc::logout,
|
||||||
|
doc::me,
|
||||||
|
doc::select_tenant,
|
||||||
|
doc::list_documents,
|
||||||
|
doc::check_document,
|
||||||
|
doc::upload_document,
|
||||||
|
doc::get_document,
|
||||||
|
doc::update_document,
|
||||||
|
doc::delete_document,
|
||||||
|
doc::download_document,
|
||||||
|
doc::download_with_token,
|
||||||
|
doc::move_document,
|
||||||
|
doc::assign_tags,
|
||||||
|
doc::remove_tag,
|
||||||
|
doc::bulk_move_documents,
|
||||||
|
doc::bulk_update_tags,
|
||||||
|
doc::bulk_assign_correspondents,
|
||||||
|
doc::assign_correspondents,
|
||||||
|
doc::remove_correspondent,
|
||||||
|
doc::reanalyze_selected_documents,
|
||||||
|
doc::list_document_assets,
|
||||||
|
doc::request_document_assets,
|
||||||
|
doc::get_document_asset,
|
||||||
|
doc::create_folder,
|
||||||
|
doc::ensure_folder_path,
|
||||||
|
doc::get_folder,
|
||||||
|
doc::list_folder_contents,
|
||||||
|
doc::delete_folder,
|
||||||
|
doc::update_folder,
|
||||||
|
doc::list_tags,
|
||||||
|
doc::create_tag,
|
||||||
|
doc::update_tag,
|
||||||
|
doc::delete_tag,
|
||||||
|
doc::list_correspondents,
|
||||||
|
doc::create_correspondent,
|
||||||
|
doc::update_correspondent,
|
||||||
|
doc::delete_correspondent,
|
||||||
|
),
|
||||||
|
components(
|
||||||
|
schemas(
|
||||||
|
schemas::LoginRequest,
|
||||||
|
schemas::AccessTokenResponse,
|
||||||
|
schemas::TenantSummary,
|
||||||
|
schemas::TenantSelectionResponse,
|
||||||
|
schemas::TenantSelectionRequest,
|
||||||
|
schemas::LoginResponseVariants,
|
||||||
|
schemas::DocumentResponse,
|
||||||
|
schemas::DocumentDetailResponse,
|
||||||
|
schemas::DocumentVersion,
|
||||||
|
schemas::DocumentAssetSummary,
|
||||||
|
schemas::DocumentAssetDetail,
|
||||||
|
schemas::DocumentAssetObject,
|
||||||
|
schemas::DocumentCorrespondent,
|
||||||
|
schemas::DocumentTag,
|
||||||
|
schemas::DocumentDownloadResponse,
|
||||||
|
schemas::UpdateDocumentRequest,
|
||||||
|
schemas::BulkMoveDocumentsRequest,
|
||||||
|
schemas::BulkMoveDocumentsResponse,
|
||||||
|
schemas::AssignTagsRequest,
|
||||||
|
schemas::MoveDocumentRequest,
|
||||||
|
schemas::BulkTagRequest,
|
||||||
|
schemas::BulkTagResponse,
|
||||||
|
schemas::CorrespondentAssignment,
|
||||||
|
schemas::BulkTagAction,
|
||||||
|
schemas::BulkCorrespondentsRequest,
|
||||||
|
schemas::BulkCorrespondentsResponse,
|
||||||
|
schemas::BulkCorrespondentAction,
|
||||||
|
schemas::AssignCorrespondentsRequest,
|
||||||
|
schemas::RemoveCorrespondentParams,
|
||||||
|
schemas::ReanalyzeRequest,
|
||||||
|
schemas::ReanalyzeResponse,
|
||||||
|
schemas::DocumentAssetRequestParams,
|
||||||
|
schemas::AssetObjectsQuery,
|
||||||
|
schemas::DocumentCheckQuery,
|
||||||
|
schemas::DocumentCheckResponse,
|
||||||
|
schemas::UploadDocumentForm,
|
||||||
|
schemas::CreateFolderRequest,
|
||||||
|
schemas::EnsureFolderPathRequest,
|
||||||
|
schemas::FolderResponse,
|
||||||
|
schemas::FolderInfo,
|
||||||
|
schemas::FolderContentsResponse,
|
||||||
|
schemas::FolderDocumentSummary,
|
||||||
|
schemas::UpdateFolderRequest,
|
||||||
|
schemas::FolderContentsParams,
|
||||||
|
schemas::TagCatalogEntry,
|
||||||
|
schemas::CreateTagRequest,
|
||||||
|
schemas::UpdateTagRequest,
|
||||||
|
schemas::CorrespondentCatalogEntry,
|
||||||
|
schemas::CreateCorrespondentRequest,
|
||||||
|
schemas::UpdateCorrespondentRequest,
|
||||||
|
)
|
||||||
|
),
|
||||||
|
tags(
|
||||||
|
(name = "Health", description = "Service health"),
|
||||||
|
(name = "Auth", description = "Authentication"),
|
||||||
|
(name = "Documents", description = "Document management"),
|
||||||
|
(name = "Assets", description = "Document assets"),
|
||||||
|
(name = "Folders", description = "Folder management"),
|
||||||
|
(name = "Tags", description = "Tag catalog"),
|
||||||
|
(name = "Correspondents", description = "Correspondent catalog")
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
pub struct ApiDoc;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
mod doc {
|
||||||
|
use super::schemas::*;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn __keep_uuid_import() {
|
||||||
|
let _ = Uuid::nil();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/health",
|
||||||
|
responses((status = 200, description = "Service is healthy")),
|
||||||
|
tag = "Health"
|
||||||
|
)]
|
||||||
|
pub(super) fn health_check() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/auth/login",
|
||||||
|
request_body = LoginRequest,
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Login succeeded", body = LoginResponseVariants),
|
||||||
|
(status = 401, description = "Invalid credentials")
|
||||||
|
),
|
||||||
|
tag = "Auth"
|
||||||
|
)]
|
||||||
|
pub(super) fn login() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/auth/refresh",
|
||||||
|
responses(
|
||||||
|
(status = 200, description = "Refreshed access token", body = AccessTokenResponse),
|
||||||
|
(status = 401, description = "Missing or invalid refresh token")
|
||||||
|
),
|
||||||
|
tag = "Auth"
|
||||||
|
)]
|
||||||
|
pub(super) fn refresh() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/auth/logout",
|
||||||
|
responses((status = 204, description = "Session revoked")),
|
||||||
|
tag = "Auth"
|
||||||
|
)]
|
||||||
|
pub(super) fn logout() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/auth/me",
|
||||||
|
responses((status = 200, description = "Authenticated principal", body = AccessTokenResponse)),
|
||||||
|
tag = "Auth"
|
||||||
|
)]
|
||||||
|
pub(super) fn me() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/auth/select-tenant",
|
||||||
|
request_body = TenantSelectionRequest,
|
||||||
|
responses((status = 200, description = "Tenant selected", body = AccessTokenResponse)),
|
||||||
|
tag = "Auth"
|
||||||
|
)]
|
||||||
|
pub(super) fn select_tenant() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/documents",
|
||||||
|
params(DocumentListQuery),
|
||||||
|
responses((status = 200, description = "List documents", body = [DocumentResponse])),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn list_documents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents",
|
||||||
|
request_body = UploadDocumentForm,
|
||||||
|
responses(
|
||||||
|
(status = 201, description = "Document created", body = DocumentDetailResponse),
|
||||||
|
(status = 200, description = "Existing document reused", body = DocumentDetailResponse),
|
||||||
|
(status = 204, description = "Upload skipped because the document already exists")
|
||||||
|
),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn upload_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/documents/check",
|
||||||
|
params(DocumentCheckQuery),
|
||||||
|
responses((status = 200, description = "Checksum lookup", body = DocumentCheckResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn check_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/documents/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
responses((status = 200, description = "Document detail", body = DocumentDetailResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn get_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = "/api/documents/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
request_body = UpdateDocumentRequest,
|
||||||
|
responses((status = 200, description = "Updated document", body = DocumentDetailResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn update_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/documents/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
responses((status = 204, description = "Document deleted")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn delete_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/documents/{id}/download",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
responses((status = 200, description = "Download metadata", body = DocumentDownloadResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn download_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/download/{token}",
|
||||||
|
params(("token" = String, Path, description = "Download token")),
|
||||||
|
responses((status = 302, description = "Redirect to pre-signed URL")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn download_with_token() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = "/api/documents/{id}/folder",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
request_body = MoveDocumentRequest,
|
||||||
|
responses((status = 204, description = "Document moved")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn move_document() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/{id}/tags",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
request_body = AssignTagsRequest,
|
||||||
|
responses((status = 204, description = "Tags assigned")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn assign_tags() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/documents/{id}/tags/{tag_id}",
|
||||||
|
params(
|
||||||
|
("id" = Uuid, Path, description = "Document ID"),
|
||||||
|
("tag_id" = Uuid, Path, description = "Tag ID")
|
||||||
|
),
|
||||||
|
responses((status = 204, description = "Tag removed")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn remove_tag() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/bulk/move",
|
||||||
|
request_body = BulkMoveDocumentsRequest,
|
||||||
|
responses((status = 200, description = "Bulk move outcome", body = BulkMoveDocumentsResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn bulk_move_documents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/bulk/tags",
|
||||||
|
request_body = BulkTagRequest,
|
||||||
|
responses((status = 200, description = "Bulk tag outcome", body = BulkTagResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn bulk_update_tags() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/bulk/correspondents",
|
||||||
|
request_body = BulkCorrespondentsRequest,
|
||||||
|
responses((status = 200, description = "Bulk correspondents outcome", body = BulkCorrespondentsResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn bulk_assign_correspondents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/{id}/correspondents",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
request_body = AssignCorrespondentsRequest,
|
||||||
|
responses((status = 204, description = "Correspondents assigned")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn assign_correspondents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/documents/{id}/correspondents/{correspondent_id}",
|
||||||
|
params(
|
||||||
|
("id" = Uuid, Path, description = "Document ID"),
|
||||||
|
("correspondent_id" = Uuid, Path, description = "Correspondent ID"),
|
||||||
|
RemoveCorrespondentParams
|
||||||
|
),
|
||||||
|
responses((status = 204, description = "Correspondent removed")),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn remove_correspondent() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/bulk/reanalyze",
|
||||||
|
request_body = ReanalyzeRequest,
|
||||||
|
responses((status = 200, description = "Reanalyze queued", body = ReanalyzeResponse)),
|
||||||
|
tag = "Documents"
|
||||||
|
)]
|
||||||
|
pub(super) fn reanalyze_selected_documents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/documents/{id}/assets",
|
||||||
|
params(("id" = Uuid, Path, description = "Document ID")),
|
||||||
|
responses((status = 200, description = "Document assets", body = [DocumentAssetSummary])),
|
||||||
|
tag = "Assets"
|
||||||
|
)]
|
||||||
|
pub(super) fn list_document_assets() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/documents/{id}/assets",
|
||||||
|
params(
|
||||||
|
("id" = Uuid, Path, description = "Document ID"),
|
||||||
|
DocumentAssetRequestParams
|
||||||
|
),
|
||||||
|
responses((status = 202, description = "Asset generation requested")),
|
||||||
|
tag = "Assets"
|
||||||
|
)]
|
||||||
|
pub(super) fn request_document_assets() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/assets/{asset_id}",
|
||||||
|
params(
|
||||||
|
("asset_id" = Uuid, Path, description = "Asset ID"),
|
||||||
|
AssetObjectsQuery
|
||||||
|
),
|
||||||
|
responses((status = 200, description = "Asset detail", body = DocumentAssetDetail)),
|
||||||
|
tag = "Assets"
|
||||||
|
)]
|
||||||
|
pub(super) fn get_document_asset() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/folders",
|
||||||
|
request_body = CreateFolderRequest,
|
||||||
|
responses((status = 200, description = "Folder created", body = FolderResponse)),
|
||||||
|
tag = "Folders"
|
||||||
|
)]
|
||||||
|
pub(super) fn create_folder() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/folders/path",
|
||||||
|
request_body = EnsureFolderPathRequest,
|
||||||
|
responses((status = 200, description = "Folder path ensured", body = FolderResponse)),
|
||||||
|
tag = "Folders"
|
||||||
|
)]
|
||||||
|
pub(super) fn ensure_folder_path() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/folders/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Folder ID")),
|
||||||
|
responses((status = 200, description = "Folder detail", body = FolderResponse)),
|
||||||
|
tag = "Folders"
|
||||||
|
)]
|
||||||
|
pub(super) fn get_folder() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/folders/{id}/contents",
|
||||||
|
params(
|
||||||
|
("id" = Uuid, Path, description = "Folder ID"),
|
||||||
|
FolderContentsParams
|
||||||
|
),
|
||||||
|
responses((status = 200, description = "Folder contents", body = FolderContentsResponse)),
|
||||||
|
tag = "Folders"
|
||||||
|
)]
|
||||||
|
pub(super) fn list_folder_contents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/folders/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Folder ID")),
|
||||||
|
responses((status = 204, description = "Folder deleted")),
|
||||||
|
tag = "Folders"
|
||||||
|
)]
|
||||||
|
pub(super) fn delete_folder() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = "/api/folders/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Folder ID")),
|
||||||
|
request_body = UpdateFolderRequest,
|
||||||
|
responses((status = 204, description = "Folder updated")),
|
||||||
|
tag = "Folders"
|
||||||
|
)]
|
||||||
|
pub(super) fn update_folder() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/tags",
|
||||||
|
responses((status = 200, description = "Tags", body = [TagCatalogEntry])),
|
||||||
|
tag = "Tags"
|
||||||
|
)]
|
||||||
|
pub(super) fn list_tags() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/tags",
|
||||||
|
request_body = CreateTagRequest,
|
||||||
|
responses((status = 200, description = "Tag created", body = TagCatalogEntry)),
|
||||||
|
tag = "Tags"
|
||||||
|
)]
|
||||||
|
pub(super) fn create_tag() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = "/api/tags/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Tag ID")),
|
||||||
|
request_body = UpdateTagRequest,
|
||||||
|
responses((status = 200, description = "Tag updated", body = TagCatalogEntry)),
|
||||||
|
tag = "Tags"
|
||||||
|
)]
|
||||||
|
pub(super) fn update_tag() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/tags/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Tag ID")),
|
||||||
|
responses((status = 204, description = "Tag deleted")),
|
||||||
|
tag = "Tags"
|
||||||
|
)]
|
||||||
|
pub(super) fn delete_tag() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
get,
|
||||||
|
path = "/api/correspondents",
|
||||||
|
responses((status = 200, description = "Correspondents", body = [CorrespondentCatalogEntry])),
|
||||||
|
tag = "Correspondents"
|
||||||
|
)]
|
||||||
|
pub(super) fn list_correspondents() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
post,
|
||||||
|
path = "/api/correspondents",
|
||||||
|
request_body = CreateCorrespondentRequest,
|
||||||
|
responses((status = 200, description = "Correspondent created", body = CorrespondentCatalogEntry)),
|
||||||
|
tag = "Correspondents"
|
||||||
|
)]
|
||||||
|
pub(super) fn create_correspondent() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
patch,
|
||||||
|
path = "/api/correspondents/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Correspondent ID")),
|
||||||
|
request_body = UpdateCorrespondentRequest,
|
||||||
|
responses((status = 200, description = "Correspondent updated", body = CorrespondentCatalogEntry)),
|
||||||
|
tag = "Correspondents"
|
||||||
|
)]
|
||||||
|
pub(super) fn update_correspondent() {}
|
||||||
|
|
||||||
|
#[utoipa::path(
|
||||||
|
delete,
|
||||||
|
path = "/api/correspondents/{id}",
|
||||||
|
params(("id" = Uuid, Path, description = "Correspondent ID")),
|
||||||
|
responses((status = 204, description = "Correspondent deleted")),
|
||||||
|
tag = "Correspondents"
|
||||||
|
)]
|
||||||
|
pub(super) fn delete_correspondent() {}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::ApiDoc;
|
||||||
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn openapi_serializes() {
|
||||||
|
let spec = ApiDoc::openapi();
|
||||||
|
let _ = serde_json::to_string(&spec).expect("serialize openapi");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod schemas {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct LoginRequest {
|
||||||
|
pub username: String,
|
||||||
|
pub password: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub preferred_tenant_slug: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct AccessTokenResponse {
|
||||||
|
pub access_token: String,
|
||||||
|
pub token_type: String,
|
||||||
|
pub expires_in: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct TenantSummary {
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
pub slug: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct TenantSelectionResponse {
|
||||||
|
pub selection_token: String,
|
||||||
|
pub tenants: Vec<TenantSummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct TenantSelectionRequest {
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum LoginResponseVariants {
|
||||||
|
Token(AccessTokenResponse),
|
||||||
|
Selection(TenantSelectionResponse),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
|
||||||
|
#[into_params(parameter_in = Query)]
|
||||||
|
pub struct DocumentListQuery {
|
||||||
|
pub folder_id: Option<Uuid>,
|
||||||
|
pub include_deleted: Option<bool>,
|
||||||
|
pub include_descendants: Option<bool>,
|
||||||
|
pub query: Option<String>,
|
||||||
|
pub tags: Option<String>,
|
||||||
|
pub correspondents: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentTag {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub label: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub color: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentAssetObject {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub ordinal: i32,
|
||||||
|
pub metadata: Value,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub url: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub expires_at: Option<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentAssetSummary {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub asset_type: String,
|
||||||
|
pub mime_type: String,
|
||||||
|
pub metadata: Value,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub cardinality: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentAssetDetail {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub asset_type: String,
|
||||||
|
pub mime_type: String,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub created_at: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub cardinality: Option<i32>,
|
||||||
|
pub objects: Vec<DocumentAssetObject>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentVersion {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub version_number: i32,
|
||||||
|
pub checksum: String,
|
||||||
|
pub size_bytes: i64,
|
||||||
|
pub created_at: String,
|
||||||
|
pub metadata: Value,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub operations_summary: Option<Value>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub assets: Option<Vec<DocumentAssetSummary>>,
|
||||||
|
pub download_path: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentCorrespondent {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub role: String,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub assigned_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentResponse {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub filename: String,
|
||||||
|
pub title: String,
|
||||||
|
pub original_name: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub content_type: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub folder_id: Option<Uuid>,
|
||||||
|
pub uploaded_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub deleted_at: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub issued_at: Option<String>,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub tags: Vec<DocumentTag>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub correspondents: Option<Vec<DocumentCorrespondent>>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub current_version: Option<DocumentVersion>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentDetailResponse {
|
||||||
|
pub document: DocumentResponse,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentDownloadResponse {
|
||||||
|
pub url: String,
|
||||||
|
pub expires_in: u64,
|
||||||
|
pub filename: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub content_type: Option<String>,
|
||||||
|
pub size_bytes: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct UpdateDocumentRequest {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub title: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct BulkMoveDocumentsRequest {
|
||||||
|
pub document_ids: Vec<Uuid>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub folder_id: Option<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct BulkMoveDocumentsResponse {
|
||||||
|
pub updated: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct AssignTagsRequest {
|
||||||
|
pub tag_ids: Vec<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct MoveDocumentRequest {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub folder_id: Option<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[serde(rename_all = "snake_case")]
|
||||||
|
pub enum BulkTagAction {
|
||||||
|
Add,
|
||||||
|
Remove,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct BulkTagRequest {
|
||||||
|
pub document_ids: Vec<Uuid>,
|
||||||
|
pub tag_ids: Vec<Uuid>,
|
||||||
|
pub action: BulkTagAction,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct BulkTagResponse {
|
||||||
|
pub added: usize,
|
||||||
|
pub removed: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct CorrespondentAssignment {
|
||||||
|
pub correspondent_id: Uuid,
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct AssignCorrespondentsRequest {
|
||||||
|
pub assignments: Vec<CorrespondentAssignment>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub replace: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum BulkCorrespondentAction {
|
||||||
|
Add,
|
||||||
|
Remove,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct BulkCorrespondentsRequest {
|
||||||
|
pub document_ids: Vec<Uuid>,
|
||||||
|
pub assignments: Vec<CorrespondentAssignment>,
|
||||||
|
#[serde(default = "default_bulk_correspondent_action")]
|
||||||
|
pub action: BulkCorrespondentAction,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_bulk_correspondent_action() -> BulkCorrespondentAction {
|
||||||
|
BulkCorrespondentAction::Add
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct BulkCorrespondentsResponse {
|
||||||
|
pub assigned: usize,
|
||||||
|
pub removed: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
|
||||||
|
#[into_params(parameter_in = Query)]
|
||||||
|
pub struct RemoveCorrespondentParams {
|
||||||
|
pub role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct ReanalyzeRequest {
|
||||||
|
pub document_ids: Vec<Uuid>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub force: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct ReanalyzeResponse {
|
||||||
|
pub queued: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
|
||||||
|
#[into_params(parameter_in = Query)]
|
||||||
|
pub struct DocumentAssetRequestParams {
|
||||||
|
#[serde(default)]
|
||||||
|
pub force: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
|
||||||
|
#[into_params(parameter_in = Query)]
|
||||||
|
pub struct AssetObjectsQuery {
|
||||||
|
#[serde(default)]
|
||||||
|
pub start: Option<i32>,
|
||||||
|
#[serde(default)]
|
||||||
|
pub limit: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
|
||||||
|
#[into_params(parameter_in = Query)]
|
||||||
|
pub struct DocumentCheckQuery {
|
||||||
|
pub checksum: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct DocumentCheckResponse {
|
||||||
|
pub exists: bool,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub document_id: Option<Uuid>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub title: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub filename: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub version_id: Option<Uuid>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub version_number: Option<i32>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub uploaded_at: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct UploadDocumentForm {
|
||||||
|
#[schema(value_type = String, format = Binary)]
|
||||||
|
pub file: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub folder_id: Option<Uuid>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub metadata: Option<Value>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub title: Option<String>,
|
||||||
|
#[schema(nullable, value_type = Vec<Uuid>)]
|
||||||
|
pub tag_ids: Option<Vec<Uuid>>,
|
||||||
|
#[schema(nullable, value_type = Vec<CorrespondentAssignment>)]
|
||||||
|
pub correspondents: Option<Vec<CorrespondentAssignment>>,
|
||||||
|
#[schema(nullable, example = "2024-01-01T00:00:00Z")]
|
||||||
|
pub issued_at: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub skip_existing: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct CreateFolderRequest {
|
||||||
|
pub name: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub parent_id: Option<Uuid>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct EnsureFolderPathRequest {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub parent_id: Option<Uuid>,
|
||||||
|
pub segments: Vec<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct FolderInfo {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub parent_id: Option<Uuid>,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct FolderResponse {
|
||||||
|
pub folder: FolderInfo,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, IntoParams, ToSchema)]
|
||||||
|
#[into_params(parameter_in = Query)]
|
||||||
|
pub struct FolderContentsParams {
|
||||||
|
#[serde(default)]
|
||||||
|
pub include_documents: Option<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct FolderDocumentSummary {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct FolderContentsResponse {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub folder: Option<FolderInfo>,
|
||||||
|
pub subfolders: Vec<FolderInfo>,
|
||||||
|
pub documents: Vec<FolderDocumentSummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct UpdateFolderRequest {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub parent_id: Option<Option<Uuid>>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct TagCatalogEntry {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub label: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub color: Option<String>,
|
||||||
|
pub usage_count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct CreateTagRequest {
|
||||||
|
pub label: String,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub color: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct UpdateTagRequest {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub label: Option<Option<String>>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub color: Option<Option<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct CorrespondentCatalogEntry {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub role_counts: Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct CreateCorrespondentRequest {
|
||||||
|
pub name: String,
|
||||||
|
#[schema(default, value_type = Object)]
|
||||||
|
pub metadata: Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, ToSchema)]
|
||||||
|
pub struct UpdateCorrespondentRequest {
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub name: Option<String>,
|
||||||
|
#[schema(nullable)]
|
||||||
|
pub metadata: Option<Value>,
|
||||||
|
}
|
||||||
|
}
|
||||||
+301
-15
@@ -1,19 +1,41 @@
|
|||||||
use axum::{extract::State, http::StatusCode, response::IntoResponse, Json};
|
use axum::{
|
||||||
use diesel::prelude::*;
|
extract::State,
|
||||||
|
http::{header::SET_COOKIE, HeaderMap, HeaderValue, StatusCode},
|
||||||
|
response::{IntoResponse, Response},
|
||||||
|
Json,
|
||||||
|
};
|
||||||
|
use axum_extra::{
|
||||||
|
headers::{authorization::Bearer, Authorization, Cookie},
|
||||||
|
typed_header::TypedHeader,
|
||||||
|
};
|
||||||
|
use chrono::{Duration as ChronoDuration, Utc};
|
||||||
|
use diesel::{pg::PgConnection, prelude::*};
|
||||||
|
use rand::{rngs::OsRng, RngCore};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sha2::{Digest, Sha256};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::{password, AuthenticatedUser},
|
auth::{password, AuthenticatedUser},
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
models::User,
|
models::{NewRefreshToken, RefreshToken, Tenant, User, UserMembership},
|
||||||
schema::users::dsl,
|
schema::{
|
||||||
|
refresh_tokens, tenants::dsl as tenant_dsl, user_memberships::dsl as memberships_dsl,
|
||||||
|
users::dsl,
|
||||||
|
},
|
||||||
state::AppState,
|
state::AppState,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::schema::refresh_tokens::dsl as refresh_dsl;
|
||||||
|
|
||||||
|
const REFRESH_COOKIE_NAME: &str = "refresh_token";
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct LoginRequest {
|
pub struct LoginRequest {
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub preferred_tenant_slug: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -23,11 +45,28 @@ pub struct LoginResponse {
|
|||||||
pub expires_in: i64,
|
pub expires_in: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct TenantSummary {
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
pub slug: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct TenantSelectionResponse {
|
||||||
|
pub selection_token: String,
|
||||||
|
pub tenants: Vec<TenantSummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct TenantSelectionRequest {
|
||||||
|
pub tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn login(
|
pub async fn login(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Json(payload): Json<LoginRequest>,
|
Json(payload): Json<LoginRequest>,
|
||||||
) -> AppResult<Json<LoginResponse>> {
|
) -> AppResult<Response> {
|
||||||
let mut conn = state.db()?;
|
let mut conn = state.db_unscoped()?;
|
||||||
|
|
||||||
let user: User = dsl::users
|
let user: User = dsl::users
|
||||||
.filter(dsl::username.eq(&payload.username))
|
.filter(dsl::username.eq(&payload.username))
|
||||||
@@ -40,22 +79,269 @@ pub async fn login(
|
|||||||
return Err(AppError::unauthorized());
|
return Err(AppError::unauthorized());
|
||||||
}
|
}
|
||||||
|
|
||||||
let token = state
|
let memberships: Vec<(UserMembership, Tenant)> = memberships_dsl::user_memberships
|
||||||
|
.inner_join(tenant_dsl::tenants)
|
||||||
|
.filter(memberships_dsl::user_id.eq(user.id))
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
if memberships.is_empty() {
|
||||||
|
return Err(AppError::unauthorized());
|
||||||
|
}
|
||||||
|
|
||||||
|
let preferred_slug = payload
|
||||||
|
.preferred_tenant_slug
|
||||||
|
.as_ref()
|
||||||
|
.map(|slug| slug.trim().to_string())
|
||||||
|
.filter(|slug| !slug.is_empty());
|
||||||
|
|
||||||
|
if let Some(tenant) = preferred_slug.as_ref().and_then(|slug| {
|
||||||
|
memberships
|
||||||
|
.iter()
|
||||||
|
.find(|(_, tenant)| tenant.slug.eq_ignore_ascii_case(slug))
|
||||||
|
}) {
|
||||||
|
return issue_session(&state, &mut conn, &user, tenant.1.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if memberships.len() == 1 {
|
||||||
|
let tenant_id = memberships[0].1.id;
|
||||||
|
return issue_session(&state, &mut conn, &user, tenant_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
let selection_token = state
|
||||||
.jwt
|
.jwt
|
||||||
.generate_token(user.id, &user.username, &user.role)
|
.generate_tenant_selector_token(user.id)
|
||||||
.map_err(AppError::from)?;
|
.map_err(AppError::from)?;
|
||||||
|
|
||||||
Ok(Json(LoginResponse {
|
let tenants = memberships
|
||||||
access_token: token,
|
.into_iter()
|
||||||
token_type: "Bearer".to_string(),
|
.map(|(_, tenant)| TenantSummary {
|
||||||
expires_in: state.config.jwt_expiry_minutes * 60,
|
tenant_id: tenant.id,
|
||||||
}))
|
slug: tenant.slug,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let response = Json(TenantSelectionResponse {
|
||||||
|
selection_token,
|
||||||
|
tenants,
|
||||||
|
})
|
||||||
|
.into_response();
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn logout(_user: AuthenticatedUser) -> impl IntoResponse {
|
pub async fn refresh(
|
||||||
StatusCode::NO_CONTENT
|
State(state): State<AppState>,
|
||||||
|
jar: Option<TypedHeader<Cookie>>,
|
||||||
|
) -> AppResult<Response> {
|
||||||
|
let cookies = jar.ok_or_else(AppError::unauthorized)?;
|
||||||
|
let refresh_value = cookies
|
||||||
|
.get(REFRESH_COOKIE_NAME)
|
||||||
|
.ok_or_else(AppError::unauthorized)?;
|
||||||
|
|
||||||
|
let hashed = hash_refresh_token(refresh_value);
|
||||||
|
let mut conn = state.db_unscoped()?;
|
||||||
|
let now = Utc::now();
|
||||||
|
let now_naive = now.naive_utc();
|
||||||
|
|
||||||
|
let token = match refresh_dsl::refresh_tokens
|
||||||
|
.filter(refresh_dsl::token_hash.eq(&hashed))
|
||||||
|
.filter(refresh_dsl::revoked_at.is_null())
|
||||||
|
.filter(refresh_dsl::expires_at.gt(now_naive))
|
||||||
|
.first::<RefreshToken>(&mut conn)
|
||||||
|
{
|
||||||
|
Ok(token) => token,
|
||||||
|
Err(diesel::result::Error::NotFound) => return Err(AppError::unauthorized()),
|
||||||
|
Err(err) => return Err(AppError::from(err)),
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::update(refresh_dsl::refresh_tokens.filter(refresh_dsl::id.eq(token.id)))
|
||||||
|
.set((
|
||||||
|
refresh_dsl::revoked_at.eq(now_naive),
|
||||||
|
refresh_dsl::updated_at.eq(now_naive),
|
||||||
|
))
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
let user: User = dsl::users
|
||||||
|
.find(token.user_id)
|
||||||
|
.first(&mut conn)
|
||||||
|
.map_err(AppError::from)?;
|
||||||
|
|
||||||
|
issue_session(&state, &mut conn, &user, token.tenant_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn select_tenant(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
TypedHeader(Authorization(bearer)): TypedHeader<Authorization<Bearer>>,
|
||||||
|
Json(payload): Json<TenantSelectionRequest>,
|
||||||
|
) -> AppResult<Response> {
|
||||||
|
let claims = state
|
||||||
|
.jwt
|
||||||
|
.verify_tenant_selector_token(bearer.token())
|
||||||
|
.map_err(|_| AppError::unauthorized())?;
|
||||||
|
|
||||||
|
let mut conn = state.db_unscoped()?;
|
||||||
|
|
||||||
|
let membership_exists = memberships_dsl::user_memberships
|
||||||
|
.filter(memberships_dsl::user_id.eq(claims.sub))
|
||||||
|
.filter(memberships_dsl::tenant_id.eq(payload.tenant_id))
|
||||||
|
.inner_join(tenant_dsl::tenants)
|
||||||
|
.select(memberships_dsl::id)
|
||||||
|
.first::<Uuid>(&mut conn)
|
||||||
|
.optional()?;
|
||||||
|
|
||||||
|
if membership_exists.is_none() {
|
||||||
|
return Err(AppError::unauthorized());
|
||||||
|
}
|
||||||
|
|
||||||
|
let user: User = dsl::users
|
||||||
|
.find(claims.sub)
|
||||||
|
.first(&mut conn)
|
||||||
|
.map_err(AppError::from)?;
|
||||||
|
|
||||||
|
issue_session(&state, &mut conn, &user, payload.tenant_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn logout(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
user: AuthenticatedUser,
|
||||||
|
jar: Option<TypedHeader<Cookie>>,
|
||||||
|
) -> AppResult<(HeaderMap, StatusCode)> {
|
||||||
|
let mut conn = state.db_unscoped()?;
|
||||||
|
let now = Utc::now().naive_utc();
|
||||||
|
let mut rows_affected = 0;
|
||||||
|
|
||||||
|
if let Some(cookies) = jar {
|
||||||
|
if let Some(value) = cookies.get(REFRESH_COOKIE_NAME) {
|
||||||
|
let hashed = hash_refresh_token(value);
|
||||||
|
rows_affected = diesel::update(
|
||||||
|
refresh_dsl::refresh_tokens
|
||||||
|
.filter(refresh_dsl::token_hash.eq(hashed))
|
||||||
|
.filter(refresh_dsl::user_id.eq(user.user_id))
|
||||||
|
.filter(refresh_dsl::revoked_at.is_null()),
|
||||||
|
)
|
||||||
|
.set((
|
||||||
|
refresh_dsl::revoked_at.eq(now),
|
||||||
|
refresh_dsl::updated_at.eq(now),
|
||||||
|
))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.unwrap_or(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if rows_affected == 0 {
|
||||||
|
let _ = diesel::update(
|
||||||
|
refresh_dsl::refresh_tokens
|
||||||
|
.filter(refresh_dsl::user_id.eq(user.user_id))
|
||||||
|
.filter(refresh_dsl::revoked_at.is_null()),
|
||||||
|
)
|
||||||
|
.set((
|
||||||
|
refresh_dsl::revoked_at.eq(now),
|
||||||
|
refresh_dsl::updated_at.eq(now),
|
||||||
|
))
|
||||||
|
.execute(&mut conn);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
headers.insert(SET_COOKIE, build_clear_refresh_cookie(&state));
|
||||||
|
Ok((headers, StatusCode::NO_CONTENT))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn me(user: AuthenticatedUser) -> Json<AuthenticatedUser> {
|
pub async fn me(user: AuthenticatedUser) -> Json<AuthenticatedUser> {
|
||||||
Json(user)
|
Json(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn issue_session(
|
||||||
|
state: &AppState,
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
user: &User,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
) -> AppResult<Response> {
|
||||||
|
let now = Utc::now();
|
||||||
|
let access_token = state
|
||||||
|
.jwt
|
||||||
|
.generate_token(user.id, tenant_id, &user.username)
|
||||||
|
.map_err(AppError::from)?;
|
||||||
|
|
||||||
|
let refresh_value = generate_refresh_token();
|
||||||
|
let refresh_hash = hash_refresh_token(&refresh_value);
|
||||||
|
let refresh_expires_at = now + ChronoDuration::days(state.config.refresh_token_expiry_days);
|
||||||
|
|
||||||
|
let new_refresh = NewRefreshToken {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
user_id: user.id,
|
||||||
|
token_hash: refresh_hash,
|
||||||
|
issued_at: now.naive_utc(),
|
||||||
|
expires_at: refresh_expires_at.naive_utc(),
|
||||||
|
tenant_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(refresh_tokens::table)
|
||||||
|
.values(&new_refresh)
|
||||||
|
.execute(conn)?;
|
||||||
|
|
||||||
|
let mut response = Json(LoginResponse {
|
||||||
|
access_token,
|
||||||
|
token_type: "Bearer".to_string(),
|
||||||
|
expires_in: state.config.jwt_expiry_minutes * 60,
|
||||||
|
})
|
||||||
|
.into_response();
|
||||||
|
|
||||||
|
response.headers_mut().insert(
|
||||||
|
SET_COOKIE,
|
||||||
|
build_refresh_cookie(state, &refresh_value, refresh_expires_at),
|
||||||
|
);
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn hash_refresh_token(token: &str) -> String {
|
||||||
|
let mut hasher = Sha256::new();
|
||||||
|
hasher.update(token.as_bytes());
|
||||||
|
hex::encode(hasher.finalize())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_refresh_token() -> String {
|
||||||
|
let mut bytes = [0u8; 32];
|
||||||
|
OsRng.fill_bytes(&mut bytes);
|
||||||
|
hex::encode(bytes)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_refresh_cookie(
|
||||||
|
state: &AppState,
|
||||||
|
token: &str,
|
||||||
|
expires_at: chrono::DateTime<Utc>,
|
||||||
|
) -> HeaderValue {
|
||||||
|
let max_age = ChronoDuration::days(state.config.refresh_token_expiry_days).num_seconds();
|
||||||
|
|
||||||
|
let mut parts = vec![format!("{}={}", REFRESH_COOKIE_NAME, token)];
|
||||||
|
parts.push("Path=/".into());
|
||||||
|
parts.push("HttpOnly".into());
|
||||||
|
parts.push("SameSite=Strict".into());
|
||||||
|
parts.push(format!("Max-Age={}", max_age));
|
||||||
|
parts.push(format!("Expires={}", expires_at.to_rfc2822()));
|
||||||
|
if state.config.refresh_cookie_secure {
|
||||||
|
parts.push("Secure".into());
|
||||||
|
}
|
||||||
|
if let Some(domain) = &state.config.refresh_cookie_domain {
|
||||||
|
parts.push(format!("Domain={}", domain));
|
||||||
|
}
|
||||||
|
|
||||||
|
HeaderValue::from_str(&parts.join("; ")).expect("valid refresh cookie")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_clear_refresh_cookie(state: &AppState) -> HeaderValue {
|
||||||
|
let mut parts = vec![format!("{}=", REFRESH_COOKIE_NAME)];
|
||||||
|
parts.push("Path=/".into());
|
||||||
|
parts.push("HttpOnly".into());
|
||||||
|
parts.push("SameSite=Strict".into());
|
||||||
|
parts.push("Max-Age=0".into());
|
||||||
|
parts.push("Expires=Thu, 01 Jan 1970 00:00:00 GMT".into());
|
||||||
|
if state.config.refresh_cookie_secure {
|
||||||
|
parts.push("Secure".into());
|
||||||
|
}
|
||||||
|
if let Some(domain) = &state.config.refresh_cookie_domain {
|
||||||
|
parts.push(format!("Domain={}", domain));
|
||||||
|
}
|
||||||
|
|
||||||
|
HeaderValue::from_str(&parts.join("; ")).expect("valid refresh cookie")
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,290 @@
|
|||||||
|
use std::collections::{BTreeMap, HashMap};
|
||||||
|
|
||||||
|
use axum::{extract::Path, http::StatusCode, Json};
|
||||||
|
use chrono::Utc;
|
||||||
|
use diesel::{dsl::count_star, prelude::*, result::DatabaseErrorKind, PgConnection};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
auth::TenantScopedConn,
|
||||||
|
error::{AppError, AppResult},
|
||||||
|
models::{Correspondent, NewCorrespondent},
|
||||||
|
schema::{correspondents, document_correspondents},
|
||||||
|
utils::{
|
||||||
|
db::{no_content, EnsureEntity, IntoJsonResponse},
|
||||||
|
time::to_iso,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct CorrespondentUsage {
|
||||||
|
pub total: i64,
|
||||||
|
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
|
||||||
|
pub by_role: BTreeMap<String, i64>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct CorrespondentSummary {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub metadata: Value,
|
||||||
|
pub created_at: String,
|
||||||
|
pub updated_at: String,
|
||||||
|
pub usage: CorrespondentUsage,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateCorrespondentRequest {
|
||||||
|
pub name: String,
|
||||||
|
#[serde(default)]
|
||||||
|
pub metadata: Option<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct UpdateCorrespondentRequest {
|
||||||
|
pub name: Option<String>,
|
||||||
|
pub metadata: Option<Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(AsChangeset, Default)]
|
||||||
|
#[diesel(table_name = correspondents)]
|
||||||
|
struct CorrespondentChangeset<'a> {
|
||||||
|
name: Option<&'a str>,
|
||||||
|
metadata: Option<&'a Value>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_correspondents(
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
) -> AppResult<Json<Vec<CorrespondentSummary>>> {
|
||||||
|
let correspondents_list: Vec<Correspondent> = correspondents::table
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.order(correspondents::name.asc())
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
let usage_rows: Vec<(Uuid, String, i64)> = document_correspondents::table
|
||||||
|
.filter(document_correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.group_by((
|
||||||
|
document_correspondents::correspondent_id,
|
||||||
|
document_correspondents::role,
|
||||||
|
))
|
||||||
|
.select((
|
||||||
|
document_correspondents::correspondent_id,
|
||||||
|
document_correspondents::role,
|
||||||
|
count_star(),
|
||||||
|
))
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
let mut usage_map: HashMap<Uuid, BTreeMap<String, i64>> = HashMap::new();
|
||||||
|
for (correspondent_id, role, count) in usage_rows {
|
||||||
|
usage_map
|
||||||
|
.entry(correspondent_id)
|
||||||
|
.or_default()
|
||||||
|
.insert(role, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut response = Vec::with_capacity(correspondents_list.len());
|
||||||
|
for correspondent in correspondents_list {
|
||||||
|
let role_counts = usage_map.remove(&correspondent.id).unwrap_or_default();
|
||||||
|
response.push(build_summary(correspondent, role_counts));
|
||||||
|
}
|
||||||
|
|
||||||
|
response.into_json()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn create_correspondent(
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
Json(payload): Json<CreateCorrespondentRequest>,
|
||||||
|
) -> AppResult<Json<CorrespondentSummary>> {
|
||||||
|
let name = payload.name.trim();
|
||||||
|
if name.is_empty() {
|
||||||
|
return Err(AppError::bad_request("name must not be empty"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let metadata_value = normalize_metadata(payload.metadata);
|
||||||
|
let new_id = Uuid::new_v4();
|
||||||
|
let new_correspondent = NewCorrespondent {
|
||||||
|
id: new_id,
|
||||||
|
name: name.to_string(),
|
||||||
|
metadata: metadata_value,
|
||||||
|
tenant_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
match diesel::insert_into(correspondents::table)
|
||||||
|
.values(&new_correspondent)
|
||||||
|
.execute(&mut conn)
|
||||||
|
{
|
||||||
|
Ok(_) => {}
|
||||||
|
Err(diesel::result::Error::DatabaseError(DatabaseErrorKind::UniqueViolation, _)) => {
|
||||||
|
return Err(AppError::bad_request("correspondent name already exists"));
|
||||||
|
}
|
||||||
|
Err(err) => return Err(AppError::from(err)),
|
||||||
|
}
|
||||||
|
|
||||||
|
let correspondent: Correspondent = correspondents::table
|
||||||
|
.find(new_id)
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.one()?;
|
||||||
|
|
||||||
|
build_summary(correspondent, BTreeMap::new()).into_json()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_correspondent(
|
||||||
|
Path(correspondent_id): Path<Uuid>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
Json(payload): Json<UpdateCorrespondentRequest>,
|
||||||
|
) -> AppResult<Json<CorrespondentSummary>> {
|
||||||
|
let existing: Correspondent = correspondents::table
|
||||||
|
.find(correspondent_id)
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.one()?;
|
||||||
|
|
||||||
|
let mut new_name: Option<String> = None;
|
||||||
|
if let Some(ref candidate) = payload.name {
|
||||||
|
let trimmed = candidate.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(AppError::bad_request("name must not be empty"));
|
||||||
|
}
|
||||||
|
if trimmed != existing.name {
|
||||||
|
let duplicate = correspondents::table
|
||||||
|
.filter(correspondents::name.eq(trimmed))
|
||||||
|
.filter(correspondents::id.ne(correspondent_id))
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.first::<Correspondent>(&mut conn)
|
||||||
|
.optional()?;
|
||||||
|
if duplicate.is_some() {
|
||||||
|
return Err(AppError::bad_request("correspondent name already exists"));
|
||||||
|
}
|
||||||
|
new_name = Some(trimmed.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_metadata: Option<Value> = None;
|
||||||
|
if let Some(metadata) = payload.metadata.clone() {
|
||||||
|
let candidate = normalize_metadata(Some(metadata));
|
||||||
|
if candidate != existing.metadata {
|
||||||
|
new_metadata = Some(candidate);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if new_name.is_none() && new_metadata.is_none() {
|
||||||
|
let usage = load_usage_for_correspondent(&mut conn, tenant_id, correspondent_id)?;
|
||||||
|
return build_summary(existing.clone(), usage).into_json();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut changeset = CorrespondentChangeset::default();
|
||||||
|
if let Some(ref name) = new_name {
|
||||||
|
changeset.name = Some(name.as_str());
|
||||||
|
}
|
||||||
|
if let Some(ref metadata) = new_metadata {
|
||||||
|
changeset.metadata = Some(metadata);
|
||||||
|
}
|
||||||
|
|
||||||
|
let now = Utc::now().naive_utc();
|
||||||
|
diesel::update(
|
||||||
|
correspondents::table
|
||||||
|
.find(correspondent_id)
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.set((&changeset, correspondents::updated_at.eq(now)))
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
let updated: Correspondent = correspondents::table
|
||||||
|
.find(correspondent_id)
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.one()?;
|
||||||
|
let usage = load_usage_for_correspondent(&mut conn, tenant_id, correspondent_id)?;
|
||||||
|
build_summary(updated, usage).into_json()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_correspondent(
|
||||||
|
Path(correspondent_id): Path<Uuid>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
) -> AppResult<StatusCode> {
|
||||||
|
let usage: i64 = document_correspondents::table
|
||||||
|
.filter(document_correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.filter(document_correspondents::correspondent_id.eq(correspondent_id))
|
||||||
|
.select(count_star())
|
||||||
|
.first(&mut conn)?;
|
||||||
|
|
||||||
|
if usage > 0 {
|
||||||
|
return Err(AppError::bad_request(
|
||||||
|
"cannot delete correspondent that is still assigned to documents",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let deleted = diesel::delete(
|
||||||
|
correspondents::table
|
||||||
|
.filter(correspondents::id.eq(correspondent_id))
|
||||||
|
.filter(correspondents::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
if deleted == 0 {
|
||||||
|
return Err(AppError::not_found());
|
||||||
|
}
|
||||||
|
no_content()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_summary(
|
||||||
|
correspondent: Correspondent,
|
||||||
|
role_counts: BTreeMap<String, i64>,
|
||||||
|
) -> CorrespondentSummary {
|
||||||
|
let total = role_counts.values().copied().sum();
|
||||||
|
CorrespondentSummary {
|
||||||
|
id: correspondent.id,
|
||||||
|
name: correspondent.name,
|
||||||
|
metadata: correspondent.metadata,
|
||||||
|
created_at: to_iso(correspondent.created_at),
|
||||||
|
updated_at: to_iso(correspondent.updated_at),
|
||||||
|
usage: CorrespondentUsage {
|
||||||
|
total,
|
||||||
|
by_role: role_counts,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn normalize_metadata(input: Option<Value>) -> Value {
|
||||||
|
match input {
|
||||||
|
None | Some(Value::Null) => Value::Object(Default::default()),
|
||||||
|
Some(value) => value,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_usage_for_correspondent(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
correspondent_id: Uuid,
|
||||||
|
) -> AppResult<BTreeMap<String, i64>> {
|
||||||
|
let rows: Vec<(String, i64)> = document_correspondents::table
|
||||||
|
.filter(document_correspondents::correspondent_id.eq(correspondent_id))
|
||||||
|
.filter(document_correspondents::tenant_id.eq(tenant_id))
|
||||||
|
.group_by(document_correspondents::role)
|
||||||
|
.select((document_correspondents::role, count_star()))
|
||||||
|
.load(conn)?;
|
||||||
|
|
||||||
|
let mut map = BTreeMap::new();
|
||||||
|
for (role, count) in rows {
|
||||||
|
map.insert(role, count);
|
||||||
|
}
|
||||||
|
Ok(map)
|
||||||
|
}
|
||||||
+1711
-383
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,120 @@
|
|||||||
|
use std::path::Path as FsPath;
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::error::{AppError, AppResult};
|
||||||
|
use crate::models::{Document, DocumentAsset, DocumentAssetObject, DocumentVersion};
|
||||||
|
use crate::state::AppState;
|
||||||
|
use crate::utils::time::to_iso;
|
||||||
|
|
||||||
|
use super::{
|
||||||
|
DocumentAssetDetailResponse, DocumentAssetObjectResponse, DocumentAssetResponse,
|
||||||
|
DocumentVersionResponse,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub fn build_download_path(
|
||||||
|
state: &AppState,
|
||||||
|
document: &Document,
|
||||||
|
user_id: Uuid,
|
||||||
|
) -> AppResult<String> {
|
||||||
|
state
|
||||||
|
.jwt
|
||||||
|
.generate_download_token(document.id, user_id, document.tenant_id)
|
||||||
|
.map(|token| format!("/download/{token}"))
|
||||||
|
.map_err(|err| AppError::internal(format!("failed to generate download token: {err}")))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_version_response(
|
||||||
|
version: DocumentVersion,
|
||||||
|
include_operations_summary: bool,
|
||||||
|
) -> DocumentVersionResponse {
|
||||||
|
DocumentVersionResponse {
|
||||||
|
id: version.id,
|
||||||
|
version_number: version.version_number,
|
||||||
|
s3_key: version.s3_key,
|
||||||
|
size_bytes: version.size_bytes,
|
||||||
|
checksum: version.checksum,
|
||||||
|
created_at: to_iso(version.created_at),
|
||||||
|
metadata: version.metadata,
|
||||||
|
operations_summary: if include_operations_summary {
|
||||||
|
Some(version.operations_summary)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_asset_summary(asset: DocumentAsset) -> DocumentAssetResponse {
|
||||||
|
DocumentAssetResponse {
|
||||||
|
id: asset.id,
|
||||||
|
asset_type: asset.asset_type,
|
||||||
|
mime_type: asset.mime_type,
|
||||||
|
metadata: asset.metadata,
|
||||||
|
cardinality: asset.cardinality,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_asset_detail_response(
|
||||||
|
asset: DocumentAsset,
|
||||||
|
objects: Vec<DocumentAssetObjectResponse>,
|
||||||
|
) -> DocumentAssetDetailResponse {
|
||||||
|
DocumentAssetDetailResponse {
|
||||||
|
id: asset.id,
|
||||||
|
asset_type: asset.asset_type,
|
||||||
|
mime_type: asset.mime_type,
|
||||||
|
metadata: asset.metadata,
|
||||||
|
created_at: to_iso(asset.created_at),
|
||||||
|
cardinality: asset.cardinality,
|
||||||
|
objects,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn to_asset_object_response(
|
||||||
|
object: DocumentAssetObject,
|
||||||
|
url: Option<String>,
|
||||||
|
expires_at: Option<i64>,
|
||||||
|
) -> DocumentAssetObjectResponse {
|
||||||
|
DocumentAssetObjectResponse {
|
||||||
|
id: object.id,
|
||||||
|
ordinal: object.ordinal,
|
||||||
|
metadata: object.metadata,
|
||||||
|
url,
|
||||||
|
expires_at,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn derive_document_title(original: &str) -> String {
|
||||||
|
let trimmed = original.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return "Document".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let stem = FsPath::new(trimmed)
|
||||||
|
.file_stem()
|
||||||
|
.and_then(|s| s.to_str())
|
||||||
|
.map(|s| s.trim())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
.map(|s| s.to_string());
|
||||||
|
|
||||||
|
stem.unwrap_or_else(|| trimmed.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn filename_with_retained_extension(title: &str, current_filename: &str) -> String {
|
||||||
|
let extension = FsPath::new(current_filename)
|
||||||
|
.extension()
|
||||||
|
.and_then(|ext| ext.to_str());
|
||||||
|
|
||||||
|
if let Some(ext) = extension {
|
||||||
|
if title
|
||||||
|
.rsplit_once('.')
|
||||||
|
.map(|(_, existing_ext)| existing_ext.eq_ignore_ascii_case(ext))
|
||||||
|
.unwrap_or(false)
|
||||||
|
{
|
||||||
|
title.to_string()
|
||||||
|
} else {
|
||||||
|
format!("{title}.{ext}")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
title.to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
use std::collections::HashSet;
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::error::{AppError, AppResult};
|
||||||
|
|
||||||
|
use super::CorrespondentAssignmentInput;
|
||||||
|
|
||||||
|
pub const CORRESPONDENT_ROLES: &[&str] = &["sender", "receiver", "other"];
|
||||||
|
|
||||||
|
pub fn normalize_role(value: &str) -> String {
|
||||||
|
value.trim().to_lowercase()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_valid_correspondent_role(role: &str) -> bool {
|
||||||
|
CORRESPONDENT_ROLES.iter().any(|allowed| *allowed == role)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn normalize_correspondent_assignments(
|
||||||
|
assignments: &[CorrespondentAssignmentInput],
|
||||||
|
) -> AppResult<(Vec<(Uuid, String)>, Vec<Uuid>, Vec<String>)> {
|
||||||
|
let mut unique_pairs: HashSet<(Uuid, String)> = HashSet::new();
|
||||||
|
let mut normalized_pairs: Vec<(Uuid, String)> = Vec::new();
|
||||||
|
let mut role_set: HashSet<String> = HashSet::new();
|
||||||
|
let mut correspondent_ids: HashSet<Uuid> = HashSet::new();
|
||||||
|
|
||||||
|
for assignment in assignments {
|
||||||
|
let role = normalize_role(&assignment.role);
|
||||||
|
if role.is_empty() {
|
||||||
|
return Err(AppError::bad_request("role must not be empty"));
|
||||||
|
}
|
||||||
|
if !is_valid_correspondent_role(&role) {
|
||||||
|
return Err(AppError::bad_request(format!(
|
||||||
|
"invalid correspondent role '{role}'. Allowed roles: {}",
|
||||||
|
CORRESPONDENT_ROLES.join(", ")
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if !unique_pairs.insert((assignment.correspondent_id, role.clone())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
normalized_pairs.push((assignment.correspondent_id, role.clone()));
|
||||||
|
role_set.insert(role);
|
||||||
|
correspondent_ids.insert(assignment.correspondent_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
if normalized_pairs.is_empty() {
|
||||||
|
return Err(AppError::bad_request(
|
||||||
|
"assignments must contain at least one unique correspondent/role pair",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut correspondents_vec: Vec<Uuid> = correspondent_ids.into_iter().collect();
|
||||||
|
correspondents_vec.sort();
|
||||||
|
|
||||||
|
let mut roles_vec: Vec<String> = role_set.into_iter().collect();
|
||||||
|
roles_vec.sort();
|
||||||
|
|
||||||
|
Ok((normalized_pairs, correspondents_vec, roles_vec))
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
use serde_json::Value;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
pub fn build_quickwit_query(input: &str) -> Option<String> {
|
||||||
|
let tokens: Vec<String> = input
|
||||||
|
.split_whitespace()
|
||||||
|
.filter(|token| !token.is_empty())
|
||||||
|
.map(|token| {
|
||||||
|
let normalized = token.to_lowercase();
|
||||||
|
escape_quickwit_token(&normalized)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if tokens.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let parts: Vec<String> = tokens
|
||||||
|
.into_iter()
|
||||||
|
.map(|token| format!("(title:{token} OR text:{token})"))
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
Some(parts.join(" AND "))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn escape_quickwit_token(token: &str) -> String {
|
||||||
|
let mut escaped = String::with_capacity(token.len());
|
||||||
|
for ch in token.chars() {
|
||||||
|
match ch {
|
||||||
|
'+' | '-' | '&' | '|' | '!' | '(' | ')' | '{' | '}' | '[' | ']' | '^' | '"' | '~'
|
||||||
|
| '*' | '?' | ':' | '\\' | '/' => {
|
||||||
|
escaped.push('\\');
|
||||||
|
escaped.push(ch);
|
||||||
|
}
|
||||||
|
_ => escaped.push(ch),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
escaped
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extract_document_id(hit: &Value) -> Option<Uuid> {
|
||||||
|
for key in ["_source", "source", "fields", "stored_fields"] {
|
||||||
|
if let Some(value) = hit.get(key) {
|
||||||
|
if let Some(uuid) = extract_uuid_from_value(value) {
|
||||||
|
return Some(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(value) = hit.get("document_id") {
|
||||||
|
if let Some(uuid) = extract_uuid_from_value(value) {
|
||||||
|
return Some(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn extract_uuid_from_value(value: &Value) -> Option<Uuid> {
|
||||||
|
if let Some(obj) = value.as_object() {
|
||||||
|
if let Some(inner) = obj.get("document_id") {
|
||||||
|
return parse_uuid_value(inner);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(arr) = value.as_array() {
|
||||||
|
for item in arr {
|
||||||
|
if let Some(uuid) = extract_uuid_from_value(item) {
|
||||||
|
return Some(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_uuid_value(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_uuid_value(value: &Value) -> Option<Uuid> {
|
||||||
|
if let Some(s) = value.as_str() {
|
||||||
|
return Uuid::parse_str(s).ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(arr) = value.as_array() {
|
||||||
|
for item in arr {
|
||||||
|
if let Some(uuid) = parse_uuid_value(item) {
|
||||||
|
return Some(uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
None
|
||||||
|
}
|
||||||
+194
-206
@@ -4,17 +4,24 @@ use axum::{
|
|||||||
};
|
};
|
||||||
use diesel::{dsl::exists, prelude::*, PgConnection};
|
use diesel::{dsl::exists, prelude::*, PgConnection};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::HashSet;
|
use serde_json::Value;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::error::{AppError, AppResult};
|
|
||||||
use crate::models::{Document, Folder, NewFolder};
|
use crate::models::{Document, Folder, NewFolder};
|
||||||
use crate::schema::{document_tags, documents, folders};
|
use crate::schema::{documents, folders};
|
||||||
use crate::state::AppState;
|
use crate::state::AppState;
|
||||||
|
use crate::{
|
||||||
|
auth::TenantScopedConn,
|
||||||
|
error::{AppError, AppResult},
|
||||||
|
};
|
||||||
|
|
||||||
use super::documents::{
|
use super::documents::{
|
||||||
load_primary_thumbnails, load_tags_for_documents, to_document_response, to_iso,
|
load_correspondents_for_documents, load_primary_assets, load_tags_for_documents,
|
||||||
DocumentResponse,
|
to_document_response, DocumentResponse,
|
||||||
|
};
|
||||||
|
use crate::utils::{
|
||||||
|
json::{classify_nullable, NullableValue},
|
||||||
|
time::to_iso,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -29,11 +36,6 @@ pub struct EnsureFolderPathRequest {
|
|||||||
pub segments: Vec<String>,
|
pub segments: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct UpdateFolderRequest {
|
|
||||||
pub parent_id: Option<Uuid>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct FolderResponse {
|
pub struct FolderResponse {
|
||||||
pub folder: FolderInfo,
|
pub folder: FolderInfo,
|
||||||
@@ -47,9 +49,13 @@ pub struct FolderContentsResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct DocumentSearchQuery {
|
pub struct FolderContentsQuery {
|
||||||
pub query: Option<String>,
|
#[serde(default = "default_include_documents")]
|
||||||
pub tags: Option<String>,
|
pub include_documents: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn default_include_documents() -> bool {
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -57,21 +63,40 @@ pub struct FolderInfo {
|
|||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub parent_id: Option<Uuid>,
|
pub parent_id: Option<Uuid>,
|
||||||
pub path_cache: Option<String>,
|
|
||||||
pub created_at: String,
|
pub created_at: String,
|
||||||
pub updated_at: String,
|
pub updated_at: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_folder(
|
||||||
|
Path(folder_id): Path<Uuid>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
) -> AppResult<Json<FolderResponse>> {
|
||||||
|
let folder: Folder = folders::table
|
||||||
|
.find(folder_id)
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)?;
|
||||||
|
|
||||||
|
Ok(Json(FolderResponse {
|
||||||
|
folder: folder_to_info(folder),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn ensure_folder_path(
|
pub async fn ensure_folder_path(
|
||||||
State(state): State<AppState>,
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
Json(payload): Json<EnsureFolderPathRequest>,
|
Json(payload): Json<EnsureFolderPathRequest>,
|
||||||
) -> AppResult<Json<FolderResponse>> {
|
) -> AppResult<Json<FolderResponse>> {
|
||||||
if payload.segments.is_empty() {
|
if payload.segments.is_empty() {
|
||||||
return Err(AppError::bad_request("segments must not be empty"));
|
return Err(AppError::bad_request("segments must not be empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut conn = state.db()?;
|
|
||||||
|
|
||||||
let target_folder = conn.transaction::<Folder, AppError, _>(|conn| {
|
let target_folder = conn.transaction::<Folder, AppError, _>(|conn| {
|
||||||
let mut current_parent = payload.parent_id;
|
let mut current_parent = payload.parent_id;
|
||||||
let mut last_folder: Option<Folder> = None;
|
let mut last_folder: Option<Folder> = None;
|
||||||
@@ -86,12 +111,14 @@ pub async fn ensure_folder_path(
|
|||||||
folders::table
|
folders::table
|
||||||
.filter(folders::parent_id.eq(Some(parent_id)))
|
.filter(folders::parent_id.eq(Some(parent_id)))
|
||||||
.filter(folders::name.eq(name))
|
.filter(folders::name.eq(name))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.first(conn)
|
.first(conn)
|
||||||
.optional()?
|
.optional()?
|
||||||
} else {
|
} else {
|
||||||
folders::table
|
folders::table
|
||||||
.filter(folders::parent_id.is_null())
|
.filter(folders::parent_id.is_null())
|
||||||
.filter(folders::name.eq(name))
|
.filter(folders::name.eq(name))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.first(conn)
|
.first(conn)
|
||||||
.optional()?
|
.optional()?
|
||||||
};
|
};
|
||||||
@@ -99,12 +126,11 @@ pub async fn ensure_folder_path(
|
|||||||
let folder = if let Some(folder) = existing {
|
let folder = if let Some(folder) = existing {
|
||||||
folder
|
folder
|
||||||
} else {
|
} else {
|
||||||
let path_cache = build_path_cache(conn, current_parent, name)?;
|
|
||||||
let new_folder = NewFolder {
|
let new_folder = NewFolder {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
name: name.to_string(),
|
name: name.to_string(),
|
||||||
parent_id: current_parent,
|
parent_id: current_parent,
|
||||||
path_cache,
|
tenant_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
diesel::insert_into(folders::table)
|
diesel::insert_into(folders::table)
|
||||||
@@ -127,21 +153,22 @@ pub async fn ensure_folder_path(
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_folder(
|
pub async fn create_folder(
|
||||||
State(state): State<AppState>,
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
Json(payload): Json<CreateFolderRequest>,
|
Json(payload): Json<CreateFolderRequest>,
|
||||||
) -> AppResult<Json<FolderResponse>> {
|
) -> AppResult<Json<FolderResponse>> {
|
||||||
if payload.name.trim().is_empty() {
|
if payload.name.trim().is_empty() {
|
||||||
return Err(AppError::bad_request("name must not be empty"));
|
return Err(AppError::bad_request("name must not be empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut conn = state.db()?;
|
|
||||||
let path_cache = build_path_cache(&mut conn, payload.parent_id, &payload.name)?;
|
|
||||||
|
|
||||||
let new_folder = NewFolder {
|
let new_folder = NewFolder {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
name: payload.name.trim().to_string(),
|
name: payload.name.trim().to_string(),
|
||||||
parent_id: payload.parent_id,
|
parent_id: payload.parent_id,
|
||||||
path_cache,
|
tenant_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
diesel::insert_into(folders::table)
|
diesel::insert_into(folders::table)
|
||||||
@@ -157,9 +184,14 @@ pub async fn create_folder(
|
|||||||
pub async fn list_folder_contents(
|
pub async fn list_folder_contents(
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
Path(folder_identifier): Path<String>,
|
Path(folder_identifier): Path<String>,
|
||||||
|
Query(query): Query<FolderContentsQuery>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
user_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
) -> AppResult<Json<FolderContentsResponse>> {
|
) -> AppResult<Json<FolderContentsResponse>> {
|
||||||
let mut conn = state.db()?;
|
|
||||||
|
|
||||||
let folder_id = if folder_identifier.eq_ignore_ascii_case("root") {
|
let folder_id = if folder_identifier.eq_ignore_ascii_case("root") {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
@@ -171,7 +203,10 @@ pub async fn list_folder_contents(
|
|||||||
|
|
||||||
let folder = match folder_id {
|
let folder = match folder_id {
|
||||||
Some(id) => Some(folder_to_info(
|
Some(id) => Some(folder_to_info(
|
||||||
folders::table.find(id).first::<Folder>(&mut conn)?,
|
folders::table
|
||||||
|
.find(id)
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
|
.first::<Folder>(&mut conn)?,
|
||||||
)),
|
)),
|
||||||
None => None,
|
None => None,
|
||||||
};
|
};
|
||||||
@@ -179,18 +214,22 @@ pub async fn list_folder_contents(
|
|||||||
let child_folders: Vec<Folder> = if let Some(parent_id) = folder_id {
|
let child_folders: Vec<Folder> = if let Some(parent_id) = folder_id {
|
||||||
folders::table
|
folders::table
|
||||||
.filter(folders::parent_id.eq(parent_id))
|
.filter(folders::parent_id.eq(parent_id))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.order(folders::name.asc())
|
.order(folders::name.asc())
|
||||||
.load(&mut conn)?
|
.load(&mut conn)?
|
||||||
} else {
|
} else {
|
||||||
folders::table
|
folders::table
|
||||||
.filter(folders::parent_id.is_null())
|
.filter(folders::parent_id.is_null())
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.order(folders::name.asc())
|
.order(folders::name.asc())
|
||||||
.load(&mut conn)?
|
.load(&mut conn)?
|
||||||
};
|
};
|
||||||
let subfolders = child_folders.into_iter().map(folder_to_info).collect();
|
let subfolders = child_folders.into_iter().map(folder_to_info).collect();
|
||||||
|
|
||||||
|
let documents = if query.include_documents {
|
||||||
let docs_query = documents::table
|
let docs_query = documents::table
|
||||||
.filter(documents::deleted_at.is_null())
|
.filter(documents::deleted_at.is_null())
|
||||||
|
.filter(documents::tenant_id.eq(tenant_id))
|
||||||
.order(documents::uploaded_at.desc());
|
.order(documents::uploaded_at.desc());
|
||||||
|
|
||||||
let docs: Vec<Document> = if let Some(current_folder) = folder_id {
|
let docs: Vec<Document> = if let Some(current_folder) = folder_id {
|
||||||
@@ -205,18 +244,30 @@ pub async fn list_folder_contents(
|
|||||||
|
|
||||||
let doc_ids: Vec<Uuid> = docs.iter().map(|doc| doc.id).collect();
|
let doc_ids: Vec<Uuid> = docs.iter().map(|doc| doc.id).collect();
|
||||||
let tags_map = load_tags_for_documents(&mut conn, &doc_ids)?;
|
let tags_map = load_tags_for_documents(&mut conn, &doc_ids)?;
|
||||||
|
let mut correspondents_map = load_correspondents_for_documents(&mut conn, &doc_ids)?;
|
||||||
drop(conn);
|
drop(conn);
|
||||||
|
|
||||||
let thumbnails = load_primary_thumbnails(&state, &docs).await?;
|
let primary_versions = load_primary_assets(&state, tenant_id, &docs).await?;
|
||||||
|
|
||||||
let documents = docs
|
let mut documents = Vec::with_capacity(doc_ids.len());
|
||||||
.into_iter()
|
for doc in docs {
|
||||||
.map(|doc| {
|
|
||||||
let tags = tags_map.get(&doc.id).cloned();
|
let tags = tags_map.get(&doc.id).cloned();
|
||||||
let thumbnail = thumbnails.get(&doc.id).cloned();
|
let correspondents = correspondents_map.remove(&doc.id).unwrap_or_default();
|
||||||
to_document_response(doc, tags, thumbnail)
|
let current_version = primary_versions.get(&doc.id).cloned();
|
||||||
})
|
documents.push(to_document_response(
|
||||||
.collect();
|
&state,
|
||||||
|
user_id,
|
||||||
|
doc,
|
||||||
|
tags,
|
||||||
|
correspondents,
|
||||||
|
current_version,
|
||||||
|
)?);
|
||||||
|
}
|
||||||
|
|
||||||
|
documents
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
Ok(Json(FolderContentsResponse {
|
Ok(Json(FolderContentsResponse {
|
||||||
folder,
|
folder,
|
||||||
@@ -225,117 +276,24 @@ pub async fn list_folder_contents(
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn search_documents(
|
|
||||||
State(state): State<AppState>,
|
|
||||||
Path(folder_identifier): Path<String>,
|
|
||||||
Query(params): Query<DocumentSearchQuery>,
|
|
||||||
) -> AppResult<Json<Vec<DocumentResponse>>> {
|
|
||||||
let mut conn = state.db()?;
|
|
||||||
|
|
||||||
let folder_id = if folder_identifier.eq_ignore_ascii_case("root") {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some(
|
|
||||||
Uuid::parse_str(&folder_identifier)
|
|
||||||
.map_err(|_| AppError::bad_request("folder identifier must be 'root' or a UUID"))?,
|
|
||||||
)
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut docs_query = documents::table
|
|
||||||
.filter(documents::deleted_at.is_null())
|
|
||||||
.into_boxed();
|
|
||||||
|
|
||||||
if let Some(folder_id) = folder_id {
|
|
||||||
let descendant_ids = gather_descendant_folder_ids(&mut conn, folder_id)?;
|
|
||||||
docs_query = docs_query.filter(documents::folder_id.eq_any(descendant_ids));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(query) = params
|
|
||||||
.query
|
|
||||||
.as_ref()
|
|
||||||
.map(|s| s.trim())
|
|
||||||
.filter(|s| !s.is_empty())
|
|
||||||
{
|
|
||||||
let pattern = format!("%{}%", query);
|
|
||||||
docs_query = docs_query.filter(documents::original_name.ilike(pattern));
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(tags_param) = params
|
|
||||||
.tags
|
|
||||||
.as_ref()
|
|
||||||
.map(|s| s.trim())
|
|
||||||
.filter(|s| !s.is_empty())
|
|
||||||
{
|
|
||||||
let tag_ids: Result<Vec<Uuid>, _> = tags_param
|
|
||||||
.split(',')
|
|
||||||
.map(|s| Uuid::parse_str(s.trim()))
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
if let Ok(ids) = tag_ids {
|
|
||||||
if !ids.is_empty() {
|
|
||||||
let mut doc_id_set: Option<HashSet<Uuid>> = None;
|
|
||||||
for tag_id in &ids {
|
|
||||||
let docs_for_tag: Vec<Uuid> = document_tags::table
|
|
||||||
.filter(document_tags::tag_id.eq(*tag_id))
|
|
||||||
.select(document_tags::document_id)
|
|
||||||
.load(&mut conn)?;
|
|
||||||
let docs_set: HashSet<Uuid> = docs_for_tag.into_iter().collect();
|
|
||||||
doc_id_set = Some(match doc_id_set {
|
|
||||||
Some(existing) => existing.intersection(&docs_set).cloned().collect(),
|
|
||||||
None => docs_set,
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(ref set) = doc_id_set {
|
|
||||||
if set.is_empty() {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let matching_doc_ids: Vec<Uuid> =
|
|
||||||
doc_id_set.unwrap_or_default().into_iter().collect();
|
|
||||||
|
|
||||||
if matching_doc_ids.is_empty() {
|
|
||||||
return Ok(Json(vec![]));
|
|
||||||
}
|
|
||||||
|
|
||||||
docs_query = docs_query.filter(documents::id.eq_any(matching_doc_ids));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let docs: Vec<Document> = docs_query
|
|
||||||
.order(documents::uploaded_at.desc())
|
|
||||||
.load(&mut conn)?;
|
|
||||||
|
|
||||||
let doc_ids: Vec<Uuid> = docs.iter().map(|doc| doc.id).collect();
|
|
||||||
let tags_map = load_tags_for_documents(&mut conn, &doc_ids)?;
|
|
||||||
drop(conn);
|
|
||||||
|
|
||||||
let thumbnails = load_primary_thumbnails(&state, &docs).await?;
|
|
||||||
let response = docs
|
|
||||||
.into_iter()
|
|
||||||
.map(|doc| {
|
|
||||||
let tags = tags_map.get(&doc.id).cloned();
|
|
||||||
let thumbnail = thumbnails.get(&doc.id).cloned();
|
|
||||||
to_document_response(doc, tags, thumbnail)
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
Ok(Json(response))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete_folder(
|
pub async fn delete_folder(
|
||||||
State(state): State<AppState>,
|
|
||||||
Path(folder_id): Path<Uuid>,
|
Path(folder_id): Path<Uuid>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
) -> AppResult<StatusCode> {
|
) -> AppResult<StatusCode> {
|
||||||
let mut conn = state.db()?;
|
|
||||||
|
|
||||||
conn.transaction::<_, AppError, _>(|conn| {
|
conn.transaction::<_, AppError, _>(|conn| {
|
||||||
folders::table.find(folder_id).first::<Folder>(conn)?;
|
folders::table
|
||||||
|
.find(folder_id)
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
|
.first::<Folder>(conn)?;
|
||||||
|
|
||||||
let has_child_folders: bool = diesel::select(exists(
|
let has_child_folders: bool = diesel::select(exists(
|
||||||
folders::table.filter(folders::parent_id.eq(Some(folder_id))),
|
folders::table
|
||||||
|
.filter(folders::parent_id.eq(Some(folder_id)))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id)),
|
||||||
))
|
))
|
||||||
.get_result(conn)?;
|
.get_result(conn)?;
|
||||||
|
|
||||||
@@ -348,6 +306,7 @@ pub async fn delete_folder(
|
|||||||
let has_documents: bool = diesel::select(exists(
|
let has_documents: bool = diesel::select(exists(
|
||||||
documents::table
|
documents::table
|
||||||
.filter(documents::folder_id.eq(Some(folder_id)))
|
.filter(documents::folder_id.eq(Some(folder_id)))
|
||||||
|
.filter(documents::tenant_id.eq(tenant_id))
|
||||||
.filter(documents::deleted_at.is_null()),
|
.filter(documents::deleted_at.is_null()),
|
||||||
))
|
))
|
||||||
.get_result(conn)?;
|
.get_result(conn)?;
|
||||||
@@ -358,7 +317,12 @@ pub async fn delete_folder(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
diesel::delete(folders::table.find(folder_id)).execute(conn)?;
|
diesel::delete(
|
||||||
|
folders::table
|
||||||
|
.filter(folders::id.eq(folder_id))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.execute(conn)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
@@ -366,51 +330,106 @@ pub async fn delete_folder(
|
|||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn update_folder_parent(
|
pub async fn update_folder(
|
||||||
State(state): State<AppState>,
|
|
||||||
Path(folder_id): Path<Uuid>,
|
Path(folder_id): Path<Uuid>,
|
||||||
Json(payload): Json<UpdateFolderRequest>,
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
Json(body): Json<Value>,
|
||||||
) -> AppResult<StatusCode> {
|
) -> AppResult<StatusCode> {
|
||||||
if payload.parent_id == Some(folder_id) {
|
if !body.is_object() {
|
||||||
|
return Err(AppError::bad_request("request body must be a JSON object"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let parent_class = classify_nullable(body.get("parent_id")).map_err(AppError::bad_request)?;
|
||||||
|
let name_class = classify_nullable(body.get("name")).map_err(AppError::bad_request)?;
|
||||||
|
|
||||||
|
conn.transaction::<(), AppError, _>(|conn| {
|
||||||
|
let folder: Folder = folders::table
|
||||||
|
.find(folder_id)
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
|
.first(conn)?;
|
||||||
|
|
||||||
|
let mut next_parent = folder.parent_id;
|
||||||
|
let mut parent_changed = false;
|
||||||
|
match parent_class {
|
||||||
|
NullableValue::Omitted => {}
|
||||||
|
NullableValue::Null => {
|
||||||
|
if folder.parent_id.is_some() {
|
||||||
|
parent_changed = true;
|
||||||
|
}
|
||||||
|
next_parent = None;
|
||||||
|
}
|
||||||
|
NullableValue::String(value) => {
|
||||||
|
let trimmed = value.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(AppError::bad_request("parent_id must not be empty"));
|
||||||
|
}
|
||||||
|
let parent_id = Uuid::parse_str(trimmed)
|
||||||
|
.map_err(|_| AppError::bad_request("parent_id must be a valid UUID or null"))?;
|
||||||
|
if parent_id == folder_id {
|
||||||
return Err(AppError::bad_request("folder cannot be its own parent"));
|
return Err(AppError::bad_request("folder cannot be its own parent"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut conn = state.db()?;
|
let _parent: Folder = folders::table
|
||||||
|
.find(parent_id)
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
|
.first(conn)?;
|
||||||
|
|
||||||
conn.transaction::<(), AppError, _>(|conn| {
|
if folder.parent_id != Some(parent_id) {
|
||||||
let folder: Folder = folders::table.find(folder_id).first(conn)?;
|
let descendant_ids = gather_descendant_folder_ids(conn, tenant_id, folder_id)?;
|
||||||
let current_parent = folder.parent_id;
|
|
||||||
let next_parent = payload.parent_id;
|
|
||||||
|
|
||||||
if current_parent == next_parent {
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(parent_id) = next_parent {
|
|
||||||
let _parent: Folder = folders::table.find(parent_id).first(conn)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(parent_id) = next_parent {
|
|
||||||
let descendant_ids = gather_descendant_folder_ids(conn, folder_id)?;
|
|
||||||
if descendant_ids.contains(&parent_id) {
|
if descendant_ids.contains(&parent_id) {
|
||||||
return Err(AppError::bad_request(
|
return Err(AppError::bad_request(
|
||||||
"cannot move folder into itself or a descendant",
|
"cannot move folder into itself or a descendant",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
parent_changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
next_parent = Some(parent_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_name = folder.name.clone();
|
||||||
|
let mut name_changed = false;
|
||||||
|
match name_class {
|
||||||
|
NullableValue::Omitted => {}
|
||||||
|
NullableValue::Null => {
|
||||||
|
return Err(AppError::bad_request("name cannot be null"));
|
||||||
|
}
|
||||||
|
NullableValue::String(value) => {
|
||||||
|
let trimmed = value.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(AppError::bad_request("name must not be empty"));
|
||||||
|
}
|
||||||
|
|
||||||
|
if trimmed != folder.name {
|
||||||
|
new_name = trimmed.to_string();
|
||||||
|
name_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !parent_changed && !name_changed {
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let conflict = if let Some(parent_id) = next_parent {
|
let conflict = if let Some(parent_id) = next_parent {
|
||||||
folders::table
|
folders::table
|
||||||
.filter(folders::parent_id.eq(Some(parent_id)))
|
.filter(folders::parent_id.eq(Some(parent_id)))
|
||||||
.filter(folders::name.eq(&folder.name))
|
.filter(folders::name.eq(&new_name))
|
||||||
.filter(folders::id.ne(folder_id))
|
.filter(folders::id.ne(folder_id))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.first::<Folder>(conn)
|
.first::<Folder>(conn)
|
||||||
.optional()?
|
.optional()?
|
||||||
} else {
|
} else {
|
||||||
folders::table
|
folders::table
|
||||||
.filter(folders::parent_id.is_null())
|
.filter(folders::parent_id.is_null())
|
||||||
.filter(folders::name.eq(&folder.name))
|
.filter(folders::name.eq(&new_name))
|
||||||
.filter(folders::id.ne(folder_id))
|
.filter(folders::id.ne(folder_id))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.first::<Folder>(conn)
|
.first::<Folder>(conn)
|
||||||
.optional()?
|
.optional()?
|
||||||
};
|
};
|
||||||
@@ -421,60 +440,45 @@ pub async fn update_folder_parent(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
let new_path = build_path_cache(conn, next_parent, &folder.name)?;
|
diesel::update(
|
||||||
|
folders::table
|
||||||
diesel::update(folders::table.find(folder_id))
|
.find(folder_id)
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
.set((
|
.set((
|
||||||
folders::parent_id.eq(next_parent),
|
folders::parent_id.eq(next_parent),
|
||||||
folders::path_cache.eq(new_path),
|
folders::name.eq(&new_name),
|
||||||
))
|
))
|
||||||
.execute(conn)?;
|
.execute(conn)?;
|
||||||
|
|
||||||
refresh_descendant_paths(conn, folder_id)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
Ok(StatusCode::NO_CONTENT)
|
Ok(StatusCode::NO_CONTENT)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_path_cache(
|
|
||||||
conn: &mut PgConnection,
|
|
||||||
parent_id: Option<Uuid>,
|
|
||||||
name: &str,
|
|
||||||
) -> AppResult<Option<String>> {
|
|
||||||
let path = if let Some(parent_id) = parent_id {
|
|
||||||
let parent: Folder = folders::table.find(parent_id).first(conn)?;
|
|
||||||
let base = parent
|
|
||||||
.path_cache
|
|
||||||
.unwrap_or_else(|| "/".to_string())
|
|
||||||
.trim_end_matches('/')
|
|
||||||
.to_string();
|
|
||||||
format!("{}/{}", base, name)
|
|
||||||
} else {
|
|
||||||
format!("/{}", name)
|
|
||||||
};
|
|
||||||
Ok(Some(path))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn folder_to_info(folder: Folder) -> FolderInfo {
|
fn folder_to_info(folder: Folder) -> FolderInfo {
|
||||||
FolderInfo {
|
FolderInfo {
|
||||||
id: folder.id,
|
id: folder.id,
|
||||||
name: folder.name,
|
name: folder.name,
|
||||||
parent_id: folder.parent_id,
|
parent_id: folder.parent_id,
|
||||||
path_cache: folder.path_cache,
|
|
||||||
created_at: to_iso(folder.created_at),
|
created_at: to_iso(folder.created_at),
|
||||||
updated_at: to_iso(folder.updated_at),
|
updated_at: to_iso(folder.updated_at),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gather_descendant_folder_ids(conn: &mut PgConnection, folder_id: Uuid) -> AppResult<Vec<Uuid>> {
|
pub(super) fn gather_descendant_folder_ids(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
folder_id: Uuid,
|
||||||
|
) -> AppResult<Vec<Uuid>> {
|
||||||
let mut ids = vec![folder_id];
|
let mut ids = vec![folder_id];
|
||||||
let mut queue = vec![folder_id];
|
let mut queue = vec![folder_id];
|
||||||
|
|
||||||
while let Some(current) = queue.pop() {
|
while let Some(current) = queue.pop() {
|
||||||
let child_ids: Vec<Uuid> = folders::table
|
let child_ids: Vec<Uuid> = folders::table
|
||||||
.filter(folders::parent_id.eq(Some(current)))
|
.filter(folders::parent_id.eq(Some(current)))
|
||||||
|
.filter(folders::tenant_id.eq(tenant_id))
|
||||||
.select(folders::id)
|
.select(folders::id)
|
||||||
.load(conn)?;
|
.load(conn)?;
|
||||||
queue.extend(child_ids.iter().copied());
|
queue.extend(child_ids.iter().copied());
|
||||||
@@ -483,19 +487,3 @@ fn gather_descendant_folder_ids(conn: &mut PgConnection, folder_id: Uuid) -> App
|
|||||||
|
|
||||||
Ok(ids)
|
Ok(ids)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn refresh_descendant_paths(conn: &mut PgConnection, parent_id: Uuid) -> AppResult<()> {
|
|
||||||
let children: Vec<Folder> = folders::table
|
|
||||||
.filter(folders::parent_id.eq(Some(parent_id)))
|
|
||||||
.load(conn)?;
|
|
||||||
|
|
||||||
for child in children {
|
|
||||||
let path_cache = build_path_cache(conn, Some(parent_id), &child.name)?;
|
|
||||||
diesel::update(folders::table.find(child.id))
|
|
||||||
.set(folders::path_cache.eq(path_cache))
|
|
||||||
.execute(conn)?;
|
|
||||||
refresh_descendant_paths(conn, child.id)?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|||||||
+94
-17
@@ -1,45 +1,82 @@
|
|||||||
|
use axum::http::HeaderValue;
|
||||||
use axum::{
|
use axum::{
|
||||||
extract::DefaultBodyLimit,
|
extract::DefaultBodyLimit,
|
||||||
middleware,
|
middleware,
|
||||||
|
response::Json,
|
||||||
routing::{delete, get, patch, post},
|
routing::{delete, get, patch, post},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
use tower_http::cors::{Any, CorsLayer};
|
use std::sync::Arc;
|
||||||
|
use tower_http::cors::{AllowOrigin, CorsLayer};
|
||||||
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
use crate::{auth::AuthenticatedUser, state::AppState};
|
use crate::{auth::AuthenticatedUser, openapi::ApiDoc, state::AppState};
|
||||||
|
|
||||||
pub mod auth;
|
pub mod auth;
|
||||||
|
pub mod correspondents;
|
||||||
pub mod documents;
|
pub mod documents;
|
||||||
pub mod folders;
|
pub mod folders;
|
||||||
pub mod health;
|
pub mod health;
|
||||||
pub mod tags;
|
pub mod tags;
|
||||||
|
pub mod webdav;
|
||||||
|
|
||||||
pub fn create_router(state: AppState) -> Router<()> {
|
pub fn create_router(state: AppState) -> Router<()> {
|
||||||
let cors = CorsLayer::new()
|
let cors = if let Some(origins) = state.config.cors_allowed_origin.as_ref() {
|
||||||
.allow_origin(Any)
|
let headers: Vec<HeaderValue> = origins
|
||||||
.allow_methods(Any)
|
.split(',')
|
||||||
.allow_headers(Any);
|
.filter_map(|value| {
|
||||||
|
let trimmed = value.trim();
|
||||||
|
(!trimmed.is_empty()).then(|| {
|
||||||
|
trimmed
|
||||||
|
.parse::<HeaderValue>()
|
||||||
|
.expect("invalid CORS allowed origin")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
let allow_origin = AllowOrigin::list(headers);
|
||||||
|
|
||||||
|
CorsLayer::new()
|
||||||
|
.allow_origin(allow_origin)
|
||||||
|
.allow_methods(tower_http::cors::AllowMethods::mirror_request())
|
||||||
|
.allow_headers(tower_http::cors::AllowHeaders::mirror_request())
|
||||||
|
.allow_credentials(true)
|
||||||
|
} else {
|
||||||
|
CorsLayer::new()
|
||||||
|
.allow_origin(AllowOrigin::mirror_request())
|
||||||
|
.allow_methods(tower_http::cors::AllowMethods::mirror_request())
|
||||||
|
.allow_headers(tower_http::cors::AllowHeaders::mirror_request())
|
||||||
|
.allow_credentials(true)
|
||||||
|
};
|
||||||
|
|
||||||
let auth_routes = Router::new()
|
let auth_routes = Router::new()
|
||||||
.route("/login", post(auth::login))
|
.route("/login", post(auth::login))
|
||||||
|
.route("/refresh", post(auth::refresh))
|
||||||
.route("/logout", post(auth::logout))
|
.route("/logout", post(auth::logout))
|
||||||
|
.route("/select-tenant", post(auth::select_tenant))
|
||||||
.route("/me", get(auth::me));
|
.route("/me", get(auth::me));
|
||||||
|
|
||||||
let documents_routes = Router::new()
|
let documents_routes = Router::new()
|
||||||
|
.route("/check", get(documents::check_document))
|
||||||
.route(
|
.route(
|
||||||
"/",
|
"/",
|
||||||
get(documents::list_documents).post(documents::upload_document),
|
get(documents::list_documents).post(documents::upload_document),
|
||||||
)
|
)
|
||||||
.route("/reanalyze", post(documents::reanalyze_all_documents))
|
|
||||||
.route("/bulk/move", post(documents::bulk_move_documents))
|
.route("/bulk/move", post(documents::bulk_move_documents))
|
||||||
.route("/bulk/tags", post(documents::bulk_update_tags))
|
.route("/bulk/tags", post(documents::bulk_update_tags))
|
||||||
|
.route(
|
||||||
|
"/bulk/correspondents",
|
||||||
|
post(documents::bulk_assign_correspondents),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/bulk/reanalyze",
|
"/bulk/reanalyze",
|
||||||
post(documents::reanalyze_selected_documents),
|
post(documents::reanalyze_selected_documents),
|
||||||
)
|
)
|
||||||
.route(
|
.route(
|
||||||
"/:id",
|
"/:id",
|
||||||
get(documents::get_document).delete(documents::delete_document),
|
get(documents::get_document)
|
||||||
|
.delete(documents::delete_document)
|
||||||
|
.patch(documents::update_document),
|
||||||
)
|
)
|
||||||
.route("/:id/download", get(documents::download_document))
|
.route("/:id/download", get(documents::download_document))
|
||||||
.route(
|
.route(
|
||||||
@@ -48,31 +85,71 @@ pub fn create_router(state: AppState) -> Router<()> {
|
|||||||
)
|
)
|
||||||
.route("/:id/folder", patch(documents::move_document))
|
.route("/:id/folder", patch(documents::move_document))
|
||||||
.route("/:id/tags", post(documents::assign_tags))
|
.route("/:id/tags", post(documents::assign_tags))
|
||||||
.route("/:id/tags/:tag_id", delete(documents::remove_tag));
|
.route("/:id/tags/:tag_id", delete(documents::remove_tag))
|
||||||
|
.route(
|
||||||
|
"/:id/correspondents",
|
||||||
|
post(documents::assign_correspondents),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
"/:id/correspondents/:correspondent_id",
|
||||||
|
delete(documents::remove_correspondent),
|
||||||
|
);
|
||||||
|
|
||||||
|
let download_routes =
|
||||||
|
Router::new().route("/download/:token", get(documents::download_with_token));
|
||||||
|
|
||||||
let folders_routes = Router::new()
|
let folders_routes = Router::new()
|
||||||
.route("/", post(folders::create_folder))
|
.route("/", post(folders::create_folder))
|
||||||
.route("/path", post(folders::ensure_folder_path))
|
.route("/path", post(folders::ensure_folder_path))
|
||||||
|
.route("/:id", get(folders::get_folder))
|
||||||
|
.route("/:id", delete(folders::delete_folder))
|
||||||
|
.route("/:id", patch(folders::update_folder))
|
||||||
|
.route("/:id/contents", get(folders::list_folder_contents));
|
||||||
|
|
||||||
|
let tags_routes = Router::new()
|
||||||
|
.route("/", get(tags::list_tags).post(tags::create_tag))
|
||||||
|
.route("/:id", patch(tags::update_tag).delete(tags::delete_tag));
|
||||||
|
|
||||||
|
let correspondents_routes = Router::new()
|
||||||
|
.route(
|
||||||
|
"/",
|
||||||
|
get(correspondents::list_correspondents).post(correspondents::create_correspondent),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/:id",
|
"/:id",
|
||||||
delete(folders::delete_folder).patch(folders::update_folder_parent),
|
patch(correspondents::update_correspondent)
|
||||||
)
|
.delete(correspondents::delete_correspondent),
|
||||||
.route("/:id/contents", get(folders::list_folder_contents))
|
);
|
||||||
.route("/:id/documents", get(folders::search_documents));
|
|
||||||
|
|
||||||
let tags_routes = Router::new().route("/", get(tags::list_tags).post(tags::create_tag));
|
|
||||||
|
|
||||||
let protected_state = state.clone();
|
let protected_state = state.clone();
|
||||||
|
let assets_routes = Router::new().route("/:asset_id", get(documents::get_document_asset));
|
||||||
|
|
||||||
let protected_routes = Router::new()
|
let protected_routes = Router::new()
|
||||||
.nest("/api/documents", documents_routes)
|
.nest("/api/documents", documents_routes)
|
||||||
.nest("/api/folders", folders_routes)
|
.nest("/api/folders", folders_routes)
|
||||||
.nest("/api/tags", tags_routes)
|
.nest("/api/tags", tags_routes)
|
||||||
.route("/api/health", get(health::health_check))
|
.nest("/api/correspondents", correspondents_routes)
|
||||||
|
.nest("/api/assets", assets_routes)
|
||||||
.layer(middleware::from_extractor_with_state::<AuthenticatedUser, _>(protected_state));
|
.layer(middleware::from_extractor_with_state::<AuthenticatedUser, _>(protected_state));
|
||||||
|
|
||||||
|
let openapi_arc = Arc::new(ApiDoc::openapi());
|
||||||
|
let docs_route = Router::new().route(
|
||||||
|
"/api/docs/openapi.json",
|
||||||
|
get({
|
||||||
|
let spec = openapi_arc.clone();
|
||||||
|
move || {
|
||||||
|
let spec = spec.clone();
|
||||||
|
async move { Json((*spec).clone()) }
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
.nest("/api/auth", auth_routes)
|
.merge(download_routes)
|
||||||
.merge(protected_routes)
|
.merge(protected_routes)
|
||||||
|
.merge(docs_route)
|
||||||
|
.nest("/api/auth", auth_routes)
|
||||||
|
.route("/api/health", get(health::health_check))
|
||||||
.with_state(state)
|
.with_state(state)
|
||||||
.layer(cors)
|
.layer(cors)
|
||||||
.layer(DefaultBodyLimit::max(1024 * 1024 * 512))
|
.layer(DefaultBodyLimit::max(1024 * 1024 * 512))
|
||||||
|
|||||||
+238
-17
@@ -1,14 +1,16 @@
|
|||||||
use axum::{extract::State, Json};
|
use crate::utils::json::{classify_nullable, NullableValue};
|
||||||
use diesel::prelude::*;
|
use axum::{extract::Path, http::StatusCode, Json};
|
||||||
use serde::Deserialize;
|
use diesel::{dsl::count_star, prelude::*};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::Value;
|
||||||
|
use std::collections::HashMap;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::auth::TenantScopedConn;
|
||||||
use crate::error::{AppError, AppResult};
|
use crate::error::{AppError, AppResult};
|
||||||
use crate::models::{NewTag, Tag};
|
use crate::models::{NewTag, Tag};
|
||||||
use crate::schema::tags;
|
use crate::schema::{document_tags, tags};
|
||||||
use crate::state::AppState;
|
use crate::utils::db::{no_content, EnsureEntity, IntoJsonResponse};
|
||||||
|
|
||||||
use super::documents::TagResponse;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
pub struct CreateTagRequest {
|
pub struct CreateTagRequest {
|
||||||
@@ -16,26 +18,71 @@ pub struct CreateTagRequest {
|
|||||||
pub color: Option<String>,
|
pub color: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn list_tags(State(state): State<AppState>) -> AppResult<Json<Vec<TagResponse>>> {
|
#[derive(AsChangeset, Default)]
|
||||||
let mut conn = state.db()?;
|
#[diesel(table_name = tags)]
|
||||||
let tag_list: Vec<Tag> = tags::table.order(tags::label.asc()).load(&mut conn)?;
|
struct UpdateTagChangeset<'a> {
|
||||||
let response = tag_list.into_iter().map(TagResponse::from).collect();
|
label: Option<&'a str>,
|
||||||
Ok(Json(response))
|
color: Option<Option<&'a str>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
pub struct TagCatalogEntry {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub label: String,
|
||||||
|
pub color: Option<String>,
|
||||||
|
pub usage_count: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn list_tags(
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
) -> AppResult<Json<Vec<TagCatalogEntry>>> {
|
||||||
|
let tag_list: Vec<Tag> = tags::table
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id))
|
||||||
|
.order(tags::label.asc())
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
let usage_rows: Vec<(Uuid, i64)> = document_tags::table
|
||||||
|
.filter(document_tags::tenant_id.eq(tenant_id))
|
||||||
|
.group_by(document_tags::tag_id)
|
||||||
|
.select((document_tags::tag_id, count_star()))
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
let usage_map: HashMap<Uuid, i64> = usage_rows.into_iter().collect();
|
||||||
|
|
||||||
|
let response: Vec<TagCatalogEntry> = tag_list
|
||||||
|
.into_iter()
|
||||||
|
.map(|tag| TagCatalogEntry {
|
||||||
|
id: tag.id,
|
||||||
|
label: tag.label,
|
||||||
|
color: tag.color,
|
||||||
|
usage_count: *usage_map.get(&tag.id).unwrap_or(&0),
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
response.into_json()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_tag(
|
pub async fn create_tag(
|
||||||
State(state): State<AppState>,
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
Json(payload): Json<CreateTagRequest>,
|
Json(payload): Json<CreateTagRequest>,
|
||||||
) -> AppResult<Json<TagResponse>> {
|
) -> AppResult<Json<TagCatalogEntry>> {
|
||||||
if payload.label.trim().is_empty() {
|
if payload.label.trim().is_empty() {
|
||||||
return Err(AppError::bad_request("label must not be empty"));
|
return Err(AppError::bad_request("label must not be empty"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut conn = state.db()?;
|
|
||||||
let new_tag = NewTag {
|
let new_tag = NewTag {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
label: payload.label.trim().to_string(),
|
label: payload.label.trim().to_string(),
|
||||||
color: payload.color,
|
color: payload.color,
|
||||||
|
tenant_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
match diesel::insert_into(tags::table)
|
match diesel::insert_into(tags::table)
|
||||||
@@ -52,6 +99,180 @@ pub async fn create_tag(
|
|||||||
Err(err) => return Err(AppError::from(err)),
|
Err(err) => return Err(AppError::from(err)),
|
||||||
}
|
}
|
||||||
|
|
||||||
let tag: Tag = tags::table.find(new_tag.id).first(&mut conn)?;
|
let tag: Tag = tags::table
|
||||||
Ok(Json(TagResponse::from(tag)))
|
.find(new_tag.id)
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.one()?;
|
||||||
|
|
||||||
|
TagCatalogEntry {
|
||||||
|
id: tag.id,
|
||||||
|
label: tag.label,
|
||||||
|
color: tag.color,
|
||||||
|
usage_count: 0,
|
||||||
|
}
|
||||||
|
.into_json()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_tag(
|
||||||
|
Path(tag_id): Path<Uuid>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
Json(body): Json<Value>,
|
||||||
|
) -> AppResult<Json<TagCatalogEntry>> {
|
||||||
|
let existing: Tag = tags::table
|
||||||
|
.find(tag_id)
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.one()?;
|
||||||
|
let label_class = classify_nullable(body.get("label")).map_err(AppError::bad_request)?;
|
||||||
|
let color_class = classify_nullable(body.get("color")).map_err(AppError::bad_request)?;
|
||||||
|
|
||||||
|
if matches!(label_class, NullableValue::Omitted)
|
||||||
|
&& matches!(color_class, NullableValue::Omitted)
|
||||||
|
{
|
||||||
|
let usage_count: i64 = document_tags::table
|
||||||
|
.filter(document_tags::tag_id.eq(tag_id))
|
||||||
|
.select(count_star())
|
||||||
|
.first(&mut conn)?;
|
||||||
|
return TagCatalogEntry {
|
||||||
|
id: existing.id,
|
||||||
|
label: existing.label.clone(),
|
||||||
|
color: existing.color.clone(),
|
||||||
|
usage_count,
|
||||||
|
}
|
||||||
|
.into_json();
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut new_label: Option<String> = None;
|
||||||
|
let mut label_changed = false;
|
||||||
|
match label_class {
|
||||||
|
NullableValue::Omitted => {}
|
||||||
|
NullableValue::Null => {
|
||||||
|
return Err(AppError::bad_request("label cannot be null"));
|
||||||
|
}
|
||||||
|
NullableValue::String(value) => {
|
||||||
|
let trimmed = value.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(AppError::bad_request("label must not be empty"));
|
||||||
|
}
|
||||||
|
if trimmed != existing.label {
|
||||||
|
let duplicate = tags::table
|
||||||
|
.filter(tags::label.eq(trimmed))
|
||||||
|
.filter(tags::id.ne(tag_id))
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id))
|
||||||
|
.first::<Tag>(&mut conn)
|
||||||
|
.optional()?;
|
||||||
|
if duplicate.is_some() {
|
||||||
|
return Err(AppError::bad_request("tag label already exists"));
|
||||||
|
}
|
||||||
|
new_label = Some(trimmed.to_string());
|
||||||
|
label_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut color_change: Option<Option<String>> = None;
|
||||||
|
let mut color_changed = false;
|
||||||
|
match color_class {
|
||||||
|
NullableValue::Omitted => {}
|
||||||
|
NullableValue::Null => {
|
||||||
|
color_change = Some(None);
|
||||||
|
color_changed = true;
|
||||||
|
}
|
||||||
|
NullableValue::String(value) => {
|
||||||
|
let trimmed = value.trim();
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
return Err(AppError::bad_request("color must not be empty"));
|
||||||
|
}
|
||||||
|
if existing.color.as_deref() != Some(trimmed) {
|
||||||
|
color_change = Some(Some(trimmed.to_string()));
|
||||||
|
color_changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !label_changed && !color_changed {
|
||||||
|
let usage_count: i64 = document_tags::table
|
||||||
|
.filter(document_tags::tag_id.eq(tag_id))
|
||||||
|
.filter(document_tags::tenant_id.eq(tenant_id))
|
||||||
|
.select(count_star())
|
||||||
|
.first(&mut conn)?;
|
||||||
|
return Ok(Json(TagCatalogEntry {
|
||||||
|
id: existing.id,
|
||||||
|
label: existing.label.clone(),
|
||||||
|
color: existing.color.clone(),
|
||||||
|
usage_count,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
let changeset = UpdateTagChangeset {
|
||||||
|
label: new_label.as_deref(),
|
||||||
|
color: color_change
|
||||||
|
.as_ref()
|
||||||
|
.map(|opt| opt.as_ref().map(|value| value.as_str())),
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::update(
|
||||||
|
tags::table
|
||||||
|
.find(tag_id)
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.set(&changeset)
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
|
||||||
|
let updated: Tag = tags::table
|
||||||
|
.find(tag_id)
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.one()?;
|
||||||
|
let usage_count: i64 = document_tags::table
|
||||||
|
.filter(document_tags::tag_id.eq(tag_id))
|
||||||
|
.filter(document_tags::tenant_id.eq(tenant_id))
|
||||||
|
.select(count_star())
|
||||||
|
.first(&mut conn)?;
|
||||||
|
|
||||||
|
TagCatalogEntry {
|
||||||
|
id: updated.id,
|
||||||
|
label: updated.label,
|
||||||
|
color: updated.color,
|
||||||
|
usage_count,
|
||||||
|
}
|
||||||
|
.into_json()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_tag(
|
||||||
|
Path(tag_id): Path<Uuid>,
|
||||||
|
TenantScopedConn {
|
||||||
|
mut conn,
|
||||||
|
tenant_id,
|
||||||
|
..
|
||||||
|
}: TenantScopedConn,
|
||||||
|
) -> AppResult<StatusCode> {
|
||||||
|
let usage: i64 = document_tags::table
|
||||||
|
.filter(document_tags::tag_id.eq(tag_id))
|
||||||
|
.filter(document_tags::tenant_id.eq(tenant_id))
|
||||||
|
.select(count_star())
|
||||||
|
.first(&mut conn)?;
|
||||||
|
|
||||||
|
if usage > 0 {
|
||||||
|
return Err(AppError::bad_request(
|
||||||
|
"cannot delete tag that is still assigned to documents",
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let deleted = diesel::delete(
|
||||||
|
tags::table
|
||||||
|
.find(tag_id)
|
||||||
|
.filter(tags::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.execute(&mut conn)?;
|
||||||
|
if deleted == 0 {
|
||||||
|
return Err(AppError::not_found());
|
||||||
|
}
|
||||||
|
|
||||||
|
no_content()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,889 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use axum::body::Body;
|
||||||
|
use axum::extract::State;
|
||||||
|
use axum::http::{header, HeaderMap, Method, StatusCode};
|
||||||
|
use axum::response::Response;
|
||||||
|
use axum::Router;
|
||||||
|
use base64::engine::general_purpose::STANDARD as BASE64;
|
||||||
|
use base64::Engine;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use diesel::PgConnection;
|
||||||
|
use futures_util::StreamExt;
|
||||||
|
use percent_encoding::{percent_decode_str, utf8_percent_encode, NON_ALPHANUMERIC};
|
||||||
|
use quick_xml::events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event};
|
||||||
|
use quick_xml::Writer;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::auth::password;
|
||||||
|
use crate::error::{AppError, AppResult};
|
||||||
|
use crate::models::{Document, DocumentVersion, Folder, User};
|
||||||
|
use crate::schema::{
|
||||||
|
document_versions::dsl as document_versions_dsl, documents::dsl as documents_dsl,
|
||||||
|
folders::dsl as folders_dsl, tenants::dsl as tenant_dsl,
|
||||||
|
user_memberships::dsl as memberships_dsl, users::dsl as users_dsl,
|
||||||
|
};
|
||||||
|
use crate::state::AppState;
|
||||||
|
use crate::utils::{http::inline_content_disposition, time::to_http_date};
|
||||||
|
|
||||||
|
const REALM: &str = "Papercrate WebDAV";
|
||||||
|
const DOWNLOAD_URL_TTL_SECONDS: u64 = 300;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct TenantEntry {
|
||||||
|
tenant_id: Uuid,
|
||||||
|
slug: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
struct WebDavContext {
|
||||||
|
_user_id: Uuid,
|
||||||
|
_username: String,
|
||||||
|
tenants: Vec<TenantEntry>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn create_router() -> Router<AppState> {
|
||||||
|
Router::new().fallback(webdav_entrypoint)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn webdav_entrypoint(
|
||||||
|
State(state): State<AppState>,
|
||||||
|
req: axum::http::Request<axum::body::Body>,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let method = req.method().clone();
|
||||||
|
let headers = req.headers().clone();
|
||||||
|
let path = req.uri().path().trim_start_matches('/').to_string();
|
||||||
|
|
||||||
|
tracing::debug!(method = %method, %path, "webdav entrypoint" );
|
||||||
|
|
||||||
|
match method {
|
||||||
|
ref m if m == Method::OPTIONS => Ok(handle_options()),
|
||||||
|
ref m if m == Method::GET => handle_get_or_head(&state, &path, headers, Method::GET).await,
|
||||||
|
ref m if m == Method::HEAD => {
|
||||||
|
handle_get_or_head(&state, &path, headers, Method::HEAD).await
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
if method.as_str() == "PROPFIND" {
|
||||||
|
handle_propfind(&state, &path, headers).await
|
||||||
|
} else {
|
||||||
|
Ok(method_not_allowed())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_propfind(
|
||||||
|
state: &AppState,
|
||||||
|
path: &str,
|
||||||
|
headers: HeaderMap,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let context = match authenticate(state, &headers)? {
|
||||||
|
Some(user) => user,
|
||||||
|
None => return Ok(unauthorized_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let depth = match parse_depth(&headers) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(response) => return Ok(response),
|
||||||
|
};
|
||||||
|
|
||||||
|
let segments = parse_segments(path)?;
|
||||||
|
|
||||||
|
let resources = if segments.is_empty() {
|
||||||
|
build_account_root_resources(&context.tenants, depth)
|
||||||
|
} else {
|
||||||
|
let (requested_slug, remainder) = segments.split_first().unwrap();
|
||||||
|
let tenant_entry = match context
|
||||||
|
.tenants
|
||||||
|
.iter()
|
||||||
|
.find(|entry| entry.slug.eq_ignore_ascii_case(requested_slug))
|
||||||
|
{
|
||||||
|
Some(entry) => TenantEntry {
|
||||||
|
tenant_id: entry.tenant_id,
|
||||||
|
slug: entry.slug.clone(),
|
||||||
|
},
|
||||||
|
None => return Ok(not_found_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let resolution = match resolve_path(state, &tenant_entry, remainder)? {
|
||||||
|
Some(resolved) => resolved,
|
||||||
|
None => return Ok(not_found_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match resolution {
|
||||||
|
ResolvedPath::TenantRoot { chain } => {
|
||||||
|
let contents = fetch_folder_contents(state, tenant_entry.tenant_id, None)?;
|
||||||
|
build_resources_for_folder(None, &chain, &contents, depth)
|
||||||
|
}
|
||||||
|
ResolvedPath::Folder { folder, chain } => {
|
||||||
|
let contents =
|
||||||
|
fetch_folder_contents(state, tenant_entry.tenant_id, Some(folder.id))?;
|
||||||
|
build_resources_for_folder(Some(&folder), &chain, &contents, depth)
|
||||||
|
}
|
||||||
|
ResolvedPath::Document {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
chain,
|
||||||
|
} => build_resources_for_document(&chain, &document, &version),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let body = render_multistatus(&resources)
|
||||||
|
.map_err(|err| AppError::internal(format!("failed to render WebDAV response: {err}")))?;
|
||||||
|
|
||||||
|
let response = Response::builder()
|
||||||
|
.status(multi_status())
|
||||||
|
.header(header::CONTENT_TYPE, "application/xml; charset=utf-8")
|
||||||
|
.body(Body::from(body))
|
||||||
|
.expect("valid response");
|
||||||
|
|
||||||
|
Ok(response)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_get_or_head(
|
||||||
|
state: &AppState,
|
||||||
|
path: &str,
|
||||||
|
headers: HeaderMap,
|
||||||
|
method: Method,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let context = match authenticate(state, &headers)? {
|
||||||
|
Some(user) => user,
|
||||||
|
None => return Ok(unauthorized_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let segments = parse_segments(path)?;
|
||||||
|
let (requested_slug, remainder) = match segments.split_first() {
|
||||||
|
Some(values) => values,
|
||||||
|
None => return Ok(method_not_allowed()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let tenant_entry = match context
|
||||||
|
.tenants
|
||||||
|
.iter()
|
||||||
|
.find(|entry| entry.slug.eq_ignore_ascii_case(requested_slug))
|
||||||
|
{
|
||||||
|
Some(entry) => TenantEntry {
|
||||||
|
tenant_id: entry.tenant_id,
|
||||||
|
slug: entry.slug.clone(),
|
||||||
|
},
|
||||||
|
None => return Ok(not_found_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if remainder.is_empty() {
|
||||||
|
return Ok(method_not_allowed());
|
||||||
|
}
|
||||||
|
|
||||||
|
let resolution = match resolve_path(state, &tenant_entry, remainder)? {
|
||||||
|
Some(resolved) => resolved,
|
||||||
|
None => return Ok(not_found_response()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let (document, version, chain) = match resolution {
|
||||||
|
ResolvedPath::Document {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
chain,
|
||||||
|
} => (document, version, chain),
|
||||||
|
_ => return Ok(method_not_allowed()),
|
||||||
|
};
|
||||||
|
|
||||||
|
stream_document(state, &document, &version, &chain, headers, method).await
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_options() -> Response {
|
||||||
|
Response::builder()
|
||||||
|
.status(StatusCode::OK)
|
||||||
|
.header("DAV", "1,2")
|
||||||
|
.header(header::ALLOW, "OPTIONS, PROPFIND, GET, HEAD")
|
||||||
|
.header("Accept-Ranges", "bytes")
|
||||||
|
.body(Body::empty())
|
||||||
|
.expect("valid OPTIONS response")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn method_not_allowed() -> Response {
|
||||||
|
Response::builder()
|
||||||
|
.status(StatusCode::METHOD_NOT_ALLOWED)
|
||||||
|
.body(Body::empty())
|
||||||
|
.expect("valid response")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn not_found_response() -> Response {
|
||||||
|
Response::builder()
|
||||||
|
.status(StatusCode::NOT_FOUND)
|
||||||
|
.body(Body::empty())
|
||||||
|
.expect("valid response")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unauthorized_response() -> Response {
|
||||||
|
Response::builder()
|
||||||
|
.status(StatusCode::UNAUTHORIZED)
|
||||||
|
.header(
|
||||||
|
header::WWW_AUTHENTICATE,
|
||||||
|
format!("Basic realm=\"{REALM}\", charset=\"UTF-8\""),
|
||||||
|
)
|
||||||
|
.body(Body::empty())
|
||||||
|
.expect("valid response")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn multi_status() -> StatusCode {
|
||||||
|
StatusCode::from_u16(207).expect("valid multi-status")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_depth(headers: &HeaderMap) -> Result<u8, Response> {
|
||||||
|
match headers.get("Depth") {
|
||||||
|
None => Ok(1),
|
||||||
|
Some(value) => match value.to_str() {
|
||||||
|
Ok("0") => Ok(0),
|
||||||
|
Ok("1") => Ok(1),
|
||||||
|
Ok("infinity") => Err(Response::builder()
|
||||||
|
.status(StatusCode::FORBIDDEN)
|
||||||
|
.body(Body::empty())
|
||||||
|
.expect("valid response")),
|
||||||
|
_ => Err(Response::builder()
|
||||||
|
.status(StatusCode::BAD_REQUEST)
|
||||||
|
.body(Body::empty())
|
||||||
|
.expect("valid response")),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parse_segments(path: &str) -> AppResult<Vec<String>> {
|
||||||
|
if path.trim_matches('/').is_empty() {
|
||||||
|
return Ok(vec![]);
|
||||||
|
}
|
||||||
|
|
||||||
|
let segments = path
|
||||||
|
.split('/')
|
||||||
|
.filter(|segment| !segment.is_empty())
|
||||||
|
.map(|segment| {
|
||||||
|
percent_decode_str(segment)
|
||||||
|
.decode_utf8()
|
||||||
|
.map(|cow| cow.into_owned())
|
||||||
|
.map_err(|_| AppError::bad_request("invalid UTF-8 in path"))
|
||||||
|
})
|
||||||
|
.collect::<Result<Vec<_>, _>>()?;
|
||||||
|
|
||||||
|
Ok(segments)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn fetch_folder_contents(
|
||||||
|
state: &AppState,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
folder_id: Option<Uuid>,
|
||||||
|
) -> AppResult<WebDavFolderContents> {
|
||||||
|
let mut conn = state.db_for_tenant(tenant_id)?;
|
||||||
|
|
||||||
|
let folder = match folder_id {
|
||||||
|
Some(id) => Some(
|
||||||
|
folders_dsl::folders
|
||||||
|
.filter(folders_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.find(id)
|
||||||
|
.first::<Folder>(&mut conn)?,
|
||||||
|
),
|
||||||
|
None => None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let subfolders: Vec<Folder> = match folder_id {
|
||||||
|
Some(id) => folders_dsl::folders
|
||||||
|
.filter(folders_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.filter(folders_dsl::parent_id.eq(Some(id)))
|
||||||
|
.order(folders_dsl::name.asc())
|
||||||
|
.load(&mut conn)?,
|
||||||
|
None => folders_dsl::folders
|
||||||
|
.filter(folders_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.filter(folders_dsl::parent_id.is_null())
|
||||||
|
.order(folders_dsl::name.asc())
|
||||||
|
.load(&mut conn)?,
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut docs_query = documents_dsl::documents
|
||||||
|
.filter(documents_dsl::deleted_at.is_null())
|
||||||
|
.filter(documents_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.into_boxed();
|
||||||
|
|
||||||
|
docs_query = match folder_id {
|
||||||
|
Some(id) => docs_query.filter(documents_dsl::folder_id.eq(Some(id))),
|
||||||
|
None => docs_query.filter(documents_dsl::folder_id.is_null()),
|
||||||
|
};
|
||||||
|
|
||||||
|
let documents: Vec<Document> = docs_query
|
||||||
|
.order(documents_dsl::uploaded_at.desc())
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
let version_ids: Vec<Uuid> = documents.iter().map(|doc| doc.current_version_id).collect();
|
||||||
|
let versions: Vec<DocumentVersion> = if version_ids.is_empty() {
|
||||||
|
Vec::new()
|
||||||
|
} else {
|
||||||
|
document_versions_dsl::document_versions
|
||||||
|
.filter(document_versions_dsl::id.eq_any(&version_ids))
|
||||||
|
.load(&mut conn)?
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut version_map = versions
|
||||||
|
.into_iter()
|
||||||
|
.map(|version| (version.id, version))
|
||||||
|
.collect::<std::collections::HashMap<_, _>>();
|
||||||
|
|
||||||
|
let mut entries = Vec::with_capacity(documents.len());
|
||||||
|
for document in documents {
|
||||||
|
if let Some(version) = version_map.remove(&document.current_version_id) {
|
||||||
|
entries.push(DocumentEntry { document, version });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(WebDavFolderContents {
|
||||||
|
_folder: folder,
|
||||||
|
subfolders,
|
||||||
|
documents: entries,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn stream_document(
|
||||||
|
state: &AppState,
|
||||||
|
document: &Document,
|
||||||
|
version: &DocumentVersion,
|
||||||
|
_chain: &[String],
|
||||||
|
headers: HeaderMap,
|
||||||
|
method: Method,
|
||||||
|
) -> Result<Response, AppError> {
|
||||||
|
let range_header = headers.get(header::RANGE).cloned();
|
||||||
|
|
||||||
|
let storage = state.storage_for_tenant(document.tenant_id)?;
|
||||||
|
|
||||||
|
let url = storage
|
||||||
|
.presign_get_object(
|
||||||
|
&version.s3_key,
|
||||||
|
Duration::from_secs(DOWNLOAD_URL_TTL_SECONDS),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map_err(|err| AppError::internal(format!("failed to presign document download: {err}")))?;
|
||||||
|
|
||||||
|
let client = reqwest::Client::new();
|
||||||
|
let mut request = client.request(method.clone(), url.clone());
|
||||||
|
|
||||||
|
if let Some(range) = range_header.clone() {
|
||||||
|
request = request.header(header::RANGE, range.clone());
|
||||||
|
}
|
||||||
|
|
||||||
|
let upstream = request
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.map_err(|err| AppError::internal(format!("failed to fetch document stream: {err}")))?;
|
||||||
|
|
||||||
|
let status =
|
||||||
|
StatusCode::from_u16(upstream.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
|
||||||
|
|
||||||
|
if !(status.is_success() || status == StatusCode::PARTIAL_CONTENT) {
|
||||||
|
return Err(AppError::internal(format!(
|
||||||
|
"upstream download returned status {status}"
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut builder = Response::builder().status(status);
|
||||||
|
|
||||||
|
if let Some(content_type) = upstream.headers().get(header::CONTENT_TYPE) {
|
||||||
|
builder = builder.header(header::CONTENT_TYPE, content_type);
|
||||||
|
} else if let Some(ref typ) = document.content_type {
|
||||||
|
builder = builder.header(header::CONTENT_TYPE, typ);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(content_length) = upstream.headers().get(header::CONTENT_LENGTH) {
|
||||||
|
builder = builder.header(header::CONTENT_LENGTH, content_length);
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(range) = upstream.headers().get(header::CONTENT_RANGE) {
|
||||||
|
builder = builder.header(header::CONTENT_RANGE, range);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder = builder.header("Accept-Ranges", "bytes");
|
||||||
|
|
||||||
|
if let Some(disposition) = inline_content_disposition(&document.filename) {
|
||||||
|
builder = builder.header(header::CONTENT_DISPOSITION, disposition);
|
||||||
|
}
|
||||||
|
|
||||||
|
builder = builder.header(header::ETAG, format!("\"{}\"", version.id));
|
||||||
|
|
||||||
|
if method == Method::HEAD {
|
||||||
|
return builder
|
||||||
|
.body(Body::empty())
|
||||||
|
.map_err(|err| AppError::internal(format!("failed to build response: {err}")));
|
||||||
|
}
|
||||||
|
|
||||||
|
let stream = upstream
|
||||||
|
.bytes_stream()
|
||||||
|
.map(|chunk| chunk.map_err(|err| std::io::Error::new(std::io::ErrorKind::Other, err)));
|
||||||
|
let body = Body::from_stream(stream);
|
||||||
|
|
||||||
|
builder
|
||||||
|
.body(body)
|
||||||
|
.map_err(|err| AppError::internal(format!("failed to build response: {err}")))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn authenticate(state: &AppState, headers: &HeaderMap) -> Result<Option<WebDavContext>, AppError> {
|
||||||
|
tracing::debug!("webdav authenticate invoked");
|
||||||
|
let authorization = match headers.get(header::AUTHORIZATION) {
|
||||||
|
Some(value) => match value.to_str() {
|
||||||
|
Ok(header) if header.starts_with("Basic ") => {
|
||||||
|
tracing::debug!("authorization header present");
|
||||||
|
&header[6..]
|
||||||
|
}
|
||||||
|
Ok(other) => {
|
||||||
|
tracing::warn!(header = %other, "non-basic authorization header");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
tracing::warn!(error = %err, "invalid authorization header");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
tracing::debug!("no authorization header");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let decoded = match BASE64.decode(authorization) {
|
||||||
|
Ok(bytes) => bytes,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::warn!(error = %err, "failed to decode basic credentials");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let credential_str = match String::from_utf8(decoded) {
|
||||||
|
Ok(value) => value,
|
||||||
|
Err(err) => {
|
||||||
|
tracing::warn!(error = %err, "invalid utf-8 basic credentials");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let (username, password) = match credential_str.split_once(':') {
|
||||||
|
Some((username, password)) if !username.is_empty() => (username, password),
|
||||||
|
_ => return Ok(None),
|
||||||
|
};
|
||||||
|
|
||||||
|
tracing::debug!(%username, "attempting webdav login");
|
||||||
|
let mut conn = state.db_unscoped()?;
|
||||||
|
|
||||||
|
let user: User = match users_dsl::users
|
||||||
|
.filter(users_dsl::username.eq(username))
|
||||||
|
.first(&mut conn)
|
||||||
|
{
|
||||||
|
Ok(user) => user,
|
||||||
|
Err(diesel::result::Error::NotFound) => {
|
||||||
|
tracing::warn!(%username, "webdav user not found");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
Err(err) => return Err(AppError::from(err)),
|
||||||
|
};
|
||||||
|
|
||||||
|
let valid = password::verify_password(password, &user.password_hash)
|
||||||
|
.map_err(|_| AppError::internal("failed to verify password"))?;
|
||||||
|
|
||||||
|
if !valid {
|
||||||
|
tracing::warn!(%username, "webdav password invalid");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let tenant_rows: Vec<(Uuid, String)> = memberships_dsl::user_memberships
|
||||||
|
.inner_join(tenant_dsl::tenants)
|
||||||
|
.filter(memberships_dsl::user_id.eq(user.id))
|
||||||
|
.select((tenant_dsl::id, tenant_dsl::slug))
|
||||||
|
.load(&mut conn)?;
|
||||||
|
|
||||||
|
if tenant_rows.is_empty() {
|
||||||
|
tracing::warn!(%username, "webdav user has no tenant memberships");
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
let tenants: Vec<TenantEntry> = tenant_rows
|
||||||
|
.into_iter()
|
||||||
|
.map(|(tenant_id, slug)| TenantEntry { tenant_id, slug })
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
tracing::debug!(%username, tenant_count = tenants.len(), "webdav login success");
|
||||||
|
Ok(Some(WebDavContext {
|
||||||
|
_user_id: user.id,
|
||||||
|
_username: user.username,
|
||||||
|
tenants,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_resources_for_folder(
|
||||||
|
folder: Option<&Folder>,
|
||||||
|
chain: &[String],
|
||||||
|
contents: &WebDavFolderContents,
|
||||||
|
depth: u8,
|
||||||
|
) -> Vec<DavResource> {
|
||||||
|
let mut resources = Vec::new();
|
||||||
|
|
||||||
|
let display_name = folder
|
||||||
|
.map(|folder| folder.name.clone())
|
||||||
|
.unwrap_or_else(|| chain.last().cloned().unwrap_or_else(|| "/".to_string()));
|
||||||
|
|
||||||
|
let href = build_href(chain, true);
|
||||||
|
let last_modified = folder.map(|folder| to_http_date(folder.updated_at));
|
||||||
|
|
||||||
|
resources.push(DavResource {
|
||||||
|
href,
|
||||||
|
display_name,
|
||||||
|
is_collection: true,
|
||||||
|
content_length: None,
|
||||||
|
content_type: None,
|
||||||
|
last_modified,
|
||||||
|
});
|
||||||
|
|
||||||
|
if depth == 0 {
|
||||||
|
return resources;
|
||||||
|
}
|
||||||
|
|
||||||
|
for subfolder in &contents.subfolders {
|
||||||
|
let mut child_chain = chain.to_vec();
|
||||||
|
child_chain.push(subfolder.name.clone());
|
||||||
|
resources.push(DavResource {
|
||||||
|
href: build_href(&child_chain, true),
|
||||||
|
display_name: subfolder.name.clone(),
|
||||||
|
is_collection: true,
|
||||||
|
content_length: None,
|
||||||
|
content_type: None,
|
||||||
|
last_modified: Some(to_http_date(subfolder.updated_at)),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for entry in &contents.documents {
|
||||||
|
let mut child_chain = chain.to_vec();
|
||||||
|
child_chain.push(entry.document.filename.clone());
|
||||||
|
resources.push(document_to_resource(
|
||||||
|
&child_chain,
|
||||||
|
&entry.document,
|
||||||
|
&entry.version,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
resources
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_account_root_resources(tenants: &[TenantEntry], depth: u8) -> Vec<DavResource> {
|
||||||
|
let mut resources = Vec::new();
|
||||||
|
|
||||||
|
resources.push(DavResource {
|
||||||
|
href: "/".to_string(),
|
||||||
|
display_name: "/".to_string(),
|
||||||
|
is_collection: true,
|
||||||
|
content_length: None,
|
||||||
|
content_type: None,
|
||||||
|
last_modified: None,
|
||||||
|
});
|
||||||
|
|
||||||
|
if depth == 0 {
|
||||||
|
return resources;
|
||||||
|
}
|
||||||
|
|
||||||
|
for tenant in tenants {
|
||||||
|
let href = build_href(&[tenant.slug.clone()], true);
|
||||||
|
resources.push(DavResource {
|
||||||
|
href,
|
||||||
|
display_name: tenant.slug.clone(),
|
||||||
|
is_collection: true,
|
||||||
|
content_length: None,
|
||||||
|
content_type: None,
|
||||||
|
last_modified: None,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
resources
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_resources_for_document(
|
||||||
|
chain: &[String],
|
||||||
|
document: &Document,
|
||||||
|
version: &DocumentVersion,
|
||||||
|
) -> Vec<DavResource> {
|
||||||
|
vec![document_to_resource(chain, document, version)]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn document_to_resource(
|
||||||
|
chain: &[String],
|
||||||
|
document: &Document,
|
||||||
|
version: &DocumentVersion,
|
||||||
|
) -> DavResource {
|
||||||
|
let href = build_href(chain, false);
|
||||||
|
|
||||||
|
DavResource {
|
||||||
|
href,
|
||||||
|
display_name: document.title.clone(),
|
||||||
|
is_collection: false,
|
||||||
|
content_length: Some(version.size_bytes),
|
||||||
|
content_type: document.content_type.clone(),
|
||||||
|
last_modified: Some(to_http_date(document.updated_at)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_href(names: &[String], is_collection: bool) -> String {
|
||||||
|
if names.is_empty() {
|
||||||
|
return "/".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
let encoded = names
|
||||||
|
.iter()
|
||||||
|
.map(|name| utf8_percent_encode(name, NON_ALPHANUMERIC).to_string())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
let mut path = format!("/{}", encoded.join("/"));
|
||||||
|
if is_collection && !path.ends_with('/') {
|
||||||
|
path.push('/');
|
||||||
|
}
|
||||||
|
path
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_multistatus(resources: &[DavResource]) -> Result<Vec<u8>, quick_xml::Error> {
|
||||||
|
let mut writer = Writer::new(Vec::new());
|
||||||
|
writer.write_event(Event::Decl(BytesDecl::new("1.0", Some("UTF-8"), None)))?;
|
||||||
|
|
||||||
|
let mut multistatus = BytesStart::new("D:multistatus");
|
||||||
|
multistatus.push_attribute(("xmlns:D", "DAV:"));
|
||||||
|
writer.write_event(Event::Start(multistatus))?;
|
||||||
|
|
||||||
|
for resource in resources {
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:response")))?;
|
||||||
|
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:href")))?;
|
||||||
|
writer.write_event(Event::Text(BytesText::new(&resource.href)))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:href")))?;
|
||||||
|
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:propstat")))?;
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:prop")))?;
|
||||||
|
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:displayname")))?;
|
||||||
|
writer.write_event(Event::Text(BytesText::new(&resource.display_name)))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:displayname")))?;
|
||||||
|
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:resourcetype")))?;
|
||||||
|
if resource.is_collection {
|
||||||
|
writer.write_event(Event::Empty(BytesStart::new("D:collection")))?;
|
||||||
|
}
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:resourcetype")))?;
|
||||||
|
|
||||||
|
if let Some(length) = resource.content_length {
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:getcontentlength")))?;
|
||||||
|
writer.write_event(Event::Text(BytesText::new(&length.to_string())))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:getcontentlength")))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(content_type) = &resource.content_type {
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:getcontenttype")))?;
|
||||||
|
writer.write_event(Event::Text(BytesText::new(content_type)))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:getcontenttype")))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(last_modified) = &resource.last_modified {
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:getlastmodified")))?;
|
||||||
|
writer.write_event(Event::Text(BytesText::new(last_modified)))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:getlastmodified")))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:prop")))?;
|
||||||
|
|
||||||
|
writer.write_event(Event::Start(BytesStart::new("D:status")))?;
|
||||||
|
writer.write_event(Event::Text(BytesText::new("HTTP/1.1 200 OK")))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:status")))?;
|
||||||
|
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:propstat")))?;
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:response")))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.write_event(Event::End(BytesEnd::new("D:multistatus")))?;
|
||||||
|
Ok(writer.into_inner())
|
||||||
|
}
|
||||||
|
|
||||||
|
struct WebDavFolderContents {
|
||||||
|
_folder: Option<Folder>,
|
||||||
|
subfolders: Vec<Folder>,
|
||||||
|
documents: Vec<DocumentEntry>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DocumentEntry {
|
||||||
|
document: Document,
|
||||||
|
version: DocumentVersion,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct DavResource {
|
||||||
|
href: String,
|
||||||
|
display_name: String,
|
||||||
|
is_collection: bool,
|
||||||
|
content_length: Option<i64>,
|
||||||
|
content_type: Option<String>,
|
||||||
|
last_modified: Option<String>,
|
||||||
|
}
|
||||||
|
enum ResolvedPath {
|
||||||
|
TenantRoot {
|
||||||
|
chain: Vec<String>,
|
||||||
|
},
|
||||||
|
Folder {
|
||||||
|
folder: Folder,
|
||||||
|
chain: Vec<String>,
|
||||||
|
},
|
||||||
|
Document {
|
||||||
|
document: Document,
|
||||||
|
version: DocumentVersion,
|
||||||
|
chain: Vec<String>,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_path(
|
||||||
|
state: &AppState,
|
||||||
|
tenant: &TenantEntry,
|
||||||
|
segments: &[String],
|
||||||
|
) -> AppResult<Option<ResolvedPath>> {
|
||||||
|
let mut conn = state.db_for_tenant(tenant.tenant_id)?;
|
||||||
|
let mut parent_id: Option<Uuid> = None;
|
||||||
|
let mut chain: Vec<String> = vec![tenant.slug.clone()];
|
||||||
|
let mut current_folder: Option<Folder> = None;
|
||||||
|
|
||||||
|
if segments.is_empty() {
|
||||||
|
return Ok(Some(ResolvedPath::TenantRoot { chain }));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (index, segment) in segments.iter().enumerate() {
|
||||||
|
let is_last = index == segments.len() - 1;
|
||||||
|
|
||||||
|
if let Some(folder) = find_folder_by_name(&mut conn, tenant.tenant_id, parent_id, segment)?
|
||||||
|
{
|
||||||
|
chain.push(folder.name.clone());
|
||||||
|
if is_last {
|
||||||
|
return Ok(Some(ResolvedPath::Folder { folder, chain }));
|
||||||
|
}
|
||||||
|
parent_id = Some(folder.id);
|
||||||
|
current_folder = Some(folder);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if is_last {
|
||||||
|
if let Some((document, version)) =
|
||||||
|
find_document_by_filename(&mut conn, tenant.tenant_id, parent_id, segment)?
|
||||||
|
{
|
||||||
|
chain.push(document.filename.clone());
|
||||||
|
return Ok(Some(ResolvedPath::Document {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
chain,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(uuid) = Uuid::parse_str(segment) {
|
||||||
|
if let Some(folder) = find_folder_by_id(&mut conn, tenant.tenant_id, uuid)? {
|
||||||
|
if folder.parent_id != parent_id {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
chain.push(folder.name.clone());
|
||||||
|
if is_last {
|
||||||
|
return Ok(Some(ResolvedPath::Folder { folder, chain }));
|
||||||
|
}
|
||||||
|
parent_id = Some(folder.id);
|
||||||
|
current_folder = Some(folder);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some((document, version)) =
|
||||||
|
find_document_by_id(&mut conn, tenant.tenant_id, uuid)?
|
||||||
|
{
|
||||||
|
if document.folder_id != parent_id {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
chain.push(document.filename.clone());
|
||||||
|
return Ok(Some(ResolvedPath::Document {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
chain,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(current_folder.map(|folder| ResolvedPath::Folder { folder, chain }))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_folder_by_name(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
parent_id: Option<Uuid>,
|
||||||
|
name: &str,
|
||||||
|
) -> AppResult<Option<Folder>> {
|
||||||
|
let mut query = folders_dsl::folders
|
||||||
|
.filter(folders_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.into_boxed();
|
||||||
|
|
||||||
|
query = match parent_id {
|
||||||
|
Some(parent) => query.filter(folders_dsl::parent_id.eq(Some(parent))),
|
||||||
|
None => query.filter(folders_dsl::parent_id.is_null()),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(query
|
||||||
|
.filter(folders_dsl::name.eq(name))
|
||||||
|
.first::<Folder>(conn)
|
||||||
|
.optional()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_folder_by_id(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
folder_id: Uuid,
|
||||||
|
) -> AppResult<Option<Folder>> {
|
||||||
|
Ok(folders_dsl::folders
|
||||||
|
.filter(folders_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.find(folder_id)
|
||||||
|
.first::<Folder>(conn)
|
||||||
|
.optional()?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_document_by_filename(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
parent_id: Option<Uuid>,
|
||||||
|
filename: &str,
|
||||||
|
) -> AppResult<Option<(Document, DocumentVersion)>> {
|
||||||
|
let mut query = documents_dsl::documents
|
||||||
|
.filter(documents_dsl::deleted_at.is_null())
|
||||||
|
.filter(documents_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.filter(documents_dsl::filename.eq(filename))
|
||||||
|
.into_boxed();
|
||||||
|
|
||||||
|
query = match parent_id {
|
||||||
|
Some(parent) => query.filter(documents_dsl::folder_id.eq(Some(parent))),
|
||||||
|
None => query.filter(documents_dsl::folder_id.is_null()),
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(document) = query.first::<Document>(conn).optional()? {
|
||||||
|
let version = document_versions_dsl::document_versions
|
||||||
|
.find(document.current_version_id)
|
||||||
|
.first::<DocumentVersion>(conn)?;
|
||||||
|
return Ok(Some((document, version)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_document_by_id(
|
||||||
|
conn: &mut PgConnection,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
document_id: Uuid,
|
||||||
|
) -> AppResult<Option<(Document, DocumentVersion)>> {
|
||||||
|
if let Some(document) = documents_dsl::documents
|
||||||
|
.filter(documents_dsl::deleted_at.is_null())
|
||||||
|
.filter(documents_dsl::tenant_id.eq(tenant_id))
|
||||||
|
.find(document_id)
|
||||||
|
.first::<Document>(conn)
|
||||||
|
.optional()?
|
||||||
|
{
|
||||||
|
let version = document_versions_dsl::document_versions
|
||||||
|
.find(document.current_version_id)
|
||||||
|
.first::<DocumentVersion>(conn)?;
|
||||||
|
return Ok(Some((document, version)));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
+107
-9
@@ -1,16 +1,50 @@
|
|||||||
// @generated automatically by Diesel CLI.
|
// @generated automatically by Diesel CLI.
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
correspondents (id) {
|
||||||
|
id -> Uuid,
|
||||||
|
#[max_length = 255]
|
||||||
|
name -> Varchar,
|
||||||
|
metadata -> Jsonb,
|
||||||
|
created_at -> Timestamptz,
|
||||||
|
updated_at -> Timestamptz,
|
||||||
|
tenant_id -> Uuid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
document_asset_objects (id) {
|
||||||
|
id -> Uuid,
|
||||||
|
asset_id -> Uuid,
|
||||||
|
ordinal -> Int4,
|
||||||
|
s3_key -> Text,
|
||||||
|
metadata -> Jsonb,
|
||||||
|
tenant_id -> Uuid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
diesel::table! {
|
diesel::table! {
|
||||||
document_assets (id) {
|
document_assets (id) {
|
||||||
id -> Uuid,
|
id -> Uuid,
|
||||||
document_version_id -> Uuid,
|
document_version_id -> Uuid,
|
||||||
asset_type -> Text,
|
asset_type -> Text,
|
||||||
s3_key -> Text,
|
|
||||||
mime_type -> Text,
|
mime_type -> Text,
|
||||||
width -> Nullable<Int4>,
|
|
||||||
height -> Nullable<Int4>,
|
|
||||||
metadata -> Jsonb,
|
metadata -> Jsonb,
|
||||||
created_at -> Timestamptz,
|
created_at -> Timestamptz,
|
||||||
|
cardinality -> Nullable<Int4>,
|
||||||
|
tenant_id -> Uuid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
document_correspondents (document_id, correspondent_id, role) {
|
||||||
|
document_id -> Uuid,
|
||||||
|
correspondent_id -> Uuid,
|
||||||
|
#[max_length = 32]
|
||||||
|
role -> Varchar,
|
||||||
|
assigned_at -> Timestamptz,
|
||||||
|
assigned_by -> Nullable<Uuid>,
|
||||||
|
tenant_id -> Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,6 +54,7 @@ diesel::table! {
|
|||||||
tag_id -> Uuid,
|
tag_id -> Uuid,
|
||||||
assigned_at -> Timestamptz,
|
assigned_at -> Timestamptz,
|
||||||
assigned_by -> Nullable<Uuid>,
|
assigned_by -> Nullable<Uuid>,
|
||||||
|
tenant_id -> Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,6 +70,8 @@ diesel::table! {
|
|||||||
checksum -> Varchar,
|
checksum -> Varchar,
|
||||||
created_at -> Timestamptz,
|
created_at -> Timestamptz,
|
||||||
operations_summary -> Jsonb,
|
operations_summary -> Jsonb,
|
||||||
|
metadata -> Jsonb,
|
||||||
|
tenant_id -> Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +85,6 @@ diesel::table! {
|
|||||||
#[max_length = 100]
|
#[max_length = 100]
|
||||||
content_type -> Nullable<Varchar>,
|
content_type -> Nullable<Varchar>,
|
||||||
folder_id -> Nullable<Uuid>,
|
folder_id -> Nullable<Uuid>,
|
||||||
current_version -> Int4,
|
|
||||||
uploaded_at -> Timestamptz,
|
uploaded_at -> Timestamptz,
|
||||||
updated_at -> Timestamptz,
|
updated_at -> Timestamptz,
|
||||||
deleted_at -> Nullable<Timestamptz>,
|
deleted_at -> Nullable<Timestamptz>,
|
||||||
@@ -56,6 +92,8 @@ diesel::table! {
|
|||||||
issued_at -> Nullable<Timestamptz>,
|
issued_at -> Nullable<Timestamptz>,
|
||||||
#[max_length = 255]
|
#[max_length = 255]
|
||||||
title -> Varchar,
|
title -> Varchar,
|
||||||
|
current_version_id -> Uuid,
|
||||||
|
tenant_id -> Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,10 +103,9 @@ diesel::table! {
|
|||||||
#[max_length = 255]
|
#[max_length = 255]
|
||||||
name -> Varchar,
|
name -> Varchar,
|
||||||
parent_id -> Nullable<Uuid>,
|
parent_id -> Nullable<Uuid>,
|
||||||
#[max_length = 1000]
|
|
||||||
path_cache -> Nullable<Varchar>,
|
|
||||||
created_at -> Timestamptz,
|
created_at -> Timestamptz,
|
||||||
updated_at -> Timestamptz,
|
updated_at -> Timestamptz,
|
||||||
|
tenant_id -> Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,6 +120,21 @@ diesel::table! {
|
|||||||
last_error -> Nullable<Text>,
|
last_error -> Nullable<Text>,
|
||||||
created_at -> Timestamptz,
|
created_at -> Timestamptz,
|
||||||
updated_at -> Timestamptz,
|
updated_at -> Timestamptz,
|
||||||
|
tenant_id -> Uuid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
refresh_tokens (id) {
|
||||||
|
id -> Uuid,
|
||||||
|
user_id -> Uuid,
|
||||||
|
token_hash -> Text,
|
||||||
|
issued_at -> Timestamptz,
|
||||||
|
expires_at -> Timestamptz,
|
||||||
|
revoked_at -> Nullable<Timestamptz>,
|
||||||
|
created_at -> Timestamptz,
|
||||||
|
updated_at -> Timestamptz,
|
||||||
|
tenant_id -> Uuid,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,6 +146,31 @@ diesel::table! {
|
|||||||
#[max_length = 7]
|
#[max_length = 7]
|
||||||
color -> Nullable<Varchar>,
|
color -> Nullable<Varchar>,
|
||||||
created_at -> Timestamptz,
|
created_at -> Timestamptz,
|
||||||
|
tenant_id -> Uuid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
tenants (id) {
|
||||||
|
id -> Uuid,
|
||||||
|
slug -> Text,
|
||||||
|
storage_root -> Nullable<Text>,
|
||||||
|
quickwit_index -> Nullable<Text>,
|
||||||
|
status -> Text,
|
||||||
|
config -> Jsonb,
|
||||||
|
created_at -> Timestamptz,
|
||||||
|
updated_at -> Timestamptz,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
diesel::table! {
|
||||||
|
user_memberships (id) {
|
||||||
|
id -> Uuid,
|
||||||
|
user_id -> Uuid,
|
||||||
|
tenant_id -> Uuid,
|
||||||
|
role -> Text,
|
||||||
|
created_at -> Timestamptz,
|
||||||
|
updated_at -> Timestamptz,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,27 +181,48 @@ diesel::table! {
|
|||||||
username -> Varchar,
|
username -> Varchar,
|
||||||
#[max_length = 255]
|
#[max_length = 255]
|
||||||
password_hash -> Varchar,
|
password_hash -> Varchar,
|
||||||
#[max_length = 16]
|
|
||||||
role -> Varchar,
|
|
||||||
created_at -> Timestamptz,
|
created_at -> Timestamptz,
|
||||||
updated_at -> Timestamptz,
|
updated_at -> Timestamptz,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
diesel::joinable!(correspondents -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(document_asset_objects -> document_assets (asset_id));
|
||||||
|
diesel::joinable!(document_asset_objects -> tenants (tenant_id));
|
||||||
diesel::joinable!(document_assets -> document_versions (document_version_id));
|
diesel::joinable!(document_assets -> document_versions (document_version_id));
|
||||||
|
diesel::joinable!(document_assets -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(document_correspondents -> correspondents (correspondent_id));
|
||||||
|
diesel::joinable!(document_correspondents -> documents (document_id));
|
||||||
|
diesel::joinable!(document_correspondents -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(document_correspondents -> users (assigned_by));
|
||||||
diesel::joinable!(document_tags -> documents (document_id));
|
diesel::joinable!(document_tags -> documents (document_id));
|
||||||
diesel::joinable!(document_tags -> tags (tag_id));
|
diesel::joinable!(document_tags -> tags (tag_id));
|
||||||
|
diesel::joinable!(document_tags -> tenants (tenant_id));
|
||||||
diesel::joinable!(document_tags -> users (assigned_by));
|
diesel::joinable!(document_tags -> users (assigned_by));
|
||||||
diesel::joinable!(document_versions -> documents (document_id));
|
diesel::joinable!(document_versions -> tenants (tenant_id));
|
||||||
diesel::joinable!(documents -> folders (folder_id));
|
diesel::joinable!(documents -> folders (folder_id));
|
||||||
|
diesel::joinable!(documents -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(folders -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(jobs -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(refresh_tokens -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(refresh_tokens -> users (user_id));
|
||||||
|
diesel::joinable!(tags -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(user_memberships -> tenants (tenant_id));
|
||||||
|
diesel::joinable!(user_memberships -> users (user_id));
|
||||||
|
|
||||||
diesel::allow_tables_to_appear_in_same_query!(
|
diesel::allow_tables_to_appear_in_same_query!(
|
||||||
|
correspondents,
|
||||||
|
document_asset_objects,
|
||||||
document_assets,
|
document_assets,
|
||||||
|
document_correspondents,
|
||||||
document_tags,
|
document_tags,
|
||||||
document_versions,
|
document_versions,
|
||||||
documents,
|
documents,
|
||||||
folders,
|
folders,
|
||||||
jobs,
|
jobs,
|
||||||
|
refresh_tokens,
|
||||||
tags,
|
tags,
|
||||||
|
tenants,
|
||||||
|
user_memberships,
|
||||||
users,
|
users,
|
||||||
);
|
);
|
||||||
|
|||||||
+41
-5
@@ -4,43 +4,79 @@ use diesel::{
|
|||||||
pg::PgConnection,
|
pg::PgConnection,
|
||||||
r2d2::{ConnectionManager, PooledConnection},
|
r2d2::{ConnectionManager, PooledConnection},
|
||||||
};
|
};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
auth::jwt::JwtService,
|
auth::jwt::JwtService,
|
||||||
config::AppConfig,
|
config::AppConfig,
|
||||||
db::PgPool,
|
db::PgPool,
|
||||||
error::{AppError, AppResult},
|
error::{AppError, AppResult},
|
||||||
storage::ObjectStorage,
|
storage::{ObjectStorage, TenantStorage},
|
||||||
|
tenants::{apply_tenant_guc, TenantService},
|
||||||
};
|
};
|
||||||
|
|
||||||
type PgPooledConnection = PooledConnection<ConnectionManager<PgConnection>>;
|
pub type PgPooledConnection = PooledConnection<ConnectionManager<PgConnection>>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub pool: PgPool,
|
pub pool: PgPool,
|
||||||
pub config: Arc<AppConfig>,
|
pub config: Arc<AppConfig>,
|
||||||
pub storage: Arc<dyn ObjectStorage>,
|
storage: Arc<dyn ObjectStorage>,
|
||||||
pub jwt: JwtService,
|
pub jwt: JwtService,
|
||||||
|
pub tenants: TenantService,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
|
pub async fn initialize(
|
||||||
|
config: AppConfig,
|
||||||
|
pool_size_override: Option<u32>,
|
||||||
|
) -> anyhow::Result<Self> {
|
||||||
|
let pool_size = pool_size_override.unwrap_or(config.database_max_pool_size);
|
||||||
|
let pool = crate::db::init_pool_with_size(&config.database_url, pool_size)?;
|
||||||
|
let s3_client = crate::s3::build_client(&config).await?;
|
||||||
|
let storage = Arc::new(crate::storage::S3Storage::new(
|
||||||
|
s3_client,
|
||||||
|
config.s3_bucket.clone(),
|
||||||
|
));
|
||||||
|
let jwt = crate::auth::jwt::JwtService::from_config(&config)?;
|
||||||
|
|
||||||
|
Ok(Self::new(pool, config, storage, jwt))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn new(
|
pub fn new(
|
||||||
pool: PgPool,
|
pool: PgPool,
|
||||||
config: AppConfig,
|
config: AppConfig,
|
||||||
storage: Arc<dyn ObjectStorage>,
|
storage: Arc<dyn ObjectStorage>,
|
||||||
jwt: JwtService,
|
jwt: JwtService,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
let config = Arc::new(config);
|
||||||
|
let tenants = TenantService::new(pool.clone());
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
pool,
|
pool,
|
||||||
config: Arc::new(config),
|
config,
|
||||||
storage,
|
storage,
|
||||||
jwt,
|
jwt,
|
||||||
|
tenants,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn db(&self) -> AppResult<PgPooledConnection> {
|
pub fn db_for_tenant(&self, tenant_id: Uuid) -> AppResult<PgPooledConnection> {
|
||||||
|
debug_assert!(!tenant_id.is_nil(), "nil tenant_id passed to db_for_tenant");
|
||||||
|
let mut conn = self.db_unscoped()?;
|
||||||
|
apply_tenant_guc(&mut conn, tenant_id)?;
|
||||||
|
Ok(conn)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn db_unscoped(&self) -> AppResult<PgPooledConnection> {
|
||||||
self.pool
|
self.pool
|
||||||
.get()
|
.get()
|
||||||
.map_err(|err| AppError::internal(format!("database pool error: {err}")))
|
.map_err(|err| AppError::internal(format!("database pool error: {err}")))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn storage_for_tenant(&self, tenant_id: Uuid) -> AppResult<TenantStorage> {
|
||||||
|
let tenant = self.tenants.get_by_id(tenant_id)?;
|
||||||
|
TenantStorage::new(self.storage.clone(), &tenant)
|
||||||
|
.map_err(|err| AppError::internal(format!("tenant storage error: {err}")))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+73
-1
@@ -1,11 +1,15 @@
|
|||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Context, Result};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use aws_sdk_s3::presigning::PresigningConfig;
|
use aws_sdk_s3::presigning::PresigningConfig;
|
||||||
use aws_sdk_s3::primitives::ByteStream;
|
use aws_sdk_s3::primitives::ByteStream;
|
||||||
use aws_sdk_s3::Client as S3Client;
|
use aws_sdk_s3::Client as S3Client;
|
||||||
|
|
||||||
|
use crate::models::Tenant;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait ObjectStorage: Send + Sync + 'static {
|
pub trait ObjectStorage: Send + Sync + 'static {
|
||||||
async fn put_object(
|
async fn put_object(
|
||||||
@@ -13,11 +17,14 @@ pub trait ObjectStorage: Send + Sync + 'static {
|
|||||||
key: &str,
|
key: &str,
|
||||||
bytes: Vec<u8>,
|
bytes: Vec<u8>,
|
||||||
content_type: Option<String>,
|
content_type: Option<String>,
|
||||||
|
content_disposition: Option<String>,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
async fn presign_get_object(&self, key: &str, expires_in: Duration) -> Result<String>;
|
async fn presign_get_object(&self, key: &str, expires_in: Duration) -> Result<String>;
|
||||||
|
|
||||||
async fn get_object(&self, key: &str) -> Result<Vec<u8>>;
|
async fn get_object(&self, key: &str) -> Result<Vec<u8>>;
|
||||||
|
|
||||||
|
async fn delete_object(&self, key: &str) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct S3Storage {
|
pub struct S3Storage {
|
||||||
@@ -41,6 +48,7 @@ impl ObjectStorage for S3Storage {
|
|||||||
key: &str,
|
key: &str,
|
||||||
bytes: Vec<u8>,
|
bytes: Vec<u8>,
|
||||||
content_type: Option<String>,
|
content_type: Option<String>,
|
||||||
|
content_disposition: Option<String>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut request = self
|
let mut request = self
|
||||||
.client
|
.client
|
||||||
@@ -53,6 +61,10 @@ impl ObjectStorage for S3Storage {
|
|||||||
request = request.content_type(content_type);
|
request = request.content_type(content_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(content_disposition) = content_disposition {
|
||||||
|
request = request.content_disposition(content_disposition);
|
||||||
|
}
|
||||||
|
|
||||||
request
|
request
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
@@ -99,4 +111,64 @@ impl ObjectStorage for S3Storage {
|
|||||||
|
|
||||||
Ok(bytes)
|
Ok(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn delete_object(&self, key: &str) -> Result<()> {
|
||||||
|
self.client
|
||||||
|
.delete_object()
|
||||||
|
.bucket(&self.bucket)
|
||||||
|
.key(key)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.context("failed to delete object from S3")?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TenantStorage {
|
||||||
|
inner: Arc<dyn ObjectStorage>,
|
||||||
|
root: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TenantStorage {
|
||||||
|
pub fn new(inner: Arc<dyn ObjectStorage>, tenant: &Tenant) -> Result<Self> {
|
||||||
|
let root = tenant
|
||||||
|
.storage_root
|
||||||
|
.as_ref()
|
||||||
|
.ok_or_else(|| anyhow!("tenant {} missing storage_root", tenant.id))?
|
||||||
|
.to_owned();
|
||||||
|
|
||||||
|
Ok(Self { inner, root })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn qualify(&self, key: &str) -> String {
|
||||||
|
format!("{}{}", self.root, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn put_object(
|
||||||
|
&self,
|
||||||
|
key: &str,
|
||||||
|
bytes: Vec<u8>,
|
||||||
|
content_type: Option<String>,
|
||||||
|
content_disposition: Option<String>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let qualified = self.qualify(key);
|
||||||
|
self.inner
|
||||||
|
.put_object(&qualified, bytes, content_type, content_disposition)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn presign_get_object(&self, key: &str, expires_in: Duration) -> Result<String> {
|
||||||
|
let qualified = self.qualify(key);
|
||||||
|
self.inner.presign_get_object(&qualified, expires_in).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_object(&self, key: &str) -> Result<Vec<u8>> {
|
||||||
|
let qualified = self.qualify(key);
|
||||||
|
self.inner.get_object(&qualified).await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_object(&self, key: &str) -> Result<()> {
|
||||||
|
let qualified = self.qualify(key);
|
||||||
|
self.inner.delete_object(&qualified).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,91 @@
|
|||||||
|
use axum::{async_trait, extract::FromRequestParts, http::request::Parts};
|
||||||
|
use diesel::{pg::PgConnection, prelude::*, sql_types::Text};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
db::PgPool,
|
||||||
|
error::{AppError, AppResult},
|
||||||
|
models::Tenant,
|
||||||
|
schema::tenants::dsl,
|
||||||
|
state::AppState,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct TenantRepository;
|
||||||
|
|
||||||
|
impl TenantRepository {
|
||||||
|
pub fn get_by_id(conn: &mut PgConnection, tenant_id: Uuid) -> AppResult<Tenant> {
|
||||||
|
dsl::tenants.find(tenant_id).first(conn).map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_by_slug(conn: &mut PgConnection, slug: &str) -> AppResult<Tenant> {
|
||||||
|
dsl::tenants
|
||||||
|
.filter(dsl::slug.eq(slug))
|
||||||
|
.first(conn)
|
||||||
|
.map_err(Into::into)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct TenantService {
|
||||||
|
pool: PgPool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TenantService {
|
||||||
|
pub fn new(pool: PgPool) -> Self {
|
||||||
|
Self { pool }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_by_id(&self, tenant_id: Uuid) -> AppResult<Tenant> {
|
||||||
|
let tenant = self.load(|conn| TenantRepository::get_by_id(conn, tenant_id))?;
|
||||||
|
Ok(tenant)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_by_slug(&self, slug: &str) -> AppResult<Tenant> {
|
||||||
|
let slug_owned = slug.to_owned();
|
||||||
|
let tenant = self.load(|conn| TenantRepository::get_by_slug(conn, &slug_owned))?;
|
||||||
|
Ok(tenant)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn tenant_id_for_slug(&self, slug: &str) -> AppResult<Uuid> {
|
||||||
|
Ok(self.get_by_slug(slug)?.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load<F>(&self, loader: F) -> AppResult<Tenant>
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut PgConnection) -> AppResult<Tenant>,
|
||||||
|
{
|
||||||
|
let mut conn = self
|
||||||
|
.pool
|
||||||
|
.get()
|
||||||
|
.map_err(|err| AppError::internal(format!("database pool error: {err}")))?;
|
||||||
|
let tenant = loader(&mut conn)?;
|
||||||
|
Ok(tenant)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn apply_tenant_guc(conn: &mut PgConnection, tenant_id: Uuid) -> AppResult<()> {
|
||||||
|
diesel::sql_query("SELECT set_config('papercrate.tenant_id', $1, true)")
|
||||||
|
.bind::<Text, _>(tenant_id.to_string())
|
||||||
|
.execute(conn)
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(AppError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TenantContext {
|
||||||
|
pub tenant: Tenant,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl FromRequestParts<AppState> for TenantContext {
|
||||||
|
type Rejection = AppError;
|
||||||
|
|
||||||
|
async fn from_request_parts(
|
||||||
|
_parts: &mut Parts,
|
||||||
|
state: &AppState,
|
||||||
|
) -> Result<Self, Self::Rejection> {
|
||||||
|
let tenant = state
|
||||||
|
.tenants
|
||||||
|
.get_by_slug(&state.config.default_tenant_slug)?;
|
||||||
|
Ok(Self { tenant })
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use anyhow::Result;
|
||||||
|
|
||||||
|
use crate::{config::AppConfig, state::AppState, utils::tracing::init_tracing};
|
||||||
|
|
||||||
|
/// Initialize tracing, load configuration, and build the shared `AppState`.
|
||||||
|
/// Optionally override the connection pool size for lightweight components.
|
||||||
|
pub async fn init_component(name: &str, pool_override: Option<u32>) -> Result<Arc<AppState>> {
|
||||||
|
init_tracing("info");
|
||||||
|
let config = AppConfig::load_and_log(name)?;
|
||||||
|
let state = AppState::initialize(config, pool_override).await?;
|
||||||
|
Ok(Arc::new(state))
|
||||||
|
}
|
||||||
@@ -0,0 +1,59 @@
|
|||||||
|
use diesel::{pg::PgConnection, result::Error as DieselError};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
error::{AppError, AppResult},
|
||||||
|
state::AppState,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub trait EnsureEntity<T> {
|
||||||
|
fn one(self) -> AppResult<T>;
|
||||||
|
fn maybe(self) -> AppResult<Option<T>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> EnsureEntity<T> for Result<T, DieselError> {
|
||||||
|
fn one(self) -> AppResult<T> {
|
||||||
|
self.map_err(AppError::from)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn maybe(self) -> AppResult<Option<T>> {
|
||||||
|
match self {
|
||||||
|
Ok(value) => Ok(Some(value)),
|
||||||
|
Err(DieselError::NotFound) => Ok(None),
|
||||||
|
Err(err) => Err(AppError::from(err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AppState {
|
||||||
|
pub fn with_tenant_conn<F, T>(&self, tenant_id: Uuid, f: F) -> AppResult<T>
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut PgConnection) -> AppResult<T>,
|
||||||
|
{
|
||||||
|
let mut conn = self.db_for_tenant(tenant_id)?;
|
||||||
|
f(&mut conn)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn validate_bulk_ids(ids: &mut Vec<Uuid>, label: &str) -> AppResult<()> {
|
||||||
|
if ids.is_empty() {
|
||||||
|
return Err(AppError::bad_request(format!("{label} must not be empty")));
|
||||||
|
}
|
||||||
|
ids.sort_unstable();
|
||||||
|
ids.dedup();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait IntoJsonResponse<T> {
|
||||||
|
fn into_json(self) -> AppResult<axum::Json<T>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> IntoJsonResponse<T> for T {
|
||||||
|
fn into_json(self) -> AppResult<axum::Json<T>> {
|
||||||
|
Ok(axum::Json(self))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn no_content() -> AppResult<axum::http::StatusCode> {
|
||||||
|
Ok(axum::http::StatusCode::NO_CONTENT)
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
|
||||||
|
|
||||||
|
/// Build an inline `Content-Disposition` header value for a given filename.
|
||||||
|
pub fn inline_content_disposition(filename: &str) -> Option<String> {
|
||||||
|
if filename.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
let sanitized: String = filename
|
||||||
|
.chars()
|
||||||
|
.map(|ch| match ch {
|
||||||
|
'"' | '\\' => '_',
|
||||||
|
_ => ch,
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let encoded = utf8_percent_encode(&sanitized, NON_ALPHANUMERIC);
|
||||||
|
|
||||||
|
Some(format!(
|
||||||
|
"inline; filename=\"{}\"; filename*=UTF-8''{}",
|
||||||
|
sanitized, encoded
|
||||||
|
))
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
pub enum NullableValue {
|
||||||
|
Omitted,
|
||||||
|
Null,
|
||||||
|
String(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn classify_nullable(optional_value: Option<&Value>) -> Result<NullableValue, String> {
|
||||||
|
match optional_value {
|
||||||
|
None => Ok(NullableValue::Omitted),
|
||||||
|
Some(Value::Null) => Ok(NullableValue::Null),
|
||||||
|
Some(Value::String(s)) => Ok(NullableValue::String(s.to_owned())),
|
||||||
|
Some(other) => Err(format!("expected string or null, got {other}")),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
pub mod bootstrap;
|
||||||
|
pub mod db;
|
||||||
|
pub mod http;
|
||||||
|
pub mod json;
|
||||||
|
pub mod storage_paths;
|
||||||
|
pub mod time;
|
||||||
|
pub mod tracing;
|
||||||
|
pub mod validation;
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
//! Document storage path helpers.
|
||||||
|
//!
|
||||||
|
//! NOTE: The path layout produced here is part of the durable storage contract.
|
||||||
|
//! External systems (presigned URLs, lifecycle jobs, migrations) expect the
|
||||||
|
//! `documents/{document_id}/...` structure to remain stable. Coordinate before
|
||||||
|
//! changing any of these helpers to avoid breaking compatibility with existing
|
||||||
|
//! objects.
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
const DOCUMENTS_PREFIX: &str = "documents";
|
||||||
|
|
||||||
|
/// Returns the root prefix for all objects belonging to a document.
|
||||||
|
pub fn document_prefix(document_id: Uuid) -> String {
|
||||||
|
format!("{DOCUMENTS_PREFIX}/{document_id}")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the prefix for a specific document version (without the object id).
|
||||||
|
pub fn document_version_prefix(document_id: Uuid, version_number: i32) -> String {
|
||||||
|
format!("{}/v{}", document_prefix(document_id), version_number)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the storage key for a stored document version blob.
|
||||||
|
pub fn document_version_object_key(
|
||||||
|
document_id: Uuid,
|
||||||
|
version_number: i32,
|
||||||
|
version_id: Uuid,
|
||||||
|
) -> String {
|
||||||
|
format!(
|
||||||
|
"{}/{}",
|
||||||
|
document_version_prefix(document_id, version_number),
|
||||||
|
version_id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn document_asset_prefix(document_id: Uuid, version_number: i32) -> String {
|
||||||
|
format!(
|
||||||
|
"{}/assets",
|
||||||
|
document_version_prefix(document_id, version_number)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn document_asset_type_prefix(document_id: Uuid, version_number: i32, asset_type: &str) -> String {
|
||||||
|
format!(
|
||||||
|
"{}/{}",
|
||||||
|
document_asset_prefix(document_id, version_number),
|
||||||
|
asset_type
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the storage prefix under which the asset objects for a type/id pair live.
|
||||||
|
pub fn document_asset_object_prefix(
|
||||||
|
document_id: Uuid,
|
||||||
|
version_number: i32,
|
||||||
|
asset_type: &str,
|
||||||
|
asset_id: Uuid,
|
||||||
|
) -> String {
|
||||||
|
format!(
|
||||||
|
"{}/{}",
|
||||||
|
document_asset_type_prefix(document_id, version_number, asset_type),
|
||||||
|
asset_id
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the full storage key for a specific asset object (ordinal).
|
||||||
|
pub fn document_asset_object_key(
|
||||||
|
document_id: Uuid,
|
||||||
|
version_number: i32,
|
||||||
|
asset_type: &str,
|
||||||
|
asset_id: Uuid,
|
||||||
|
ordinal: i32,
|
||||||
|
) -> String {
|
||||||
|
format!(
|
||||||
|
"{}/{}",
|
||||||
|
document_asset_object_prefix(document_id, version_number, asset_type, asset_id),
|
||||||
|
ordinal
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn generates_expected_paths() {
|
||||||
|
let document_id = Uuid::nil();
|
||||||
|
let version_id = Uuid::nil();
|
||||||
|
let asset_id = Uuid::nil();
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
document_prefix(document_id),
|
||||||
|
format!("documents/{document_id}")
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
document_version_prefix(document_id, 3),
|
||||||
|
format!("documents/{document_id}/v3")
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
document_version_object_key(document_id, 3, version_id),
|
||||||
|
format!("documents/{document_id}/v3/{version_id}")
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
document_asset_object_prefix(document_id, 3, "preview", asset_id),
|
||||||
|
format!("documents/{document_id}/v3/assets/preview/{asset_id}")
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
document_asset_object_key(document_id, 3, "preview", asset_id, 2),
|
||||||
|
format!("documents/{document_id}/v3/assets/preview/{asset_id}/2")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||||
|
|
||||||
|
/// Format a timestamp as RFC3339 using UTC.
|
||||||
|
pub fn to_iso(dt: NaiveDateTime) -> String {
|
||||||
|
DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc).to_rfc3339()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Format a timestamp for HTTP headers (RFC 7231 date).
|
||||||
|
pub fn to_http_date(dt: NaiveDateTime) -> String {
|
||||||
|
DateTime::<Utc>::from_naive_utc_and_offset(dt, Utc)
|
||||||
|
.format("%a, %d %b %Y %H:%M:%S GMT")
|
||||||
|
.to_string()
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
|
/// Initialize tracing with an optional default level.
|
||||||
|
///
|
||||||
|
/// Falls back to `default_level` when `RUST_LOG` is not provided.
|
||||||
|
pub fn init_tracing(default_level: &str) {
|
||||||
|
let filter =
|
||||||
|
EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new(default_level));
|
||||||
|
tracing_subscriber::fmt()
|
||||||
|
.with_env_filter(filter)
|
||||||
|
.with_target(false)
|
||||||
|
.compact()
|
||||||
|
.init();
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
use crate::error::{AppError, AppResult};
|
||||||
|
|
||||||
|
/// Ensure an entity exists, returning a bad request error when it does not.
|
||||||
|
pub fn ensure_exists(exists: bool, entity: &str) -> AppResult<()> {
|
||||||
|
if exists {
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(AppError::bad_request(format!("{entity} does not exist")))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -8,11 +8,13 @@ use tokio::task;
|
|||||||
use tracing::{error, warn};
|
use tracing::{error, warn};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use super::ocr::{document_is_pdf, OCR_TEXT_ASSET_TYPE};
|
||||||
use crate::{
|
use crate::{
|
||||||
jobs::{enqueue_job, JOB_ANALYZE_DOCUMENT, JOB_GENERATE_THUMBNAILS},
|
jobs::{enqueue_job, JOB_ANALYZE_DOCUMENT, JOB_GENERATE_OCR_TEXT, JOB_GENERATE_THUMBNAILS},
|
||||||
models::{Document, DocumentVersion},
|
models::{Document, DocumentAsset, DocumentVersion},
|
||||||
schema::{document_versions, documents},
|
schema::{document_assets, document_versions, documents},
|
||||||
state::AppState,
|
state::AppState,
|
||||||
|
storage::TenantStorage,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{JobExecution, JobHandler};
|
use super::{JobExecution, JobHandler};
|
||||||
@@ -39,7 +41,12 @@ impl JobHandler for AnalyzeDocumentJob {
|
|||||||
JOB_ANALYZE_DOCUMENT
|
JOB_ANALYZE_DOCUMENT
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle(&self, state: Arc<AppState>, job: crate::models::Job) -> JobExecution {
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
state: Arc<AppState>,
|
||||||
|
job: crate::models::Job,
|
||||||
|
_storage: TenantStorage,
|
||||||
|
) -> JobExecution {
|
||||||
let payload: AnalyzePayload = match serde_json::from_value(job.payload.clone()) {
|
let payload: AnalyzePayload = match serde_json::from_value(job.payload.clone()) {
|
||||||
Ok(payload) => payload,
|
Ok(payload) => payload,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -50,7 +57,9 @@ impl JobHandler for AnalyzeDocumentJob {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let state_clone = state.clone();
|
let state_clone = state.clone();
|
||||||
match task::spawn_blocking(move || analyze_document(state_clone, payload)).await {
|
let tenant_id = job.tenant_id;
|
||||||
|
match task::spawn_blocking(move || analyze_document(state_clone, tenant_id, payload)).await
|
||||||
|
{
|
||||||
Ok(Ok(execution)) => execution,
|
Ok(Ok(execution)) => execution,
|
||||||
Ok(Err(err)) => {
|
Ok(Err(err)) => {
|
||||||
warn!(job_id = %job.id, error = %err, "analyze job will retry");
|
warn!(job_id = %job.id, error = %err, "analyze job will retry");
|
||||||
@@ -70,8 +79,14 @@ impl JobHandler for AnalyzeDocumentJob {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn analyze_document(state: Arc<AppState>, payload: AnalyzePayload) -> Result<JobExecution, String> {
|
fn analyze_document(
|
||||||
let mut conn = state.db().map_err(|err| format!("{err:?}"))?;
|
state: Arc<AppState>,
|
||||||
|
tenant_id: Uuid,
|
||||||
|
payload: AnalyzePayload,
|
||||||
|
) -> Result<JobExecution, String> {
|
||||||
|
let mut conn = state
|
||||||
|
.db_for_tenant(tenant_id)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
let version: DocumentVersion = document_versions::table
|
let version: DocumentVersion = document_versions::table
|
||||||
.find(payload.document_version_id)
|
.find(payload.document_version_id)
|
||||||
@@ -87,7 +102,20 @@ fn analyze_document(state: Arc<AppState>, payload: AnalyzePayload) -> Result<Job
|
|||||||
.first(&mut conn)
|
.first(&mut conn)
|
||||||
.map_err(|err| format!("{err:?}"))?;
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let tenant_id = document.tenant_id;
|
||||||
|
|
||||||
let (supported, reason) = determine_thumbnail_support(&document);
|
let (supported, reason) = determine_thumbnail_support(&document);
|
||||||
|
let ocr_supported = document_is_pdf(&document);
|
||||||
|
|
||||||
|
let existing_ocr: Option<DocumentAsset> = document_assets::table
|
||||||
|
.filter(document_assets::document_version_id.eq(payload.document_version_id))
|
||||||
|
.filter(document_assets::asset_type.eq(OCR_TEXT_ASSET_TYPE))
|
||||||
|
.filter(document_assets::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let skip_ocr = existing_ocr.is_some() && !payload.force;
|
||||||
|
|
||||||
let mut summary_map = match version.operations_summary {
|
let mut summary_map = match version.operations_summary {
|
||||||
Value::Object(map) => map,
|
Value::Object(map) => map,
|
||||||
@@ -100,6 +128,16 @@ fn analyze_document(state: Arc<AppState>, payload: AnalyzePayload) -> Result<Job
|
|||||||
summary_map.remove("thumbnail_reason");
|
summary_map.remove("thumbnail_reason");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
summary_map.insert("ocr_supported".to_string(), Value::Bool(ocr_supported));
|
||||||
|
if ocr_supported {
|
||||||
|
summary_map.remove("ocr_reason");
|
||||||
|
} else {
|
||||||
|
summary_map.insert(
|
||||||
|
"ocr_reason".to_string(),
|
||||||
|
Value::String("document is not a PDF".into()),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
diesel::update(document_versions::table.find(version.id))
|
diesel::update(document_versions::table.find(version.id))
|
||||||
.set(document_versions::operations_summary.eq(Value::Object(summary_map)))
|
.set(document_versions::operations_summary.eq(Value::Object(summary_map)))
|
||||||
.execute(&mut conn)
|
.execute(&mut conn)
|
||||||
@@ -108,6 +146,7 @@ fn analyze_document(state: Arc<AppState>, payload: AnalyzePayload) -> Result<Job
|
|||||||
if supported {
|
if supported {
|
||||||
let enqueue_result = enqueue_job(
|
let enqueue_result = enqueue_job(
|
||||||
&mut conn,
|
&mut conn,
|
||||||
|
tenant_id,
|
||||||
JOB_GENERATE_THUMBNAILS,
|
JOB_GENERATE_THUMBNAILS,
|
||||||
json!({
|
json!({
|
||||||
"document_id": payload.document_id,
|
"document_id": payload.document_id,
|
||||||
@@ -122,6 +161,24 @@ fn analyze_document(state: Arc<AppState>, payload: AnalyzePayload) -> Result<Job
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ocr_supported && !skip_ocr {
|
||||||
|
let enqueue_result = enqueue_job(
|
||||||
|
&mut conn,
|
||||||
|
tenant_id,
|
||||||
|
JOB_GENERATE_OCR_TEXT,
|
||||||
|
json!({
|
||||||
|
"document_id": payload.document_id,
|
||||||
|
"document_version_id": payload.document_version_id,
|
||||||
|
"force": payload.force,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(err) = enqueue_result {
|
||||||
|
return Err(err.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(JobExecution::Success)
|
Ok(JobExecution::Success)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,238 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
use reqwest::Client;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::json;
|
||||||
|
use tokio::task;
|
||||||
|
use tracing::{error, warn};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
jobs::JOB_INDEX_DOCUMENT_TEXT,
|
||||||
|
models::{Document, DocumentVersion},
|
||||||
|
schema::{document_asset_objects, document_assets, document_versions, documents},
|
||||||
|
state::AppState,
|
||||||
|
storage::TenantStorage,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{ocr::OCR_TEXT_ASSET_TYPE, JobExecution, JobHandler};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct IndexPayload {
|
||||||
|
document_id: Uuid,
|
||||||
|
document_version_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IndexDocumentTextJob;
|
||||||
|
|
||||||
|
impl IndexDocumentTextJob {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl JobHandler for IndexDocumentTextJob {
|
||||||
|
fn job_type(&self) -> &'static str {
|
||||||
|
JOB_INDEX_DOCUMENT_TEXT
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
state: Arc<AppState>,
|
||||||
|
job: crate::models::Job,
|
||||||
|
storage: TenantStorage,
|
||||||
|
) -> JobExecution {
|
||||||
|
let payload: IndexPayload = match serde_json::from_value(job.payload.clone()) {
|
||||||
|
Ok(payload) => payload,
|
||||||
|
Err(err) => {
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: format!("invalid index payload: {err}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let quickwit_endpoint = match &state.config.quickwit_endpoint {
|
||||||
|
Some(endpoint) => endpoint.clone(),
|
||||||
|
None => {
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "quickwit endpoint missing".into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let tenant = match state.tenants.get_by_id(job.tenant_id) {
|
||||||
|
Ok(tenant) => tenant,
|
||||||
|
Err(err) => {
|
||||||
|
warn!(job_id = %job.id, error = ?err, "failed to load tenant for indexing");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: format!("failed to load tenant: {err:?}"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let quickwit_index = match tenant.quickwit_index.clone() {
|
||||||
|
Some(index) => index,
|
||||||
|
None => {
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "tenant quickwit index not configured".into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let client = Client::new();
|
||||||
|
|
||||||
|
let state_clone = state.clone();
|
||||||
|
let context = match task::spawn_blocking(move || load_context(state_clone, &payload)).await
|
||||||
|
{
|
||||||
|
Ok(Ok(ctx)) => ctx,
|
||||||
|
Ok(Err(err)) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "index job will retry");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Err(join_err) => {
|
||||||
|
error!(job_id = %job.id, error = %join_err, "index task panicked");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(60),
|
||||||
|
error: format!("worker panicked: {join_err}"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if context.text_s3_key.is_none() {
|
||||||
|
warn!(job_id = %job.id, "missing OCR text asset; failing indexing job");
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "missing OCR text asset".into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let s3_key = context.text_s3_key.unwrap();
|
||||||
|
let text = match storage.get_object(&s3_key).await {
|
||||||
|
Ok(bytes) => match String::from_utf8(bytes) {
|
||||||
|
Ok(text) => text,
|
||||||
|
Err(err) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "ocr text not valid UTF-8");
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "ocr text not valid UTF-8".into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "failed to download ocr text");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err.to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if text.trim().is_empty() {
|
||||||
|
warn!(job_id = %job.id, "ocr text empty; skipping");
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "ocr text empty".into(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let client = client;
|
||||||
|
let url = format!(
|
||||||
|
"{}/api/v1/{}/ingest?commit=auto",
|
||||||
|
quickwit_endpoint, quickwit_index
|
||||||
|
);
|
||||||
|
let payload = json!({
|
||||||
|
"document_id": context.document.id,
|
||||||
|
"version_id": context.version.id,
|
||||||
|
"tenant_id": job.tenant_id,
|
||||||
|
"title": context.document.title.to_lowercase(),
|
||||||
|
"text": text.to_lowercase()
|
||||||
|
});
|
||||||
|
|
||||||
|
let body = serde_json::to_string(&payload).unwrap();
|
||||||
|
|
||||||
|
match client
|
||||||
|
.post(&url)
|
||||||
|
.header("content-type", "application/x-ndjson")
|
||||||
|
.body(format!("{}\n", body))
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(response) => {
|
||||||
|
if response.status().is_success() {
|
||||||
|
JobExecution::Success
|
||||||
|
} else {
|
||||||
|
let status = response.status();
|
||||||
|
let body = response.text().await.unwrap_or_default();
|
||||||
|
warn!(job_id = %job.id, %status, %body, "quickwit ingest failed");
|
||||||
|
JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: format!("quickwit ingest failed with status {status}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "quickwit request failed");
|
||||||
|
JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct IndexContext {
|
||||||
|
document: Document,
|
||||||
|
version: DocumentVersion,
|
||||||
|
text_s3_key: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_context(state: Arc<AppState>, payload: &IndexPayload) -> Result<IndexContext, String> {
|
||||||
|
let mut base_conn = state.db_unscoped().map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let version: DocumentVersion = document_versions::table
|
||||||
|
.find(payload.document_version_id)
|
||||||
|
.first(&mut base_conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
if version.document_id != payload.document_id {
|
||||||
|
return Err("document/version mismatch".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let document: Document = documents::table
|
||||||
|
.find(payload.document_id)
|
||||||
|
.first(&mut base_conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let tenant_id = document.tenant_id;
|
||||||
|
drop(base_conn);
|
||||||
|
|
||||||
|
let mut conn = state
|
||||||
|
.db_for_tenant(tenant_id)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let text_s3_key: Option<String> = document_asset_objects::table
|
||||||
|
.inner_join(
|
||||||
|
document_assets::table.on(document_asset_objects::asset_id.eq(document_assets::id)),
|
||||||
|
)
|
||||||
|
.filter(document_assets::document_version_id.eq(payload.document_version_id))
|
||||||
|
.filter(document_assets::asset_type.eq(OCR_TEXT_ASSET_TYPE))
|
||||||
|
.filter(document_assets::tenant_id.eq(tenant_id))
|
||||||
|
.filter(document_asset_objects::ordinal.eq(1))
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant_id))
|
||||||
|
.select(document_asset_objects::s3_key)
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
Ok(IndexContext {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
text_s3_key,
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -8,9 +8,12 @@ use crate::{
|
|||||||
jobs::{mark_job_failed, mark_job_succeeded, reserve_job, retry_job_after, JobQueueError},
|
jobs::{mark_job_failed, mark_job_succeeded, reserve_job, retry_job_after, JobQueueError},
|
||||||
models::Job,
|
models::Job,
|
||||||
state::AppState,
|
state::AppState,
|
||||||
|
storage::TenantStorage,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod analyze;
|
pub mod analyze;
|
||||||
|
pub mod index;
|
||||||
|
pub mod ocr;
|
||||||
pub mod thumbnails;
|
pub mod thumbnails;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -23,7 +26,7 @@ pub enum JobExecution {
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait JobHandler: Send + Sync {
|
pub trait JobHandler: Send + Sync {
|
||||||
fn job_type(&self) -> &'static str;
|
fn job_type(&self) -> &'static str;
|
||||||
async fn handle(&self, state: Arc<AppState>, job: Job) -> JobExecution;
|
async fn handle(&self, state: Arc<AppState>, job: Job, storage: TenantStorage) -> JobExecution;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Worker {
|
pub struct Worker {
|
||||||
@@ -69,7 +72,7 @@ impl Worker {
|
|||||||
return Ok(false);
|
return Ok(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut conn = match self.state.db() {
|
let mut conn = match self.state.db_unscoped() {
|
||||||
Ok(conn) => conn,
|
Ok(conn) => conn,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
error!(?err, "failed to obtain database connection in worker");
|
error!(?err, "failed to obtain database connection in worker");
|
||||||
@@ -82,10 +85,22 @@ impl Worker {
|
|||||||
|
|
||||||
if let Some(job) = job_opt {
|
if let Some(job) = job_opt {
|
||||||
if let Some(handler) = self.handlers.get(job.job_type.as_str()) {
|
if let Some(handler) = self.handlers.get(job.job_type.as_str()) {
|
||||||
let result = handler.handle(self.state.clone(), job.clone()).await;
|
let execution = match self.state.storage_for_tenant(job.tenant_id) {
|
||||||
match result {
|
Ok(storage) => {
|
||||||
|
handler
|
||||||
|
.handle(self.state.clone(), job.clone(), storage)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
error!(job_id = %job.id, error = ?err, "failed to load tenant storage for job");
|
||||||
|
JobExecution::Failed {
|
||||||
|
error: format!("tenant storage unavailable: {err:?}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match execution {
|
||||||
JobExecution::Success => {
|
JobExecution::Success => {
|
||||||
if let Ok(mut conn) = self.state.db() {
|
if let Ok(mut conn) = self.state.db_unscoped() {
|
||||||
mark_job_succeeded(&mut conn, job.id)?;
|
mark_job_succeeded(&mut conn, job.id)?;
|
||||||
info!(job_id = %job.id, job_type = %job.job_type, "job completed successfully");
|
info!(job_id = %job.id, job_type = %job.job_type, "job completed successfully");
|
||||||
} else {
|
} else {
|
||||||
@@ -94,7 +109,7 @@ impl Worker {
|
|||||||
}
|
}
|
||||||
JobExecution::Retry { delay, error } => {
|
JobExecution::Retry { delay, error } => {
|
||||||
warn!(job_id = %job.id, job_type = %job.job_type, %error, "job will retry");
|
warn!(job_id = %job.id, job_type = %job.job_type, %error, "job will retry");
|
||||||
if let Ok(mut conn) = self.state.db() {
|
if let Ok(mut conn) = self.state.db_unscoped() {
|
||||||
retry_job_after(&mut conn, job.id, delay, &error)?;
|
retry_job_after(&mut conn, job.id, delay, &error)?;
|
||||||
} else {
|
} else {
|
||||||
error!("failed to requeue job for retry due to pool error");
|
error!("failed to requeue job for retry due to pool error");
|
||||||
@@ -102,7 +117,7 @@ impl Worker {
|
|||||||
}
|
}
|
||||||
JobExecution::Failed { error } => {
|
JobExecution::Failed { error } => {
|
||||||
error!(job_id = %job.id, job_type = %job.job_type, %error, "job failed");
|
error!(job_id = %job.id, job_type = %job.job_type, %error, "job failed");
|
||||||
if let Ok(mut conn) = self.state.db() {
|
if let Ok(mut conn) = self.state.db_unscoped() {
|
||||||
mark_job_failed(&mut conn, job.id, &error)?;
|
mark_job_failed(&mut conn, job.id, &error)?;
|
||||||
} else {
|
} else {
|
||||||
error!("failed to mark job failed due to pool error");
|
error!("failed to mark job failed due to pool error");
|
||||||
@@ -111,7 +126,7 @@ impl Worker {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error!(job_type = %job.job_type, "no handler registered for job type");
|
error!(job_type = %job.job_type, "no handler registered for job type");
|
||||||
if let Ok(mut conn) = self.state.db() {
|
if let Ok(mut conn) = self.state.db_unscoped() {
|
||||||
mark_job_failed(&mut conn, job.id, "no handler registered")?;
|
mark_job_failed(&mut conn, job.id, "no handler registered")?;
|
||||||
} else {
|
} else {
|
||||||
error!("failed to mark job failed for missing handler due to pool error");
|
error!("failed to mark job failed for missing handler due to pool error");
|
||||||
@@ -128,5 +143,7 @@ pub fn default_handlers() -> Vec<Arc<dyn JobHandler>> {
|
|||||||
vec![
|
vec![
|
||||||
Arc::new(analyze::AnalyzeDocumentJob::new()),
|
Arc::new(analyze::AnalyzeDocumentJob::new()),
|
||||||
Arc::new(thumbnails::GenerateThumbnailsJob::new()),
|
Arc::new(thumbnails::GenerateThumbnailsJob::new()),
|
||||||
|
Arc::new(ocr::GenerateOcrTextJob::new()),
|
||||||
|
Arc::new(index::IndexDocumentTextJob::new()),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,527 @@
|
|||||||
|
use std::{
|
||||||
|
fmt, fs,
|
||||||
|
io::{ErrorKind, Write},
|
||||||
|
process::Command,
|
||||||
|
sync::Arc,
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use chrono::Utc;
|
||||||
|
use diesel::{pg::upsert::excluded, prelude::*};
|
||||||
|
use pdfium_render::prelude::*;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::json;
|
||||||
|
use tempfile::NamedTempFile;
|
||||||
|
use tokio::task;
|
||||||
|
use tracing::{error, info, warn};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
jobs::{enqueue_job, JOB_GENERATE_OCR_TEXT, JOB_INDEX_DOCUMENT_TEXT},
|
||||||
|
models::{
|
||||||
|
Document, DocumentAsset, DocumentAssetObject, DocumentVersion, NewDocumentAsset,
|
||||||
|
NewDocumentAssetObject,
|
||||||
|
},
|
||||||
|
schema::{document_asset_objects, document_assets, document_versions, documents},
|
||||||
|
state::AppState,
|
||||||
|
storage::TenantStorage,
|
||||||
|
utils::storage_paths::document_asset_object_prefix,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::{JobExecution, JobHandler};
|
||||||
|
|
||||||
|
pub const OCR_TEXT_ASSET_TYPE: &str = "ocr-text";
|
||||||
|
const MIN_TEXT_LENGTH: usize = 50;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Deserialize)]
|
||||||
|
struct OcrPayload {
|
||||||
|
document_id: Uuid,
|
||||||
|
document_version_id: Uuid,
|
||||||
|
#[serde(default)]
|
||||||
|
force: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GenerateOcrTextJob;
|
||||||
|
|
||||||
|
impl GenerateOcrTextJob {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl JobHandler for GenerateOcrTextJob {
|
||||||
|
fn job_type(&self) -> &'static str {
|
||||||
|
JOB_GENERATE_OCR_TEXT
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
state: Arc<AppState>,
|
||||||
|
job: crate::models::Job,
|
||||||
|
storage: TenantStorage,
|
||||||
|
) -> JobExecution {
|
||||||
|
let payload: OcrPayload = match serde_json::from_value(job.payload.clone()) {
|
||||||
|
Ok(payload) => payload,
|
||||||
|
Err(err) => {
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: format!("invalid OCR payload: {err}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let state_clone = state.clone();
|
||||||
|
let payload_clone = payload.clone();
|
||||||
|
let context =
|
||||||
|
match task::spawn_blocking(move || load_ocr_context(state_clone, &payload_clone)).await
|
||||||
|
{
|
||||||
|
Ok(Ok(ctx)) => ctx,
|
||||||
|
Ok(Err(err)) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "ocr job will retry");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Err(join_err) => {
|
||||||
|
error!(job_id = %job.id, error = %join_err, "ocr task panicked");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(60),
|
||||||
|
error: format!("worker panicked: {join_err}"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if context.skip {
|
||||||
|
info!(job_id = %job.id, "ocr already present; skipping");
|
||||||
|
return JobExecution::Success;
|
||||||
|
}
|
||||||
|
|
||||||
|
let bytes = match storage.get_object(&context.version.s3_key).await {
|
||||||
|
Ok(bytes) => bytes,
|
||||||
|
Err(err) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "failed to fetch document for ocr");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err.to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let doc_meta = PdfDocumentMeta {
|
||||||
|
content_type: context.document.content_type.clone(),
|
||||||
|
original_name: context.document.original_name.clone(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let generation =
|
||||||
|
match task::spawn_blocking(move || generate_ocr_text(&doc_meta, &bytes)).await {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(join_err) => {
|
||||||
|
error!(job_id = %job.id, error = %join_err, "ocr text task panicked");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(60),
|
||||||
|
error: format!("worker panicked: {join_err}"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let Some(generation) = generation else {
|
||||||
|
warn!(job_id = %job.id, "no text extracted from document; failing job");
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "no text extracted and OCR unavailable".into(),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
if context.existing_asset.is_some() {
|
||||||
|
for object in &context.existing_objects {
|
||||||
|
if let Err(err) = storage.delete_object(&object.s3_key).await {
|
||||||
|
warn!(job_id = %job.id, error = %err, s3_key = %object.s3_key, "failed to delete existing ocr asset object");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let asset_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
let s3_key = document_asset_object_prefix(
|
||||||
|
context.document.id,
|
||||||
|
context.version.version_number,
|
||||||
|
OCR_TEXT_ASSET_TYPE,
|
||||||
|
asset_id,
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(err) = storage
|
||||||
|
.put_object(
|
||||||
|
&s3_key,
|
||||||
|
generation.text.into_bytes(),
|
||||||
|
Some("text/plain".into()),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
warn!(job_id = %job.id, error = %err, "failed to upload ocr text");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err.to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let state_clone = state.clone();
|
||||||
|
match task::spawn_blocking(move || {
|
||||||
|
persist_ocr_metadata(state_clone, &context, asset_id, &s3_key, generation.source)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(Ok(())) => {
|
||||||
|
if let Err(err) = enqueue_index_job(&state, &payload) {
|
||||||
|
warn!(job_id = %job.id, error = %err, "failed to enqueue index job");
|
||||||
|
}
|
||||||
|
JobExecution::Success
|
||||||
|
}
|
||||||
|
Ok(Err(err)) => {
|
||||||
|
warn!(job_id = %job.id, error = %err, "failed to persist ocr metadata");
|
||||||
|
JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(join_err) => {
|
||||||
|
error!(job_id = %job.id, error = %join_err, "ocr metadata task panicked");
|
||||||
|
JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: format!("metadata update panic: {join_err}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct PdfDocumentMeta {
|
||||||
|
content_type: Option<String>,
|
||||||
|
original_name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct OcrContext {
|
||||||
|
document: Document,
|
||||||
|
version: DocumentVersion,
|
||||||
|
existing_asset: Option<DocumentAsset>,
|
||||||
|
existing_objects: Vec<DocumentAssetObject>,
|
||||||
|
skip: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct OcrGeneration {
|
||||||
|
text: String,
|
||||||
|
source: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load_ocr_context(state: Arc<AppState>, payload: &OcrPayload) -> Result<OcrContext, String> {
|
||||||
|
let mut base_conn = state.db_unscoped().map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let version: DocumentVersion = document_versions::table
|
||||||
|
.find(payload.document_version_id)
|
||||||
|
.first(&mut base_conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
if version.document_id != payload.document_id {
|
||||||
|
return Err("document/version mismatch".into());
|
||||||
|
}
|
||||||
|
|
||||||
|
let document: Document = documents::table
|
||||||
|
.find(payload.document_id)
|
||||||
|
.first(&mut base_conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let tenant_id = document.tenant_id;
|
||||||
|
drop(base_conn);
|
||||||
|
|
||||||
|
let mut conn = state
|
||||||
|
.db_for_tenant(tenant_id)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let existing_asset: Option<DocumentAsset> = document_assets::table
|
||||||
|
.filter(document_assets::document_version_id.eq(payload.document_version_id))
|
||||||
|
.filter(document_assets::asset_type.eq(OCR_TEXT_ASSET_TYPE))
|
||||||
|
.filter(document_assets::tenant_id.eq(tenant_id))
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let existing_objects: Vec<DocumentAssetObject> = if let Some(asset) = &existing_asset {
|
||||||
|
document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::asset_id.eq(asset.id))
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant_id))
|
||||||
|
.order(document_asset_objects::ordinal.asc())
|
||||||
|
.load(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
let is_pdf = document_is_pdf(&document);
|
||||||
|
if !is_pdf {
|
||||||
|
return Ok(OcrContext {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
existing_asset: existing_asset,
|
||||||
|
existing_objects,
|
||||||
|
skip: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let skip = existing_asset.is_some() && !payload.force;
|
||||||
|
|
||||||
|
Ok(OcrContext {
|
||||||
|
document,
|
||||||
|
version,
|
||||||
|
existing_asset,
|
||||||
|
existing_objects,
|
||||||
|
skip,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_ocr_text(meta: &PdfDocumentMeta, bytes: &[u8]) -> Option<OcrGeneration> {
|
||||||
|
if !document_meta_is_pdf(meta) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Ok(text) = extract_pdf_text(bytes) {
|
||||||
|
if text.trim().chars().count() >= MIN_TEXT_LENGTH {
|
||||||
|
return Some(OcrGeneration {
|
||||||
|
text,
|
||||||
|
source: "pdf-text",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
match run_ocr(bytes) {
|
||||||
|
Ok(Some(text)) => Some(OcrGeneration {
|
||||||
|
text,
|
||||||
|
source: "ocr",
|
||||||
|
}),
|
||||||
|
Ok(None) => None,
|
||||||
|
Err(OcrError::BinaryMissing) => {
|
||||||
|
warn!("ocrmypdf not installed; cannot perform OCR");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
warn!(error = ?err, "ocr command failed");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extract_pdf_text(bytes: &[u8]) -> Result<String, String> {
|
||||||
|
let pdfium = Pdfium::default();
|
||||||
|
let document = pdfium
|
||||||
|
.load_pdf_from_byte_slice(bytes, None)
|
||||||
|
.map_err(|err| format!("load pdf: {err}"))?;
|
||||||
|
|
||||||
|
let mut combined = String::new();
|
||||||
|
let pages = document.pages();
|
||||||
|
for page_index in 0..pages.len() {
|
||||||
|
let page = pages
|
||||||
|
.get(page_index)
|
||||||
|
.map_err(|err| format!("load page {page_index}: {err}"))?;
|
||||||
|
if let Ok(page_text) = page.text() {
|
||||||
|
for segment in page_text.segments().iter() {
|
||||||
|
combined.push_str(&segment.text());
|
||||||
|
combined.push('\n');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(combined)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum OcrError {
|
||||||
|
BinaryMissing,
|
||||||
|
Failed(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for OcrError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
OcrError::BinaryMissing => write!(f, "ocrmypdf binary not found"),
|
||||||
|
OcrError::Failed(msg) => write!(f, "ocr failed: {msg}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_ocr(bytes: &[u8]) -> Result<Option<String>, OcrError> {
|
||||||
|
let mut input = NamedTempFile::new().map_err(|err| OcrError::Failed(err.to_string()))?;
|
||||||
|
input
|
||||||
|
.write_all(bytes)
|
||||||
|
.map_err(|err| OcrError::Failed(err.to_string()))?;
|
||||||
|
input
|
||||||
|
.flush()
|
||||||
|
.map_err(|err| OcrError::Failed(err.to_string()))?;
|
||||||
|
|
||||||
|
let output_pdf = NamedTempFile::new().map_err(|err| OcrError::Failed(err.to_string()))?;
|
||||||
|
let sidecar = NamedTempFile::new().map_err(|err| OcrError::Failed(err.to_string()))?;
|
||||||
|
|
||||||
|
let status = Command::new("ocrmypdf")
|
||||||
|
.arg("--sidecar")
|
||||||
|
.arg(sidecar.path())
|
||||||
|
.arg("--skip-text")
|
||||||
|
.arg(input.path())
|
||||||
|
.arg(output_pdf.path())
|
||||||
|
.output();
|
||||||
|
|
||||||
|
match status {
|
||||||
|
Ok(output) => {
|
||||||
|
if !output.status.success() {
|
||||||
|
return Err(OcrError::Failed(format!(
|
||||||
|
"ocrmypdf failed: exit={} stderr={}",
|
||||||
|
output.status,
|
||||||
|
String::from_utf8_lossy(&output.stderr)
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
|
||||||
|
let text = fs::read_to_string(sidecar.path())
|
||||||
|
.map_err(|err| OcrError::Failed(err.to_string()))?;
|
||||||
|
if text.trim().chars().count() >= MIN_TEXT_LENGTH {
|
||||||
|
Ok(Some(text))
|
||||||
|
} else {
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
if err.kind() == ErrorKind::NotFound {
|
||||||
|
Err(OcrError::BinaryMissing)
|
||||||
|
} else {
|
||||||
|
Err(OcrError::Failed(err.to_string()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn persist_ocr_metadata(
|
||||||
|
state: Arc<AppState>,
|
||||||
|
context: &OcrContext,
|
||||||
|
asset_id: Uuid,
|
||||||
|
s3_key: &str,
|
||||||
|
source: &'static str,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let tenant_id = context.document.tenant_id;
|
||||||
|
let mut conn = state
|
||||||
|
.db_for_tenant(tenant_id)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
if let Some(existing_asset) = &context.existing_asset {
|
||||||
|
diesel::delete(document_assets::table.filter(document_assets::id.eq(existing_asset.id)))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let new_asset = NewDocumentAsset {
|
||||||
|
id: asset_id,
|
||||||
|
document_version_id: context.version.id,
|
||||||
|
asset_type: OCR_TEXT_ASSET_TYPE.to_string(),
|
||||||
|
mime_type: "text/plain".to_string(),
|
||||||
|
metadata: json!({
|
||||||
|
"generated_at": Utc::now().to_rfc3339(),
|
||||||
|
"source": source,
|
||||||
|
}),
|
||||||
|
cardinality: Some(1),
|
||||||
|
tenant_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(document_assets::table)
|
||||||
|
.values(&new_asset)
|
||||||
|
.on_conflict((
|
||||||
|
document_assets::document_version_id,
|
||||||
|
document_assets::asset_type,
|
||||||
|
))
|
||||||
|
.do_update()
|
||||||
|
.set((
|
||||||
|
document_assets::mime_type.eq(excluded(document_assets::mime_type)),
|
||||||
|
document_assets::metadata.eq(excluded(document_assets::metadata)),
|
||||||
|
document_assets::cardinality.eq(excluded(document_assets::cardinality)),
|
||||||
|
))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let existing_object_id: Option<Uuid> = document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::asset_id.eq(asset_id))
|
||||||
|
.filter(document_asset_objects::ordinal.eq(1))
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant_id))
|
||||||
|
.select(document_asset_objects::id)
|
||||||
|
.first(&mut conn)
|
||||||
|
.optional()
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let object_id = existing_object_id.unwrap_or_else(Uuid::new_v4);
|
||||||
|
|
||||||
|
let new_object = NewDocumentAssetObject {
|
||||||
|
id: object_id,
|
||||||
|
asset_id,
|
||||||
|
ordinal: 1,
|
||||||
|
s3_key: s3_key.to_string(),
|
||||||
|
metadata: json!({}),
|
||||||
|
tenant_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(document_asset_objects::table)
|
||||||
|
.values(&new_object)
|
||||||
|
.on_conflict((
|
||||||
|
document_asset_objects::asset_id,
|
||||||
|
document_asset_objects::ordinal,
|
||||||
|
))
|
||||||
|
.do_update()
|
||||||
|
.set((
|
||||||
|
document_asset_objects::s3_key.eq(excluded(document_asset_objects::s3_key)),
|
||||||
|
document_asset_objects::metadata.eq(excluded(document_asset_objects::metadata)),
|
||||||
|
))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn enqueue_index_job(state: &AppState, payload: &OcrPayload) -> Result<(), String> {
|
||||||
|
let mut base_conn = state.db_unscoped().map_err(|err| format!("{err:?}"))?;
|
||||||
|
let tenant_id: Uuid = documents::table
|
||||||
|
.find(payload.document_id)
|
||||||
|
.select(documents::tenant_id)
|
||||||
|
.first(&mut base_conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
drop(base_conn);
|
||||||
|
|
||||||
|
let mut conn = state
|
||||||
|
.db_for_tenant(tenant_id)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
enqueue_job(
|
||||||
|
&mut conn,
|
||||||
|
tenant_id,
|
||||||
|
JOB_INDEX_DOCUMENT_TEXT,
|
||||||
|
json!({
|
||||||
|
"document_id": payload.document_id,
|
||||||
|
"document_version_id": payload.document_version_id,
|
||||||
|
}),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.map(|_| ())
|
||||||
|
.map_err(|err| err.to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn document_is_pdf(document: &Document) -> bool {
|
||||||
|
document_meta_is_pdf(&PdfDocumentMeta {
|
||||||
|
content_type: document.content_type.clone(),
|
||||||
|
original_name: document.original_name.clone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn document_meta_is_pdf(meta: &PdfDocumentMeta) -> bool {
|
||||||
|
if let Some(content_type) = &meta.content_type {
|
||||||
|
if content_type.eq_ignore_ascii_case("application/pdf") {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
meta.original_name
|
||||||
|
.rsplit('.')
|
||||||
|
.next()
|
||||||
|
.map(|ext| ext.eq_ignore_ascii_case("pdf"))
|
||||||
|
.unwrap_or(false)
|
||||||
|
}
|
||||||
@@ -1,30 +1,36 @@
|
|||||||
use std::{io::Cursor, panic, sync::Arc, time::Duration};
|
use std::{convert::TryInto, io::Cursor, panic, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use diesel::{pg::upsert::excluded, prelude::*};
|
use diesel::{pg::upsert::excluded, prelude::*};
|
||||||
use image::{
|
use image::{GenericImageView, ImageFormat, ImageReader};
|
||||||
codecs::png::PngEncoder, ColorType, GenericImageView, ImageEncoder, ImageFormat, ImageReader,
|
|
||||||
};
|
|
||||||
use pdfium_render::prelude::*;
|
use pdfium_render::prelude::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde_json::json;
|
use serde_json::{json, Map, Value};
|
||||||
use tokio::task;
|
use tokio::task;
|
||||||
use tracing::{error, info, warn};
|
use tracing::{error, info, warn};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
jobs::JOB_GENERATE_THUMBNAILS,
|
jobs::JOB_GENERATE_THUMBNAILS,
|
||||||
models::{Document, DocumentAsset, DocumentVersion, NewDocumentAsset},
|
models::{
|
||||||
schema::{document_assets, document_versions, documents},
|
Document, DocumentAsset, DocumentAssetObject, DocumentVersion, NewDocumentAsset,
|
||||||
|
NewDocumentAssetObject,
|
||||||
|
},
|
||||||
|
schema::{document_asset_objects, document_assets, document_versions, documents},
|
||||||
state::AppState,
|
state::AppState,
|
||||||
|
storage::TenantStorage,
|
||||||
|
utils::storage_paths::document_asset_object_key,
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::{analyze::determine_thumbnail_support, JobExecution, JobHandler};
|
use super::{analyze::determine_thumbnail_support, JobExecution, JobHandler};
|
||||||
|
|
||||||
const THUMBNAIL_WIDTH: u32 = 512;
|
const THUMBNAIL_WIDTH: u32 = 512;
|
||||||
const THUMBNAIL_HEIGHT: u32 = 512;
|
const THUMBNAIL_HEIGHT: u32 = 512;
|
||||||
|
const PREVIEW_WIDTH: u32 = THUMBNAIL_WIDTH * 4;
|
||||||
|
const PREVIEW_HEIGHT: u32 = THUMBNAIL_HEIGHT * 4;
|
||||||
const THUMBNAIL_ASSET_TYPE: &str = "thumbnail";
|
const THUMBNAIL_ASSET_TYPE: &str = "thumbnail";
|
||||||
|
const PREVIEW_ASSET_TYPE: &str = "preview";
|
||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
struct ThumbnailPayload {
|
struct ThumbnailPayload {
|
||||||
@@ -48,7 +54,12 @@ impl JobHandler for GenerateThumbnailsJob {
|
|||||||
JOB_GENERATE_THUMBNAILS
|
JOB_GENERATE_THUMBNAILS
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle(&self, state: Arc<AppState>, job: crate::models::Job) -> JobExecution {
|
async fn handle(
|
||||||
|
&self,
|
||||||
|
state: Arc<AppState>,
|
||||||
|
job: crate::models::Job,
|
||||||
|
storage: TenantStorage,
|
||||||
|
) -> JobExecution {
|
||||||
let payload: ThumbnailPayload = match serde_json::from_value(job.payload.clone()) {
|
let payload: ThumbnailPayload = match serde_json::from_value(job.payload.clone()) {
|
||||||
Ok(p) => p,
|
Ok(p) => p,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
@@ -84,7 +95,7 @@ impl JobHandler for GenerateThumbnailsJob {
|
|||||||
return JobExecution::Success;
|
return JobExecution::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes = match state.storage.get_object(&initial.version.s3_key).await {
|
let bytes = match storage.get_object(&initial.version.s3_key).await {
|
||||||
Ok(bytes) => bytes,
|
Ok(bytes) => bytes,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!(job_id = %job.id, error = %err, "thumbnail fetch failed; will retry");
|
warn!(job_id = %job.id, error = %err, "thumbnail fetch failed; will retry");
|
||||||
@@ -95,32 +106,180 @@ impl JobHandler for GenerateThumbnailsJob {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let generation = match generate_thumbnail(&initial.document, &bytes) {
|
let generation = match generate_preview_and_thumbnail(&initial.document, &bytes) {
|
||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
return JobExecution::Failed { error: err };
|
return JobExecution::Failed { error: err };
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Err(err) = state
|
if let Some(page_count) = generation.page_count {
|
||||||
.storage
|
let state_clone = state.clone();
|
||||||
|
let document_id = initial.document.id;
|
||||||
|
let version_id = initial.version.id;
|
||||||
|
match task::spawn_blocking(move || {
|
||||||
|
persist_document_page_count(state_clone, document_id, version_id, page_count)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(Ok(())) => {}
|
||||||
|
Ok(Err(err)) => {
|
||||||
|
warn!(
|
||||||
|
job_id = %job.id,
|
||||||
|
document_id = %document_id,
|
||||||
|
version_id = %version_id,
|
||||||
|
error = %err,
|
||||||
|
"failed to update document page count metadata; retrying"
|
||||||
|
);
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Err(join_err) => {
|
||||||
|
error!(
|
||||||
|
job_id = %job.id,
|
||||||
|
document_id = %document_id,
|
||||||
|
version_id = %version_id,
|
||||||
|
error = %join_err,
|
||||||
|
"page count metadata task panicked"
|
||||||
|
);
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(60),
|
||||||
|
error: format!("metadata panic: {join_err}"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if initial.existing_preview.is_some() {
|
||||||
|
for object in &initial.existing_preview_objects {
|
||||||
|
if let Err(err) = storage.delete_object(&object.s3_key).await {
|
||||||
|
warn!(
|
||||||
|
job_id = %job.id,
|
||||||
|
error = %err,
|
||||||
|
s3_key = %object.s3_key,
|
||||||
|
"failed to delete existing preview object"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if initial.existing_thumbnail.is_some() {
|
||||||
|
for object in &initial.existing_thumbnail_objects {
|
||||||
|
if let Err(err) = storage.delete_object(&object.s3_key).await {
|
||||||
|
warn!(
|
||||||
|
job_id = %job.id,
|
||||||
|
error = %err,
|
||||||
|
s3_key = %object.s3_key,
|
||||||
|
"failed to delete existing thumbnail object"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let preview_asset_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
let thumbnail_asset_id = Uuid::new_v4();
|
||||||
|
|
||||||
|
let mut preview_objects: Vec<AssetObjectPersistence> =
|
||||||
|
Vec::with_capacity(generation.preview.objects.len());
|
||||||
|
for (index, image) in generation.preview.objects.iter().enumerate() {
|
||||||
|
if index + 1 > i32::MAX as usize {
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "too many preview objects".to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let ordinal = (index + 1) as i32;
|
||||||
|
let s3_key = document_asset_object_key(
|
||||||
|
initial.document.id,
|
||||||
|
initial.version.version_number,
|
||||||
|
PREVIEW_ASSET_TYPE,
|
||||||
|
preview_asset_id,
|
||||||
|
ordinal,
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(err) = storage
|
||||||
.put_object(
|
.put_object(
|
||||||
&generation.s3_key,
|
&s3_key,
|
||||||
generation.image_bytes.clone(),
|
image.image_bytes.clone(),
|
||||||
Some("image/png".into()),
|
Some("image/png".into()),
|
||||||
|
None,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
warn!(job_id = %job.id, error = %err, "failed to upload thumbnail; retrying");
|
warn!(job_id = %job.id, error = %err, ordinal, "failed to upload preview; retrying");
|
||||||
return JobExecution::Retry {
|
return JobExecution::Retry {
|
||||||
delay: Duration::from_secs(30),
|
delay: Duration::from_secs(30),
|
||||||
error: err.to_string(),
|
error: err.to_string(),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
preview_objects.push(AssetObjectPersistence {
|
||||||
|
ordinal,
|
||||||
|
s3_key,
|
||||||
|
width: image.width,
|
||||||
|
height: image.height,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut thumbnail_objects: Vec<AssetObjectPersistence> =
|
||||||
|
Vec::with_capacity(generation.thumbnail.objects.len());
|
||||||
|
for (index, image) in generation.thumbnail.objects.iter().enumerate() {
|
||||||
|
if index + 1 > i32::MAX as usize {
|
||||||
|
return JobExecution::Failed {
|
||||||
|
error: "too many thumbnail objects".to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
let ordinal = (index + 1) as i32;
|
||||||
|
let s3_key = document_asset_object_key(
|
||||||
|
initial.document.id,
|
||||||
|
initial.version.version_number,
|
||||||
|
THUMBNAIL_ASSET_TYPE,
|
||||||
|
thumbnail_asset_id,
|
||||||
|
ordinal,
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(err) = storage
|
||||||
|
.put_object(
|
||||||
|
&s3_key,
|
||||||
|
image.image_bytes.clone(),
|
||||||
|
Some("image/png".into()),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
warn!(job_id = %job.id, error = %err, ordinal, "failed to upload thumbnail; retrying");
|
||||||
|
return JobExecution::Retry {
|
||||||
|
delay: Duration::from_secs(30),
|
||||||
|
error: err.to_string(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
thumbnail_objects.push(AssetObjectPersistence {
|
||||||
|
ordinal,
|
||||||
|
s3_key,
|
||||||
|
width: image.width,
|
||||||
|
height: image.height,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let asset_persistences = vec![
|
||||||
|
AssetPersistence {
|
||||||
|
asset_type: PREVIEW_ASSET_TYPE,
|
||||||
|
asset_id: preview_asset_id,
|
||||||
|
objects: preview_objects,
|
||||||
|
},
|
||||||
|
AssetPersistence {
|
||||||
|
asset_type: THUMBNAIL_ASSET_TYPE,
|
||||||
|
asset_id: thumbnail_asset_id,
|
||||||
|
objects: thumbnail_objects,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
let state_clone = state.clone();
|
let state_clone = state.clone();
|
||||||
match task::spawn_blocking(move || {
|
match task::spawn_blocking(move || {
|
||||||
persist_thumbnail_metadata(state_clone, &initial, &generation)
|
persist_assets_metadata(state_clone, &initial, &asset_persistences)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
@@ -148,21 +307,47 @@ impl JobHandler for GenerateThumbnailsJob {
|
|||||||
struct ThumbnailContext {
|
struct ThumbnailContext {
|
||||||
document: Document,
|
document: Document,
|
||||||
version: DocumentVersion,
|
version: DocumentVersion,
|
||||||
|
existing_thumbnail: Option<DocumentAsset>,
|
||||||
|
existing_thumbnail_objects: Vec<DocumentAssetObject>,
|
||||||
|
existing_preview: Option<DocumentAsset>,
|
||||||
|
existing_preview_objects: Vec<DocumentAssetObject>,
|
||||||
skip: bool,
|
skip: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct GeneratedThumbnail {
|
struct GeneratedImage {
|
||||||
image_bytes: Vec<u8>,
|
image_bytes: Vec<u8>,
|
||||||
width: Option<i32>,
|
width: Option<i32>,
|
||||||
height: Option<i32>,
|
height: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct GeneratedAsset {
|
||||||
|
objects: Vec<GeneratedImage>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct GeneratedAssets {
|
||||||
|
thumbnail: GeneratedAsset,
|
||||||
|
preview: GeneratedAsset,
|
||||||
|
page_count: Option<u32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AssetObjectPersistence {
|
||||||
|
ordinal: i32,
|
||||||
s3_key: String,
|
s3_key: String,
|
||||||
|
width: Option<i32>,
|
||||||
|
height: Option<i32>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AssetPersistence {
|
||||||
|
asset_type: &'static str,
|
||||||
|
asset_id: Uuid,
|
||||||
|
objects: Vec<AssetObjectPersistence>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_thumbnail_context(
|
fn load_thumbnail_context(
|
||||||
state: Arc<AppState>,
|
state: Arc<AppState>,
|
||||||
payload: &ThumbnailPayload,
|
payload: &ThumbnailPayload,
|
||||||
) -> Result<ThumbnailContext, String> {
|
) -> Result<ThumbnailContext, String> {
|
||||||
let mut conn = state.db().map_err(|err| format!("{err:?}"))?;
|
let mut conn = state.db_unscoped().map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
let version: DocumentVersion = document_versions::table
|
let version: DocumentVersion = document_versions::table
|
||||||
.find(payload.document_version_id)
|
.find(payload.document_version_id)
|
||||||
@@ -178,76 +363,144 @@ fn load_thumbnail_context(
|
|||||||
.first(&mut conn)
|
.first(&mut conn)
|
||||||
.map_err(|err| format!("{err:?}"))?;
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
let existing: Option<DocumentAsset> = document_assets::table
|
let tenant_id = document.tenant_id;
|
||||||
|
|
||||||
|
let existing_assets: Vec<DocumentAsset> = document_assets::table
|
||||||
.filter(document_assets::document_version_id.eq(payload.document_version_id))
|
.filter(document_assets::document_version_id.eq(payload.document_version_id))
|
||||||
.filter(document_assets::asset_type.eq(THUMBNAIL_ASSET_TYPE))
|
.filter(document_assets::asset_type.eq_any(vec![
|
||||||
.first(&mut conn)
|
THUMBNAIL_ASSET_TYPE.to_string(),
|
||||||
.optional()
|
PREVIEW_ASSET_TYPE.to_string(),
|
||||||
|
]))
|
||||||
|
.filter(document_assets::tenant_id.eq(tenant_id))
|
||||||
|
.load(&mut conn)
|
||||||
.map_err(|err| format!("{err:?}"))?;
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let mut existing_thumbnail = None;
|
||||||
|
let mut existing_thumbnail_objects: Vec<DocumentAssetObject> = Vec::new();
|
||||||
|
let mut existing_preview = None;
|
||||||
|
let mut existing_preview_objects: Vec<DocumentAssetObject> = Vec::new();
|
||||||
|
for asset in existing_assets {
|
||||||
|
match asset.asset_type.as_str() {
|
||||||
|
THUMBNAIL_ASSET_TYPE => {
|
||||||
|
existing_thumbnail_objects = document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::asset_id.eq(asset.id))
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant_id))
|
||||||
|
.order(document_asset_objects::ordinal.asc())
|
||||||
|
.load(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
existing_thumbnail = Some(asset);
|
||||||
|
}
|
||||||
|
PREVIEW_ASSET_TYPE => {
|
||||||
|
existing_preview_objects = document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::asset_id.eq(asset.id))
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant_id))
|
||||||
|
.order(document_asset_objects::ordinal.asc())
|
||||||
|
.load(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
existing_preview = Some(asset);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let (supported, _) = determine_thumbnail_support(&document);
|
let (supported, _) = determine_thumbnail_support(&document);
|
||||||
if !supported {
|
if !supported {
|
||||||
return Err("thumbnail generation not supported for this document".into());
|
return Err("thumbnail generation not supported for this document".into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let skip = existing.is_some() && !payload.force;
|
let expected_cardinality = expected_asset_cardinality(&document, &version);
|
||||||
|
let preview_cardinality = existing_preview
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|asset| asset.cardinality)
|
||||||
|
.unwrap_or_else(|| existing_preview_objects.len() as i32);
|
||||||
|
let thumbnail_cardinality = existing_thumbnail
|
||||||
|
.as_ref()
|
||||||
|
.and_then(|asset| asset.cardinality)
|
||||||
|
.unwrap_or_else(|| existing_thumbnail_objects.len() as i32);
|
||||||
|
|
||||||
|
let needs_regeneration = preview_cardinality < expected_cardinality
|
||||||
|
|| thumbnail_cardinality < expected_cardinality
|
||||||
|
|| (existing_preview_objects.len() as i32) < expected_cardinality
|
||||||
|
|| (existing_thumbnail_objects.len() as i32) < expected_cardinality;
|
||||||
|
|
||||||
|
let skip = existing_thumbnail.is_some()
|
||||||
|
&& existing_preview.is_some()
|
||||||
|
&& !payload.force
|
||||||
|
&& !needs_regeneration;
|
||||||
|
|
||||||
Ok(ThumbnailContext {
|
Ok(ThumbnailContext {
|
||||||
document,
|
document,
|
||||||
version,
|
version,
|
||||||
|
existing_thumbnail,
|
||||||
|
existing_thumbnail_objects,
|
||||||
|
existing_preview,
|
||||||
|
existing_preview_objects,
|
||||||
skip,
|
skip,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_thumbnail(document: &Document, bytes: &[u8]) -> Result<GeneratedThumbnail, String> {
|
fn generate_preview_and_thumbnail(
|
||||||
let is_pdf = document
|
document: &Document,
|
||||||
.content_type
|
bytes: &[u8],
|
||||||
.as_deref()
|
) -> Result<GeneratedAssets, String> {
|
||||||
.map(|mime| mime == "application/pdf")
|
let is_pdf = document_is_pdf(document);
|
||||||
.unwrap_or_else(|| {
|
|
||||||
document
|
|
||||||
.original_name
|
|
||||||
.rsplit('.')
|
|
||||||
.next()
|
|
||||||
.map(|ext| ext.eq_ignore_ascii_case("pdf"))
|
|
||||||
.unwrap_or(false)
|
|
||||||
});
|
|
||||||
|
|
||||||
let (png_bytes, width, height) = if is_pdf {
|
if is_pdf {
|
||||||
generate_pdf_thumbnail(bytes)?
|
let pdf_assets = generate_pdf_assets(bytes)?;
|
||||||
} else {
|
Ok(GeneratedAssets {
|
||||||
generate_image_thumbnail(bytes)?
|
preview: pdf_assets.preview,
|
||||||
};
|
thumbnail: pdf_assets.thumbnail,
|
||||||
|
page_count: Some(pdf_assets.page_count),
|
||||||
let s3_key = format!("thumbnails/{}/{}.png", document.id, Uuid::new_v4());
|
|
||||||
|
|
||||||
Ok(GeneratedThumbnail {
|
|
||||||
image_bytes: png_bytes,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
s3_key,
|
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
let (preview, thumbnail) = generate_image_assets(bytes)?;
|
||||||
|
Ok(GeneratedAssets {
|
||||||
|
preview,
|
||||||
|
thumbnail,
|
||||||
|
page_count: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_image_thumbnail(bytes: &[u8]) -> Result<(Vec<u8>, Option<i32>, Option<i32>), String> {
|
fn generate_image_assets(bytes: &[u8]) -> Result<(GeneratedAsset, GeneratedAsset), String> {
|
||||||
let reader = ImageReader::new(Cursor::new(bytes))
|
let reader = ImageReader::new(Cursor::new(bytes))
|
||||||
.with_guessed_format()
|
.with_guessed_format()
|
||||||
.map_err(|err| err.to_string())?;
|
.map_err(|err| err.to_string())?;
|
||||||
let mut image = reader.decode().map_err(|err| err.to_string())?;
|
let image = reader.decode().map_err(|err| err.to_string())?;
|
||||||
if image.width() > THUMBNAIL_WIDTH || image.height() > THUMBNAIL_HEIGHT {
|
|
||||||
image = image.thumbnail(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (width, height) = image.dimensions();
|
let preview_image = if image.width() > PREVIEW_WIDTH || image.height() > PREVIEW_HEIGHT {
|
||||||
let mut cursor = Cursor::new(Vec::new());
|
image.thumbnail(PREVIEW_WIDTH, PREVIEW_HEIGHT)
|
||||||
image
|
} else {
|
||||||
.write_to(&mut cursor, ImageFormat::Png)
|
image.clone()
|
||||||
.map_err(|err| err.to_string())?;
|
};
|
||||||
let buffer = cursor.into_inner();
|
|
||||||
Ok((buffer, Some(width as i32), Some(height as i32)))
|
let thumbnail_image =
|
||||||
|
if preview_image.width() > THUMBNAIL_WIDTH || preview_image.height() > THUMBNAIL_HEIGHT {
|
||||||
|
preview_image.thumbnail(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT)
|
||||||
|
} else {
|
||||||
|
preview_image.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
let preview = encode_dynamic_image(preview_image)?;
|
||||||
|
let thumbnail = encode_dynamic_image(thumbnail_image)?;
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
GeneratedAsset {
|
||||||
|
objects: vec![preview],
|
||||||
|
},
|
||||||
|
GeneratedAsset {
|
||||||
|
objects: vec![thumbnail],
|
||||||
|
},
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_pdf_thumbnail(bytes: &[u8]) -> Result<(Vec<u8>, Option<i32>, Option<i32>), String> {
|
struct PdfGeneratedAssets {
|
||||||
|
preview: GeneratedAsset,
|
||||||
|
thumbnail: GeneratedAsset,
|
||||||
|
page_count: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generate_pdf_assets(bytes: &[u8]) -> Result<PdfGeneratedAssets, String> {
|
||||||
let pdfium = panic::catch_unwind(|| Pdfium::default())
|
let pdfium = panic::catch_unwind(|| Pdfium::default())
|
||||||
.map_err(|_| "failed to initialize PDFium".to_string())?;
|
.map_err(|_| "failed to initialize PDFium".to_string())?;
|
||||||
|
|
||||||
@@ -255,49 +508,118 @@ fn generate_pdf_thumbnail(bytes: &[u8]) -> Result<(Vec<u8>, Option<i32>, Option<
|
|||||||
.load_pdf_from_byte_slice(bytes, None)
|
.load_pdf_from_byte_slice(bytes, None)
|
||||||
.map_err(|err| format!("load pdf: {err}"))?;
|
.map_err(|err| format!("load pdf: {err}"))?;
|
||||||
|
|
||||||
let page = document
|
let pages = document.pages();
|
||||||
.pages()
|
let total_pages = pages.len() as usize;
|
||||||
.get(0)
|
|
||||||
.map_err(|err| format!("load first page: {err}"))?;
|
|
||||||
|
|
||||||
let render_config = PdfRenderConfig::new()
|
let render_config = PdfRenderConfig::new()
|
||||||
.set_target_width(THUMBNAIL_WIDTH as i32)
|
.set_target_width(PREVIEW_WIDTH as i32)
|
||||||
.set_maximum_height(THUMBNAIL_HEIGHT as i32)
|
.set_maximum_height(PREVIEW_HEIGHT as i32)
|
||||||
.render_form_data(true)
|
.render_form_data(true)
|
||||||
.rotate_if_landscape(PdfPageRenderRotation::None, true);
|
.rotate_if_landscape(PdfPageRenderRotation::None, true);
|
||||||
|
|
||||||
|
let mut preview_objects: Vec<GeneratedImage> = Vec::with_capacity(total_pages);
|
||||||
|
let mut thumbnail_objects: Vec<GeneratedImage> = Vec::with_capacity(total_pages);
|
||||||
|
|
||||||
|
for page_index in 0..total_pages {
|
||||||
|
let page = pages
|
||||||
|
.get(u16::try_from(page_index).map_err(|_| "page index overflow".to_string())?)
|
||||||
|
.map_err(|err| format!("load page {page_index}: {err}"))?;
|
||||||
|
|
||||||
let bitmap = page
|
let bitmap = page
|
||||||
.render_with_config(&render_config)
|
.render_with_config(&render_config)
|
||||||
.map_err(|err| format!("render pdf page: {err}"))?;
|
.map_err(|err| format!("render pdf page {page_index}: {err}"))?;
|
||||||
|
|
||||||
let image = bitmap.as_image().to_rgb8();
|
let preview_buffer = bitmap.as_image().to_rgb8();
|
||||||
let (width, height) = image.dimensions();
|
let preview_image = image::DynamicImage::ImageRgb8(preview_buffer);
|
||||||
let mut cursor = Cursor::new(Vec::new());
|
|
||||||
PngEncoder::new(&mut cursor)
|
|
||||||
.write_image(image.as_raw(), width, height, ColorType::Rgb8.into())
|
|
||||||
.map_err(|err| format!("encode pdf thumbnail: {err}"))?;
|
|
||||||
|
|
||||||
Ok((cursor.into_inner(), Some(width as i32), Some(height as i32)))
|
let thumbnail_image = if preview_image.width() > THUMBNAIL_WIDTH
|
||||||
|
|| preview_image.height() > THUMBNAIL_HEIGHT
|
||||||
|
{
|
||||||
|
preview_image.thumbnail(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT)
|
||||||
|
} else {
|
||||||
|
preview_image.clone()
|
||||||
|
};
|
||||||
|
|
||||||
|
preview_objects.push(encode_dynamic_image(preview_image)?);
|
||||||
|
thumbnail_objects.push(encode_dynamic_image(thumbnail_image)?);
|
||||||
|
}
|
||||||
|
|
||||||
|
let page_count: u32 = total_pages
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| "page count exceeds supported range".to_string())?;
|
||||||
|
|
||||||
|
Ok(PdfGeneratedAssets {
|
||||||
|
preview: GeneratedAsset {
|
||||||
|
objects: preview_objects,
|
||||||
|
},
|
||||||
|
thumbnail: GeneratedAsset {
|
||||||
|
objects: thumbnail_objects,
|
||||||
|
},
|
||||||
|
page_count,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn persist_thumbnail_metadata(
|
fn encode_dynamic_image(image: image::DynamicImage) -> Result<GeneratedImage, String> {
|
||||||
|
let (width, height) = image.dimensions();
|
||||||
|
let mut cursor = Cursor::new(Vec::new());
|
||||||
|
image
|
||||||
|
.write_to(&mut cursor, ImageFormat::Png)
|
||||||
|
.map_err(|err| err.to_string())?;
|
||||||
|
Ok(GeneratedImage {
|
||||||
|
image_bytes: cursor.into_inner(),
|
||||||
|
width: Some(width as i32),
|
||||||
|
height: Some(height as i32),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn persist_assets_metadata(
|
||||||
state: Arc<AppState>,
|
state: Arc<AppState>,
|
||||||
context: &ThumbnailContext,
|
context: &ThumbnailContext,
|
||||||
generated: &GeneratedThumbnail,
|
assets: &[AssetPersistence],
|
||||||
) -> Result<(), String> {
|
) -> Result<(), String> {
|
||||||
let mut conn = state.db().map_err(|err| format!("{err:?}"))?;
|
let tenant_id = context.document.tenant_id;
|
||||||
|
let mut conn = state
|
||||||
|
.db_for_tenant(tenant_id)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
if let Some(existing_preview) = &context.existing_preview {
|
||||||
|
diesel::delete(document_assets::table.filter(document_assets::id.eq(existing_preview.id)))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(existing_thumbnail) = &context.existing_thumbnail {
|
||||||
|
diesel::delete(
|
||||||
|
document_assets::table.filter(document_assets::id.eq(existing_thumbnail.id)),
|
||||||
|
)
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
}
|
||||||
|
|
||||||
|
for asset in assets {
|
||||||
|
if asset.objects.is_empty() {
|
||||||
|
return Err(format!(
|
||||||
|
"asset {} has no generated objects",
|
||||||
|
asset.asset_type
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
let object_count: i32 = asset
|
||||||
|
.objects
|
||||||
|
.len()
|
||||||
|
.try_into()
|
||||||
|
.map_err(|_| "asset contains too many objects".to_string())?;
|
||||||
|
|
||||||
let new_asset = NewDocumentAsset {
|
let new_asset = NewDocumentAsset {
|
||||||
id: Uuid::new_v4(),
|
id: asset.asset_id,
|
||||||
document_version_id: context.version.id,
|
document_version_id: context.version.id,
|
||||||
asset_type: THUMBNAIL_ASSET_TYPE.to_string(),
|
asset_type: asset.asset_type.to_string(),
|
||||||
s3_key: generated.s3_key.clone(),
|
|
||||||
mime_type: "image/png".to_string(),
|
mime_type: "image/png".to_string(),
|
||||||
width: generated.width,
|
|
||||||
height: generated.height,
|
|
||||||
metadata: json!({
|
metadata: json!({
|
||||||
"generated_at": Utc::now().to_rfc3339(),
|
"generated_at": Utc::now().to_rfc3339(),
|
||||||
}),
|
}),
|
||||||
|
cardinality: Some(object_count),
|
||||||
|
tenant_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
diesel::insert_into(document_assets::table)
|
diesel::insert_into(document_assets::table)
|
||||||
@@ -308,14 +630,127 @@ fn persist_thumbnail_metadata(
|
|||||||
))
|
))
|
||||||
.do_update()
|
.do_update()
|
||||||
.set((
|
.set((
|
||||||
document_assets::s3_key.eq(excluded(document_assets::s3_key)),
|
|
||||||
document_assets::mime_type.eq(excluded(document_assets::mime_type)),
|
document_assets::mime_type.eq(excluded(document_assets::mime_type)),
|
||||||
document_assets::width.eq(excluded(document_assets::width)),
|
|
||||||
document_assets::height.eq(excluded(document_assets::height)),
|
|
||||||
document_assets::metadata.eq(excluded(document_assets::metadata)),
|
document_assets::metadata.eq(excluded(document_assets::metadata)),
|
||||||
|
document_assets::cardinality.eq(excluded(document_assets::cardinality)),
|
||||||
))
|
))
|
||||||
.execute(&mut conn)
|
.execute(&mut conn)
|
||||||
.map_err(|err| format!("{err:?}"))?;
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
diesel::delete(
|
||||||
|
document_asset_objects::table
|
||||||
|
.filter(document_asset_objects::asset_id.eq(asset.asset_id))
|
||||||
|
.filter(document_asset_objects::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
for object in &asset.objects {
|
||||||
|
let mut metadata_map = Map::new();
|
||||||
|
if let Some(width) = object.width {
|
||||||
|
metadata_map.insert("width".to_string(), Value::from(width));
|
||||||
|
}
|
||||||
|
if let Some(height) = object.height {
|
||||||
|
metadata_map.insert("height".to_string(), Value::from(height));
|
||||||
|
}
|
||||||
|
|
||||||
|
let object_metadata = Value::Object(metadata_map);
|
||||||
|
|
||||||
|
let new_object = NewDocumentAssetObject {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
asset_id: asset.asset_id,
|
||||||
|
ordinal: object.ordinal,
|
||||||
|
s3_key: object.s3_key.clone(),
|
||||||
|
metadata: object_metadata,
|
||||||
|
tenant_id,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(document_asset_objects::table)
|
||||||
|
.values(&new_object)
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn persist_document_page_count(
|
||||||
|
state: Arc<AppState>,
|
||||||
|
document_id: Uuid,
|
||||||
|
document_version_id: Uuid,
|
||||||
|
page_count: u32,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let mut conn = state.db_unscoped().map_err(|err| format!("{err:?}"))?;
|
||||||
|
let tenant_id: Uuid = documents::table
|
||||||
|
.find(document_id)
|
||||||
|
.select(documents::tenant_id)
|
||||||
|
.first(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let existing_metadata: Value = document_versions::table
|
||||||
|
.filter(document_versions::id.eq(document_version_id))
|
||||||
|
.filter(document_versions::document_id.eq(document_id))
|
||||||
|
.filter(document_versions::tenant_id.eq(tenant_id))
|
||||||
|
.select(document_versions::metadata)
|
||||||
|
.first(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
|
let updated = match existing_metadata {
|
||||||
|
Value::Object(mut map) => {
|
||||||
|
map.insert("page_count".to_string(), Value::from(page_count));
|
||||||
|
Value::Object(map)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
let mut map = Map::new();
|
||||||
|
map.insert("page_count".to_string(), Value::from(page_count));
|
||||||
|
Value::Object(map)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::update(
|
||||||
|
document_versions::table
|
||||||
|
.filter(document_versions::id.eq(document_version_id))
|
||||||
|
.filter(document_versions::document_id.eq(document_id))
|
||||||
|
.filter(document_versions::tenant_id.eq(tenant_id)),
|
||||||
|
)
|
||||||
|
.set(document_versions::metadata.eq(updated))
|
||||||
|
.execute(&mut conn)
|
||||||
|
.map_err(|err| format!("{err:?}"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn document_is_pdf(document: &Document) -> bool {
|
||||||
|
document
|
||||||
|
.content_type
|
||||||
|
.as_deref()
|
||||||
|
.map(|mime| mime.eq_ignore_ascii_case("application/pdf"))
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
document
|
||||||
|
.original_name
|
||||||
|
.rsplit('.')
|
||||||
|
.next()
|
||||||
|
.map(|ext| ext.eq_ignore_ascii_case("pdf"))
|
||||||
|
.unwrap_or(false)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn expected_asset_cardinality(document: &Document, version: &DocumentVersion) -> i32 {
|
||||||
|
if let Value::Object(map) = &version.metadata {
|
||||||
|
if let Some(count) = map.get("page_count").and_then(|v| v.as_i64()) {
|
||||||
|
if count > 0 {
|
||||||
|
return count
|
||||||
|
.min(i64::from(i32::MAX))
|
||||||
|
.try_into()
|
||||||
|
.unwrap_or(i32::MAX);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if document_is_pdf(document) {
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,7 +8,6 @@ use serde::Deserialize;
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct AuthenticatedUser {
|
struct AuthenticatedUser {
|
||||||
username: String,
|
username: String,
|
||||||
role: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -27,7 +26,6 @@ async fn login_and_me_roundtrip() -> Result<()> {
|
|||||||
let user: AuthenticatedUser = serde_json::from_slice(&body)?;
|
let user: AuthenticatedUser = serde_json::from_slice(&body)?;
|
||||||
|
|
||||||
assert_eq!(user.username, "alice");
|
assert_eq!(user.username, "alice");
|
||||||
assert_eq!(user.role, "admin");
|
|
||||||
|
|
||||||
app.cleanup().await?;
|
app.cleanup().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+233
-22
@@ -8,21 +8,23 @@ use async_trait::async_trait;
|
|||||||
use axum::body::Body;
|
use axum::body::Body;
|
||||||
use axum::http::{Method, Request, StatusCode};
|
use axum::http::{Method, Request, StatusCode};
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
use backend::auth::jwt::JwtService;
|
||||||
|
use backend::config::AppConfig;
|
||||||
|
use backend::db::{self, PgPool};
|
||||||
|
use backend::models::{Job, NewUser, NewUserMembership, Tenant};
|
||||||
|
use backend::routes;
|
||||||
|
use backend::state::AppState;
|
||||||
|
use backend::storage::ObjectStorage;
|
||||||
use diesel::connection::SimpleConnection;
|
use diesel::connection::SimpleConnection;
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
|
use diesel::OptionalExtension;
|
||||||
use diesel::PgConnection;
|
use diesel::PgConnection;
|
||||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||||
use http_body_util::BodyExt;
|
use http_body_util::BodyExt;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use paperless_backend::auth::jwt::JwtService;
|
|
||||||
use paperless_backend::config::AppConfig;
|
|
||||||
use paperless_backend::db::{self, PgPool};
|
|
||||||
use paperless_backend::models::{Job, NewUser};
|
|
||||||
use paperless_backend::routes;
|
|
||||||
use paperless_backend::state::AppState;
|
|
||||||
use paperless_backend::storage::ObjectStorage;
|
|
||||||
use rand::rngs::OsRng;
|
use rand::rngs::OsRng;
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json;
|
||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use tower::util::ServiceExt;
|
use tower::util::ServiceExt;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -37,6 +39,7 @@ pub struct StoredObject {
|
|||||||
pub key: String,
|
pub key: String,
|
||||||
pub bytes: Vec<u8>,
|
pub bytes: Vec<u8>,
|
||||||
pub content_type: Option<String>,
|
pub content_type: Option<String>,
|
||||||
|
pub content_disposition: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
@@ -51,11 +54,13 @@ impl ObjectStorage for FakeStorage {
|
|||||||
key: &str,
|
key: &str,
|
||||||
bytes: Vec<u8>,
|
bytes: Vec<u8>,
|
||||||
content_type: Option<String>,
|
content_type: Option<String>,
|
||||||
|
content_disposition: Option<String>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let stored = StoredObject {
|
let stored = StoredObject {
|
||||||
key: key.to_string(),
|
key: key.to_string(),
|
||||||
bytes,
|
bytes,
|
||||||
content_type,
|
content_type,
|
||||||
|
content_disposition,
|
||||||
};
|
};
|
||||||
let mut guard = self.objects.lock().await;
|
let mut guard = self.objects.lock().await;
|
||||||
guard.insert(stored.key.clone(), stored);
|
guard.insert(stored.key.clone(), stored);
|
||||||
@@ -78,6 +83,12 @@ impl ObjectStorage for FakeStorage {
|
|||||||
.map(|obj| obj.bytes.clone())
|
.map(|obj| obj.bytes.clone())
|
||||||
.ok_or_else(|| anyhow!("object {key} missing"))
|
.ok_or_else(|| anyhow!("object {key} missing"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn delete_object(&self, key: &str) -> Result<()> {
|
||||||
|
let mut guard = self.objects.lock().await;
|
||||||
|
guard.remove(key);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FakeStorage {
|
impl FakeStorage {
|
||||||
@@ -107,20 +118,32 @@ impl TestApp {
|
|||||||
|
|
||||||
let config = AppConfig {
|
let config = AppConfig {
|
||||||
database_url: database_url.clone(),
|
database_url: database_url.clone(),
|
||||||
|
database_max_pool_size: db::DEFAULT_MAX_POOL_SIZE,
|
||||||
server_host: "127.0.0.1".to_string(),
|
server_host: "127.0.0.1".to_string(),
|
||||||
server_port: 0,
|
server_port: 0,
|
||||||
|
webdav_host: "127.0.0.1".to_string(),
|
||||||
|
webdav_port: 0,
|
||||||
jwt_secret: "test-secret".to_string(),
|
jwt_secret: "test-secret".to_string(),
|
||||||
jwt_issuer: "test-issuer".to_string(),
|
jwt_issuer: "test-issuer".to_string(),
|
||||||
jwt_audience: "test-audience".to_string(),
|
jwt_audience: "test-audience".to_string(),
|
||||||
jwt_expiry_minutes: 60,
|
jwt_expiry_minutes: 60,
|
||||||
|
download_token_audience: "test-download".to_string(),
|
||||||
|
download_token_expiry_minutes: 60,
|
||||||
|
refresh_token_expiry_days: 30,
|
||||||
|
refresh_cookie_secure: false,
|
||||||
|
refresh_cookie_domain: None,
|
||||||
|
cors_allowed_origin: None,
|
||||||
aws_endpoint_url: None,
|
aws_endpoint_url: None,
|
||||||
aws_access_key_id: None,
|
aws_access_key_id: None,
|
||||||
aws_secret_access_key: None,
|
aws_secret_access_key: None,
|
||||||
aws_region: "us-east-1".to_string(),
|
aws_region: "us-east-1".to_string(),
|
||||||
s3_bucket: "test-bucket".to_string(),
|
s3_bucket: "test-bucket".to_string(),
|
||||||
|
quickwit_endpoint: None,
|
||||||
|
quickwit_index: None,
|
||||||
|
default_tenant_slug: "admin".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let pool = db::init_pool(&config.database_url)?;
|
let pool = db::init_pool_with_size(&config.database_url, config.database_max_pool_size)?;
|
||||||
prepare_database(&pool).await?;
|
prepare_database(&pool).await?;
|
||||||
|
|
||||||
let storage = Arc::new(FakeStorage::default());
|
let storage = Arc::new(FakeStorage::default());
|
||||||
@@ -129,16 +152,20 @@ impl TestApp {
|
|||||||
let state = AppState::new(pool.clone(), config, storage_for_state, jwt);
|
let state = AppState::new(pool.clone(), config, storage_for_state, jwt);
|
||||||
let router = routes::create_router(state.clone());
|
let router = routes::create_router(state.clone());
|
||||||
|
|
||||||
Ok(Self {
|
let app = Self {
|
||||||
state,
|
state,
|
||||||
router,
|
router,
|
||||||
storage,
|
storage,
|
||||||
})
|
};
|
||||||
|
|
||||||
|
app.ensure_default_tenant().await?;
|
||||||
|
|
||||||
|
Ok(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn cleanup(&self) -> Result<()> {
|
pub async fn cleanup(&self) -> Result<()> {
|
||||||
let pool = self.state.pool.clone();
|
let pool = self.state.pool.clone();
|
||||||
tokio::task::spawn_blocking(move || -> Result<()> {
|
let _ = tokio::task::spawn_blocking(move || -> Result<()> {
|
||||||
let mut conn = pool
|
let mut conn = pool
|
||||||
.get()
|
.get()
|
||||||
.map_err(|err| anyhow!("failed to get cleanup connection: {err}"))?;
|
.map_err(|err| anyhow!("failed to get cleanup connection: {err}"))?;
|
||||||
@@ -146,7 +173,10 @@ impl TestApp {
|
|||||||
Ok(())
|
Ok(())
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
.context("cleanup task panicked")?
|
.context("cleanup task panicked")?;
|
||||||
|
|
||||||
|
self.ensure_default_tenant().await?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -154,27 +184,111 @@ impl TestApp {
|
|||||||
self.storage.clone()
|
self.storage.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub async fn storage_key_for(&self, key: &str) -> Result<String> {
|
||||||
|
let tenant = self
|
||||||
|
.state
|
||||||
|
.tenants
|
||||||
|
.get_by_slug(&self.state.config.default_tenant_slug)
|
||||||
|
.map_err(|err| anyhow!("default tenant not found: {:?}", err))?;
|
||||||
|
let root = tenant
|
||||||
|
.storage_root
|
||||||
|
.clone()
|
||||||
|
.ok_or_else(|| anyhow!("default tenant missing storage root"))?;
|
||||||
|
Ok(format!("{}{}", root, key))
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn insert_user(&self, username: &str, password: &str, role: &str) -> Result<Uuid> {
|
pub async fn insert_user(&self, username: &str, password: &str, role: &str) -> Result<Uuid> {
|
||||||
let username = username.to_string();
|
let username = username.to_string();
|
||||||
let password = password.to_string();
|
let password = password.to_string();
|
||||||
let role = role.to_string();
|
let role = role.to_string();
|
||||||
|
let tenant_id = self
|
||||||
|
.state
|
||||||
|
.tenants
|
||||||
|
.tenant_id_for_slug(&self.state.config.default_tenant_slug)
|
||||||
|
.map_err(|err| anyhow!("default tenant not found: {:?}", err))?;
|
||||||
self.with_conn(move |conn| {
|
self.with_conn(move |conn| {
|
||||||
let password_hash = hash_password(&password)?;
|
let password_hash = hash_password(&password)?;
|
||||||
let user = NewUser {
|
let user = NewUser {
|
||||||
id: Uuid::new_v4(),
|
id: Uuid::new_v4(),
|
||||||
username,
|
username,
|
||||||
password_hash,
|
password_hash,
|
||||||
role,
|
|
||||||
};
|
};
|
||||||
diesel::insert_into(paperless_backend::schema::users::table)
|
diesel::insert_into(backend::schema::users::table)
|
||||||
.values(&user)
|
.values(&user)
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.context("failed to insert user")?;
|
.context("failed to insert user")?;
|
||||||
|
|
||||||
|
let membership = NewUserMembership {
|
||||||
|
id: Uuid::new_v4(),
|
||||||
|
user_id: user.id,
|
||||||
|
tenant_id,
|
||||||
|
role,
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(backend::schema::user_memberships::table)
|
||||||
|
.values(&membership)
|
||||||
|
.execute(conn)
|
||||||
|
.context("failed to insert user membership")?;
|
||||||
Ok(user.id)
|
Ok(user.id)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn ensure_default_tenant(&self) -> Result<Uuid> {
|
||||||
|
let slug_value = self.state.config.default_tenant_slug.clone();
|
||||||
|
let quickwit_enabled = self.state.config.quickwit_endpoint.is_some();
|
||||||
|
self.with_conn(move |conn| {
|
||||||
|
use backend::schema::tenants::dsl as tenants_dsl;
|
||||||
|
|
||||||
|
let existing = tenants_dsl::tenants
|
||||||
|
.filter(tenants_dsl::slug.eq(&slug_value))
|
||||||
|
.first::<Tenant>(conn)
|
||||||
|
.optional()
|
||||||
|
.context("failed to load default tenant")?;
|
||||||
|
|
||||||
|
let tenant_id = if let Some(current) = existing {
|
||||||
|
let desired_root = current
|
||||||
|
.storage_root
|
||||||
|
.clone()
|
||||||
|
.filter(|root| root.ends_with('/'))
|
||||||
|
.unwrap_or_else(|| format!("test-tenants/{}/", current.id));
|
||||||
|
|
||||||
|
if current.storage_root.as_deref() != Some(desired_root.as_str()) {
|
||||||
|
diesel::update(tenants_dsl::tenants.filter(tenants_dsl::id.eq(current.id)))
|
||||||
|
.set(tenants_dsl::storage_root.eq(Some(desired_root)))
|
||||||
|
.execute(conn)
|
||||||
|
.context("failed to update default tenant storage root")?;
|
||||||
|
}
|
||||||
|
|
||||||
|
current.id
|
||||||
|
} else {
|
||||||
|
let new_id = Uuid::new_v4();
|
||||||
|
let root = format!("test-tenants/{}/", new_id);
|
||||||
|
let quickwit_value = if quickwit_enabled {
|
||||||
|
Some(format!("documents-{}", new_id))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
diesel::insert_into(tenants_dsl::tenants)
|
||||||
|
.values((
|
||||||
|
tenants_dsl::id.eq(new_id),
|
||||||
|
tenants_dsl::slug.eq(&slug_value),
|
||||||
|
tenants_dsl::storage_root.eq(Some(root)),
|
||||||
|
tenants_dsl::quickwit_index.eq(quickwit_value),
|
||||||
|
))
|
||||||
|
.execute(conn)
|
||||||
|
.context("failed to insert default tenant")?;
|
||||||
|
|
||||||
|
new_id
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(tenant_id)
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn login_token(&self, username: &str, password: &str) -> Result<String> {
|
pub async fn login_token(&self, username: &str, password: &str) -> Result<String> {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct LoginPayload<'a> {
|
struct LoginPayload<'a> {
|
||||||
@@ -197,18 +311,64 @@ impl TestApp {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let body = body_to_vec(response.into_body()).await?;
|
let body = body_to_vec(response.into_body()).await?;
|
||||||
#[derive(serde::Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct LoginResponse {
|
struct LoginResponse {
|
||||||
access_token: String,
|
access_token: String,
|
||||||
}
|
}
|
||||||
let parsed: LoginResponse = serde_json::from_slice(&body)?;
|
|
||||||
|
if let Ok(parsed) = serde_json::from_slice::<LoginResponse>(&body) {
|
||||||
|
return Ok(parsed.access_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct TenantSummary {
|
||||||
|
tenant_id: Uuid,
|
||||||
|
_slug: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct TenantSelectionResponse {
|
||||||
|
selection_token: String,
|
||||||
|
tenants: Vec<TenantSummary>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let selection: TenantSelectionResponse = serde_json::from_slice(&body)?;
|
||||||
|
ensure!(
|
||||||
|
!selection.tenants.is_empty(),
|
||||||
|
"login returned no tenant options",
|
||||||
|
);
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct SelectTenantPayload {
|
||||||
|
tenant_id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
let target_tenant = selection.tenants[0].tenant_id;
|
||||||
|
let select_response = self
|
||||||
|
.post_json(
|
||||||
|
"/api/auth/select-tenant",
|
||||||
|
&SelectTenantPayload {
|
||||||
|
tenant_id: target_tenant,
|
||||||
|
},
|
||||||
|
Some(&selection.selection_token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
ensure!(
|
||||||
|
select_response.status() == StatusCode::OK,
|
||||||
|
"tenant selection failed with status {}",
|
||||||
|
select_response.status()
|
||||||
|
);
|
||||||
|
|
||||||
|
let select_body = body_to_vec(select_response.into_body()).await?;
|
||||||
|
let parsed: LoginResponse = serde_json::from_slice(&select_body)?;
|
||||||
Ok(parsed.access_token)
|
Ok(parsed.access_token)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub async fn clear_jobs(&self) -> Result<()> {
|
pub async fn clear_jobs(&self) -> Result<()> {
|
||||||
self.with_conn(|conn| {
|
self.with_conn(|conn| {
|
||||||
use paperless_backend::schema::jobs::dsl::jobs as jobs_table;
|
use backend::schema::jobs::dsl::jobs as jobs_table;
|
||||||
diesel::delete(jobs_table)
|
diesel::delete(jobs_table)
|
||||||
.execute(conn)
|
.execute(conn)
|
||||||
.context("failed to clear jobs")?;
|
.context("failed to clear jobs")?;
|
||||||
@@ -221,9 +381,7 @@ impl TestApp {
|
|||||||
pub async fn jobs_by_type(&self, ty: &str) -> Result<Vec<Job>> {
|
pub async fn jobs_by_type(&self, ty: &str) -> Result<Vec<Job>> {
|
||||||
let ty = ty.to_string();
|
let ty = ty.to_string();
|
||||||
self.with_conn(move |conn| {
|
self.with_conn(move |conn| {
|
||||||
use paperless_backend::schema::jobs::dsl::{
|
use backend::schema::jobs::dsl::{job_type as job_type_col, jobs as jobs_table};
|
||||||
job_type as job_type_col, jobs as jobs_table,
|
|
||||||
};
|
|
||||||
let rows = jobs_table
|
let rows = jobs_table
|
||||||
.filter(job_type_col.eq(&ty))
|
.filter(job_type_col.eq(&ty))
|
||||||
.load::<Job>(conn)
|
.load::<Job>(conn)
|
||||||
@@ -320,6 +478,30 @@ impl TestApp {
|
|||||||
data: &[u8],
|
data: &[u8],
|
||||||
folder_id: Option<Uuid>,
|
folder_id: Option<Uuid>,
|
||||||
token: &str,
|
token: &str,
|
||||||
|
) -> Result<hyper::Response<Body>> {
|
||||||
|
self.upload_document_with_options(
|
||||||
|
path,
|
||||||
|
filename,
|
||||||
|
content_type,
|
||||||
|
data,
|
||||||
|
folder_id,
|
||||||
|
None,
|
||||||
|
None,
|
||||||
|
token,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn upload_document_with_options(
|
||||||
|
&self,
|
||||||
|
path: &str,
|
||||||
|
filename: &str,
|
||||||
|
content_type: &str,
|
||||||
|
data: &[u8],
|
||||||
|
folder_id: Option<Uuid>,
|
||||||
|
title: Option<&str>,
|
||||||
|
metadata_json: Option<&str>,
|
||||||
|
token: &str,
|
||||||
) -> Result<hyper::Response<Body>> {
|
) -> Result<hyper::Response<Body>> {
|
||||||
let boundary = format!("boundary-{}", Uuid::new_v4());
|
let boundary = format!("boundary-{}", Uuid::new_v4());
|
||||||
let mut body = Vec::new();
|
let mut body = Vec::new();
|
||||||
@@ -342,6 +524,20 @@ impl TestApp {
|
|||||||
body.extend(b"\r\n");
|
body.extend(b"\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Some(title_value) = title {
|
||||||
|
body.extend(format!("--{boundary}\r\n").as_bytes());
|
||||||
|
body.extend(b"Content-Disposition: form-data; name=\"title\"\r\n\r\n");
|
||||||
|
body.extend(title_value.as_bytes());
|
||||||
|
body.extend(b"\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(metadata_value) = metadata_json {
|
||||||
|
body.extend(format!("--{boundary}\r\n").as_bytes());
|
||||||
|
body.extend(b"Content-Disposition: form-data; name=\"metadata\"\r\n\r\n");
|
||||||
|
body.extend(metadata_value.as_bytes());
|
||||||
|
body.extend(b"\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
body.extend(format!("--{boundary}--\r\n").as_bytes());
|
body.extend(format!("--{boundary}--\r\n").as_bytes());
|
||||||
|
|
||||||
let builder = Request::builder()
|
let builder = Request::builder()
|
||||||
@@ -408,7 +604,22 @@ async fn prepare_database(pool: &PgPool) -> Result<()> {
|
|||||||
|
|
||||||
fn truncate_all(conn: &mut PgConnection) -> Result<()> {
|
fn truncate_all(conn: &mut PgConnection) -> Result<()> {
|
||||||
conn.batch_execute(
|
conn.batch_execute(
|
||||||
"TRUNCATE TABLE document_tags, document_versions, documents, folders, tags, users RESTART IDENTITY CASCADE;",
|
"TRUNCATE TABLE \
|
||||||
|
document_asset_objects, \
|
||||||
|
document_assets, \
|
||||||
|
document_correspondents, \
|
||||||
|
correspondents, \
|
||||||
|
document_tags, \
|
||||||
|
document_versions, \
|
||||||
|
documents, \
|
||||||
|
folders, \
|
||||||
|
jobs, \
|
||||||
|
refresh_tokens, \
|
||||||
|
tags, \
|
||||||
|
user_memberships, \
|
||||||
|
users, \
|
||||||
|
tenants \
|
||||||
|
RESTART IDENTITY CASCADE;",
|
||||||
)
|
)
|
||||||
.context("failed to truncate tables")?;
|
.context("failed to truncate tables")?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+351
-89
@@ -9,19 +9,21 @@ use uuid::Uuid;
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct DocumentDetail {
|
struct DocumentDetail {
|
||||||
document: DocumentInfo,
|
document: DocumentInfo,
|
||||||
current_version: DocumentVersion,
|
|
||||||
assets: Vec<DocumentAssetInfo>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct DocumentInfo {
|
struct DocumentInfo {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
title: String,
|
title: String,
|
||||||
|
filename: String,
|
||||||
original_name: String,
|
original_name: String,
|
||||||
current_version: i32,
|
|
||||||
deleted_at: Option<String>,
|
deleted_at: Option<String>,
|
||||||
issued_at: Option<String>,
|
issued_at: Option<String>,
|
||||||
tags: Vec<TagSummary>,
|
tags: Vec<TagSummary>,
|
||||||
|
#[serde(default)]
|
||||||
|
correspondents: Vec<DocumentCorrespondentInfo>,
|
||||||
|
#[serde(default)]
|
||||||
|
current_version: Option<DocumentVersion>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -29,6 +31,10 @@ struct DocumentVersion {
|
|||||||
id: Uuid,
|
id: Uuid,
|
||||||
s3_key: String,
|
s3_key: String,
|
||||||
size_bytes: i64,
|
size_bytes: i64,
|
||||||
|
version_number: i32,
|
||||||
|
download_path: String,
|
||||||
|
#[serde(default)]
|
||||||
|
assets: Vec<DocumentAssetInfo>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -41,7 +47,8 @@ struct DocumentAssetInfo {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct DocumentListItem {
|
struct DocumentListItem {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
current_version: i32,
|
#[serde(default)]
|
||||||
|
current_version: Option<DocumentVersion>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -71,6 +78,23 @@ struct TagSummary {
|
|||||||
label: String,
|
label: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct DocumentCorrespondentInfo {
|
||||||
|
name: String,
|
||||||
|
role: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct CorrespondentSummary {
|
||||||
|
id: Uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
struct BulkCorrespondentResult {
|
||||||
|
assigned: usize,
|
||||||
|
removed: usize,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct AnalyzeJobPayload {
|
struct AnalyzeJobPayload {
|
||||||
document_id: Uuid,
|
document_id: Uuid,
|
||||||
@@ -150,16 +174,23 @@ async fn upload_and_list_document() -> Result<()> {
|
|||||||
|
|
||||||
assert_eq!(detail.document.original_name, "doc.txt");
|
assert_eq!(detail.document.original_name, "doc.txt");
|
||||||
assert_eq!(detail.document.title, "doc");
|
assert_eq!(detail.document.title, "doc");
|
||||||
assert_eq!(detail.document.current_version, 1);
|
|
||||||
assert_eq!(detail.document.deleted_at, None);
|
assert_eq!(detail.document.deleted_at, None);
|
||||||
assert!(detail.document.issued_at.is_none());
|
assert!(detail.document.issued_at.is_none());
|
||||||
assert!(detail.document.tags.is_empty());
|
assert!(detail.document.tags.is_empty());
|
||||||
assert_eq!(detail.current_version.size_bytes, file_bytes.len() as i64);
|
let current_version = detail
|
||||||
assert!(detail.assets.is_empty());
|
.document
|
||||||
|
.current_version
|
||||||
|
.as_ref()
|
||||||
|
.expect("current version detail");
|
||||||
|
assert!(current_version.download_path.starts_with("/download/"));
|
||||||
|
assert_eq!(current_version.version_number, 1);
|
||||||
|
assert_eq!(current_version.size_bytes, file_bytes.len() as i64);
|
||||||
|
assert!(current_version.assets.is_empty());
|
||||||
|
|
||||||
|
let storage_key = app.storage_key_for(¤t_version.s3_key).await?;
|
||||||
let stored = app
|
let stored = app
|
||||||
.storage()
|
.storage()
|
||||||
.get(&detail.current_version.s3_key)
|
.get(&storage_key)
|
||||||
.await
|
.await
|
||||||
.expect("object stored");
|
.expect("object stored");
|
||||||
assert_eq!(stored.bytes, file_bytes);
|
assert_eq!(stored.bytes, file_bytes);
|
||||||
@@ -172,7 +203,18 @@ async fn upload_and_list_document() -> Result<()> {
|
|||||||
assert_eq!(list.len(), 1);
|
assert_eq!(list.len(), 1);
|
||||||
let item = list.pop().unwrap();
|
let item = list.pop().unwrap();
|
||||||
assert_eq!(item.id, detail.document.id);
|
assert_eq!(item.id, detail.document.id);
|
||||||
assert_eq!(item.current_version, 1);
|
assert_eq!(
|
||||||
|
item.current_version
|
||||||
|
.as_ref()
|
||||||
|
.map(|version| version.version_number),
|
||||||
|
Some(1)
|
||||||
|
);
|
||||||
|
assert!(item
|
||||||
|
.current_version
|
||||||
|
.as_ref()
|
||||||
|
.expect("list current version")
|
||||||
|
.download_path
|
||||||
|
.starts_with("/download/"));
|
||||||
|
|
||||||
let download = app
|
let download = app
|
||||||
.get(
|
.get(
|
||||||
@@ -183,9 +225,55 @@ async fn upload_and_list_document() -> Result<()> {
|
|||||||
assert_eq!(download.status(), StatusCode::OK);
|
assert_eq!(download.status(), StatusCode::OK);
|
||||||
let body = body_to_vec(download.into_body()).await?;
|
let body = body_to_vec(download.into_body()).await?;
|
||||||
let download_info: DocumentDownload = serde_json::from_slice(&body)?;
|
let download_info: DocumentDownload = serde_json::from_slice(&body)?;
|
||||||
assert!(download_info.url.contains(&detail.current_version.s3_key));
|
assert!(download_info.url.contains(¤t_version.s3_key));
|
||||||
assert_eq!(download_info.filename, "doc.txt");
|
assert_eq!(download_info.filename, "doc.txt");
|
||||||
|
|
||||||
|
let redirect = app.get(¤t_version.download_path, None).await?;
|
||||||
|
assert_eq!(redirect.status(), StatusCode::TEMPORARY_REDIRECT);
|
||||||
|
let location = redirect
|
||||||
|
.headers()
|
||||||
|
.get("location")
|
||||||
|
.expect("redirect location header");
|
||||||
|
let location = location.to_str().expect("location header utf8");
|
||||||
|
assert!(location.contains(¤t_version.s3_key));
|
||||||
|
|
||||||
|
app.cleanup().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn upload_document_with_custom_title_sets_filename() -> Result<()> {
|
||||||
|
let _lock = acquire_db_lock().await;
|
||||||
|
let app = TestApp::new().await?;
|
||||||
|
|
||||||
|
let password = "passw0rd";
|
||||||
|
app.insert_user("nora", password, "admin").await?;
|
||||||
|
let token = app.login_token("nora", password).await?;
|
||||||
|
|
||||||
|
let file_bytes = b"example contract body".to_vec();
|
||||||
|
let title = "Vendor Contract";
|
||||||
|
let original_filename = "scan.pdf";
|
||||||
|
|
||||||
|
let upload = app
|
||||||
|
.upload_document_with_options(
|
||||||
|
"/api/documents",
|
||||||
|
original_filename,
|
||||||
|
"application/pdf",
|
||||||
|
&file_bytes,
|
||||||
|
None,
|
||||||
|
Some(title),
|
||||||
|
None,
|
||||||
|
&token,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(upload.status(), StatusCode::CREATED);
|
||||||
|
let body = body_to_vec(upload.into_body()).await?;
|
||||||
|
let detail: DocumentDetail = serde_json::from_slice(&body)?;
|
||||||
|
|
||||||
|
assert_eq!(detail.document.title, title);
|
||||||
|
assert_eq!(detail.document.filename, format!("{title}.pdf"));
|
||||||
|
assert_eq!(detail.document.original_name, original_filename);
|
||||||
|
|
||||||
app.cleanup().await?;
|
app.cleanup().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -230,7 +318,13 @@ async fn duplicate_and_restore_document() -> Result<()> {
|
|||||||
|
|
||||||
assert_eq!(first_detail.document.id, second_detail.document.id);
|
assert_eq!(first_detail.document.id, second_detail.document.id);
|
||||||
assert_eq!(second_detail.document.deleted_at, None);
|
assert_eq!(second_detail.document.deleted_at, None);
|
||||||
assert!(second_detail.assets.is_empty());
|
assert!(second_detail
|
||||||
|
.document
|
||||||
|
.current_version
|
||||||
|
.as_ref()
|
||||||
|
.expect("second current version")
|
||||||
|
.assets
|
||||||
|
.is_empty());
|
||||||
assert_eq!(app.storage().object_count().await, 1);
|
assert_eq!(app.storage().object_count().await, 1);
|
||||||
|
|
||||||
let delete = app
|
let delete = app
|
||||||
@@ -263,82 +357,6 @@ async fn duplicate_and_restore_document() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn bulk_reanalyze_documents() -> Result<()> {
|
|
||||||
let _lock = acquire_db_lock().await;
|
|
||||||
let app = TestApp::new().await?;
|
|
||||||
|
|
||||||
let password = "bulkpass";
|
|
||||||
app.insert_user("alex", password, "admin").await?;
|
|
||||||
let token = app.login_token("alex", password).await?;
|
|
||||||
|
|
||||||
app.clear_jobs().await?;
|
|
||||||
|
|
||||||
let first_bytes = b"first doc";
|
|
||||||
let first = app
|
|
||||||
.upload_document(
|
|
||||||
"/api/documents",
|
|
||||||
"first.txt",
|
|
||||||
"text/plain",
|
|
||||||
first_bytes,
|
|
||||||
None,
|
|
||||||
&token,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
assert_eq!(first.status(), StatusCode::CREATED);
|
|
||||||
let first_body = body_to_vec(first.into_body()).await?;
|
|
||||||
let first_detail: DocumentDetail = serde_json::from_slice(&first_body)?;
|
|
||||||
|
|
||||||
let second_bytes = b"second doc";
|
|
||||||
let second = app
|
|
||||||
.upload_document(
|
|
||||||
"/api/documents",
|
|
||||||
"second.txt",
|
|
||||||
"text/plain",
|
|
||||||
second_bytes,
|
|
||||||
None,
|
|
||||||
&token,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
assert_eq!(second.status(), StatusCode::CREATED);
|
|
||||||
let second_body = body_to_vec(second.into_body()).await?;
|
|
||||||
let second_detail: DocumentDetail = serde_json::from_slice(&second_body)?;
|
|
||||||
|
|
||||||
app.clear_jobs().await?;
|
|
||||||
|
|
||||||
let response = app
|
|
||||||
.post_json(
|
|
||||||
"/api/documents/reanalyze",
|
|
||||||
&serde_json::json!({}),
|
|
||||||
Some(&token),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
assert_eq!(response.status(), StatusCode::ACCEPTED);
|
|
||||||
let body = body_to_vec(response.into_body()).await?;
|
|
||||||
let bulk: BulkReanalyze = serde_json::from_slice(&body)?;
|
|
||||||
assert_eq!(bulk.queued, 2);
|
|
||||||
|
|
||||||
let jobs = app.jobs_by_type("analyze-document").await?;
|
|
||||||
assert_eq!(jobs.len(), 2);
|
|
||||||
let mut payload_docs = Vec::new();
|
|
||||||
for job in jobs {
|
|
||||||
let payload: AnalyzeJobPayload = serde_json::from_value(job.payload)?;
|
|
||||||
assert!(payload.force);
|
|
||||||
payload_docs.push((payload.document_id, payload.document_version_id));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut expected = vec![
|
|
||||||
(first_detail.document.id, first_detail.current_version.id),
|
|
||||||
(second_detail.document.id, second_detail.current_version.id),
|
|
||||||
];
|
|
||||||
payload_docs.sort();
|
|
||||||
expected.sort();
|
|
||||||
assert_eq!(payload_docs, expected);
|
|
||||||
|
|
||||||
app.cleanup().await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn bulk_move_documents_to_folder() -> Result<()> {
|
async fn bulk_move_documents_to_folder() -> Result<()> {
|
||||||
let _lock = acquire_db_lock().await;
|
let _lock = acquire_db_lock().await;
|
||||||
@@ -564,6 +582,234 @@ async fn bulk_update_tags_for_selection() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn bulk_assign_correspondents_to_selection() -> Result<()> {
|
||||||
|
let _lock = acquire_db_lock().await;
|
||||||
|
let app = TestApp::new().await?;
|
||||||
|
|
||||||
|
let password = "bulkcorresp";
|
||||||
|
app.insert_user("corra", password, "admin").await?;
|
||||||
|
let token = app.login_token("corra", password).await?;
|
||||||
|
|
||||||
|
let first = app
|
||||||
|
.upload_document(
|
||||||
|
"/api/documents",
|
||||||
|
"letter-one.txt",
|
||||||
|
"text/plain",
|
||||||
|
b"letter one",
|
||||||
|
None,
|
||||||
|
&token,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(first.status(), StatusCode::CREATED);
|
||||||
|
let first_body = body_to_vec(first.into_body()).await?;
|
||||||
|
let first_detail: DocumentDetail = serde_json::from_slice(&first_body)?;
|
||||||
|
|
||||||
|
let second = app
|
||||||
|
.upload_document(
|
||||||
|
"/api/documents",
|
||||||
|
"letter-two.txt",
|
||||||
|
"text/plain",
|
||||||
|
b"letter two",
|
||||||
|
None,
|
||||||
|
&token,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(second.status(), StatusCode::CREATED);
|
||||||
|
let second_body = body_to_vec(second.into_body()).await?;
|
||||||
|
let second_detail: DocumentDetail = serde_json::from_slice(&second_body)?;
|
||||||
|
|
||||||
|
let sender = app
|
||||||
|
.post_json(
|
||||||
|
"/api/correspondents",
|
||||||
|
&serde_json::json!({ "name": "Acme Corp" }),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(sender.status(), StatusCode::OK);
|
||||||
|
let sender_body = body_to_vec(sender.into_body()).await?;
|
||||||
|
let sender_summary: CorrespondentSummary = serde_json::from_slice(&sender_body)?;
|
||||||
|
|
||||||
|
let receiver = app
|
||||||
|
.post_json(
|
||||||
|
"/api/correspondents",
|
||||||
|
&serde_json::json!({ "name": "Bank Ltd" }),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(receiver.status(), StatusCode::OK);
|
||||||
|
let receiver_body = body_to_vec(receiver.into_body()).await?;
|
||||||
|
let receiver_summary: CorrespondentSummary = serde_json::from_slice(&receiver_body)?;
|
||||||
|
|
||||||
|
let assign_payload = serde_json::json!({
|
||||||
|
"document_ids": [
|
||||||
|
first_detail.document.id,
|
||||||
|
second_detail.document.id
|
||||||
|
],
|
||||||
|
"assignments": [
|
||||||
|
{
|
||||||
|
"correspondent_id": sender_summary.id,
|
||||||
|
"role": "sender"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"correspondent_id": receiver_summary.id,
|
||||||
|
"role": "receiver"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
let assign_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/documents/bulk/correspondents",
|
||||||
|
&assign_payload,
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(assign_resp.status(), StatusCode::OK);
|
||||||
|
let assign_body = body_to_vec(assign_resp.into_body()).await?;
|
||||||
|
let assign_result: BulkCorrespondentResult = serde_json::from_slice(&assign_body)?;
|
||||||
|
assert_eq!(assign_result.assigned, 4);
|
||||||
|
assert_eq!(assign_result.removed, 0);
|
||||||
|
|
||||||
|
for doc_id in [first_detail.document.id, second_detail.document.id] {
|
||||||
|
let refreshed = app
|
||||||
|
.get(&format!("/api/documents/{doc_id}"), Some(&token))
|
||||||
|
.await?;
|
||||||
|
assert_eq!(refreshed.status(), StatusCode::OK);
|
||||||
|
let refreshed_body = body_to_vec(refreshed.into_body()).await?;
|
||||||
|
let detail: DocumentDetail = serde_json::from_slice(&refreshed_body)?;
|
||||||
|
assert_eq!(detail.document.correspondents.len(), 2);
|
||||||
|
assert!(detail
|
||||||
|
.document
|
||||||
|
.correspondents
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.role == "sender" && entry.name == "Acme Corp"));
|
||||||
|
assert!(detail
|
||||||
|
.document
|
||||||
|
.correspondents
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.role == "receiver" && entry.name == "Bank Ltd"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let duplicate_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/documents/bulk/correspondents",
|
||||||
|
&assign_payload,
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(duplicate_resp.status(), StatusCode::OK);
|
||||||
|
let duplicate_body = body_to_vec(duplicate_resp.into_body()).await?;
|
||||||
|
let duplicate_result: BulkCorrespondentResult = serde_json::from_slice(&duplicate_body)?;
|
||||||
|
assert_eq!(duplicate_result.assigned, 0);
|
||||||
|
assert_eq!(duplicate_result.removed, 0);
|
||||||
|
|
||||||
|
let replacement = app
|
||||||
|
.post_json(
|
||||||
|
"/api/correspondents",
|
||||||
|
&serde_json::json!({ "name": "Charlie" }),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(replacement.status(), StatusCode::OK);
|
||||||
|
let replacement_body = body_to_vec(replacement.into_body()).await?;
|
||||||
|
let replacement_summary: CorrespondentSummary = serde_json::from_slice(&replacement_body)?;
|
||||||
|
|
||||||
|
let replace_payload = serde_json::json!({
|
||||||
|
"document_ids": [
|
||||||
|
first_detail.document.id,
|
||||||
|
second_detail.document.id
|
||||||
|
],
|
||||||
|
"assignments": [
|
||||||
|
{
|
||||||
|
"correspondent_id": replacement_summary.id,
|
||||||
|
"role": "sender"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
let replace_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/documents/bulk/correspondents",
|
||||||
|
&replace_payload,
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(replace_resp.status(), StatusCode::OK);
|
||||||
|
let replace_body = body_to_vec(replace_resp.into_body()).await?;
|
||||||
|
let replace_result: BulkCorrespondentResult = serde_json::from_slice(&replace_body)?;
|
||||||
|
assert_eq!(replace_result.assigned, 2);
|
||||||
|
assert_eq!(replace_result.removed, 2);
|
||||||
|
|
||||||
|
for doc_id in [first_detail.document.id, second_detail.document.id] {
|
||||||
|
let refreshed = app
|
||||||
|
.get(&format!("/api/documents/{doc_id}"), Some(&token))
|
||||||
|
.await?;
|
||||||
|
let refreshed_body = body_to_vec(refreshed.into_body()).await?;
|
||||||
|
let detail: DocumentDetail = serde_json::from_slice(&refreshed_body)?;
|
||||||
|
assert_eq!(detail.document.correspondents.len(), 2);
|
||||||
|
assert!(detail
|
||||||
|
.document
|
||||||
|
.correspondents
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.role == "sender" && entry.name == "Charlie"));
|
||||||
|
assert!(detail
|
||||||
|
.document
|
||||||
|
.correspondents
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.role == "receiver" && entry.name == "Bank Ltd"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let remove_payload = serde_json::json!({
|
||||||
|
"document_ids": [
|
||||||
|
first_detail.document.id,
|
||||||
|
second_detail.document.id
|
||||||
|
],
|
||||||
|
"assignments": [
|
||||||
|
{
|
||||||
|
"correspondent_id": receiver_summary.id,
|
||||||
|
"role": "receiver"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"action": "remove"
|
||||||
|
});
|
||||||
|
|
||||||
|
let remove_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/documents/bulk/correspondents",
|
||||||
|
&remove_payload,
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(remove_resp.status(), StatusCode::OK);
|
||||||
|
let remove_body = body_to_vec(remove_resp.into_body()).await?;
|
||||||
|
let remove_result: BulkCorrespondentResult = serde_json::from_slice(&remove_body)?;
|
||||||
|
assert_eq!(remove_result.assigned, 0);
|
||||||
|
assert_eq!(remove_result.removed, 2);
|
||||||
|
|
||||||
|
for doc_id in [first_detail.document.id, second_detail.document.id] {
|
||||||
|
let refreshed = app
|
||||||
|
.get(&format!("/api/documents/{doc_id}"), Some(&token))
|
||||||
|
.await?;
|
||||||
|
let refreshed_body = body_to_vec(refreshed.into_body()).await?;
|
||||||
|
let detail: DocumentDetail = serde_json::from_slice(&refreshed_body)?;
|
||||||
|
assert_eq!(detail.document.correspondents.len(), 1);
|
||||||
|
assert!(detail
|
||||||
|
.document
|
||||||
|
.correspondents
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.role == "sender" && entry.name == "Charlie"));
|
||||||
|
assert!(!detail
|
||||||
|
.document
|
||||||
|
.correspondents
|
||||||
|
.iter()
|
||||||
|
.any(|entry| entry.role == "receiver"));
|
||||||
|
}
|
||||||
|
|
||||||
|
app.cleanup().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn bulk_reanalyze_selected_documents() -> Result<()> {
|
async fn bulk_reanalyze_selected_documents() -> Result<()> {
|
||||||
let _lock = acquire_db_lock().await;
|
let _lock = acquire_db_lock().await;
|
||||||
@@ -648,8 +894,24 @@ async fn bulk_reanalyze_selected_documents() -> Result<()> {
|
|||||||
.all(|(doc_id, _)| *doc_id != second_detail.document.id));
|
.all(|(doc_id, _)| *doc_id != second_detail.document.id));
|
||||||
|
|
||||||
let mut expected = vec![
|
let mut expected = vec![
|
||||||
(first_detail.document.id, first_detail.current_version.id),
|
(
|
||||||
(third_detail.document.id, third_detail.current_version.id),
|
first_detail.document.id,
|
||||||
|
first_detail
|
||||||
|
.document
|
||||||
|
.current_version
|
||||||
|
.as_ref()
|
||||||
|
.expect("first current version")
|
||||||
|
.id,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
third_detail.document.id,
|
||||||
|
third_detail
|
||||||
|
.document
|
||||||
|
.current_version
|
||||||
|
.as_ref()
|
||||||
|
.expect("third current version")
|
||||||
|
.id,
|
||||||
|
),
|
||||||
];
|
];
|
||||||
payload_docs.sort();
|
payload_docs.sort();
|
||||||
expected.sort();
|
expected.sort();
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use axum::http::StatusCode;
|
|||||||
use common::{acquire_db_lock, body_to_vec, TestApp};
|
use common::{acquire_db_lock, body_to_vec, TestApp};
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use serde_json::json;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
@@ -15,10 +16,14 @@ struct FolderResponse {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct FolderInfo {
|
struct FolderInfo {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
|
name: String,
|
||||||
|
parent_id: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct FolderContents {
|
struct FolderContents {
|
||||||
|
folder: Option<FolderInfo>,
|
||||||
|
subfolders: Vec<FolderInfo>,
|
||||||
documents: Vec<DocSummary>,
|
documents: Vec<DocSummary>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -139,6 +144,78 @@ async fn folder_move_and_delete_flow() -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn update_folder_parent_to_root() -> Result<()> {
|
||||||
|
let _lock = acquire_db_lock().await;
|
||||||
|
let app = TestApp::new().await?;
|
||||||
|
|
||||||
|
let password = "rootpass";
|
||||||
|
app.insert_user("root-admin", password, "admin").await?;
|
||||||
|
let token = app.login_token("root-admin", password).await?;
|
||||||
|
|
||||||
|
// Create a parent folder under root
|
||||||
|
let parent_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/folders",
|
||||||
|
&CreateFolder {
|
||||||
|
name: "Parent",
|
||||||
|
parent_id: None,
|
||||||
|
},
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(parent_resp.status(), StatusCode::OK);
|
||||||
|
let parent_body = body_to_vec(parent_resp.into_body()).await?;
|
||||||
|
let parent: FolderResponse = serde_json::from_slice(&parent_body)?;
|
||||||
|
|
||||||
|
// Create a child folder inside the parent
|
||||||
|
let child_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/folders",
|
||||||
|
&CreateFolder {
|
||||||
|
name: "Child",
|
||||||
|
parent_id: Some(parent.folder.id),
|
||||||
|
},
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(child_resp.status(), StatusCode::OK);
|
||||||
|
let child_body = body_to_vec(child_resp.into_body()).await?;
|
||||||
|
let child: FolderResponse = serde_json::from_slice(&child_body)?;
|
||||||
|
|
||||||
|
// Move the child back to the root by setting parent_id to null
|
||||||
|
let update_resp = app
|
||||||
|
.patch_json(
|
||||||
|
&format!("/api/folders/{}", child.folder.id),
|
||||||
|
&json!({ "parent_id": null }),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(update_resp.status(), StatusCode::NO_CONTENT);
|
||||||
|
|
||||||
|
// Fetch the child folder and ensure parent_id is now null
|
||||||
|
let updated_resp = app
|
||||||
|
.get(&format!("/api/folders/{}", child.folder.id), Some(&token))
|
||||||
|
.await?;
|
||||||
|
assert_eq!(updated_resp.status(), StatusCode::OK);
|
||||||
|
let updated_body = body_to_vec(updated_resp.into_body()).await?;
|
||||||
|
let updated_folder: FolderResponse = serde_json::from_slice(&updated_body)?;
|
||||||
|
assert!(updated_folder.folder.parent_id.is_none());
|
||||||
|
|
||||||
|
// Root contents should include the child folder by name
|
||||||
|
let root_contents = app.get("/api/folders/root/contents", Some(&token)).await?;
|
||||||
|
assert_eq!(root_contents.status(), StatusCode::OK);
|
||||||
|
let root_body = body_to_vec(root_contents.into_body()).await?;
|
||||||
|
let root: FolderContents = serde_json::from_slice(&root_body)?;
|
||||||
|
assert!(root
|
||||||
|
.subfolders
|
||||||
|
.iter()
|
||||||
|
.any(|folder| folder.id == child.folder.id));
|
||||||
|
|
||||||
|
app.cleanup().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn ensure_path_creates_nested_folders() -> Result<()> {
|
async fn ensure_path_creates_nested_folders() -> Result<()> {
|
||||||
let _lock = acquire_db_lock().await;
|
let _lock = acquire_db_lock().await;
|
||||||
@@ -215,3 +292,90 @@ async fn ensure_path_creates_nested_folders() -> Result<()> {
|
|||||||
app.cleanup().await?;
|
app.cleanup().await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn folder_rename_updates_name_and_child_paths() -> Result<()> {
|
||||||
|
let _lock = acquire_db_lock().await;
|
||||||
|
let app = TestApp::new().await?;
|
||||||
|
|
||||||
|
let password = "renamepass";
|
||||||
|
app.insert_user("rename-admin", password, "admin").await?;
|
||||||
|
let token = app.login_token("rename-admin", password).await?;
|
||||||
|
|
||||||
|
let parent_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/folders",
|
||||||
|
&CreateFolder {
|
||||||
|
name: "Projects",
|
||||||
|
parent_id: None,
|
||||||
|
},
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(parent_resp.status(), StatusCode::OK);
|
||||||
|
let parent_body = body_to_vec(parent_resp.into_body()).await?;
|
||||||
|
let parent: FolderResponse = serde_json::from_slice(&parent_body)?;
|
||||||
|
|
||||||
|
let child_resp = app
|
||||||
|
.post_json(
|
||||||
|
"/api/folders",
|
||||||
|
&CreateFolder {
|
||||||
|
name: "Q1",
|
||||||
|
parent_id: Some(parent.folder.id),
|
||||||
|
},
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(child_resp.status(), StatusCode::OK);
|
||||||
|
let child_body = body_to_vec(child_resp.into_body()).await?;
|
||||||
|
let child: FolderResponse = serde_json::from_slice(&child_body)?;
|
||||||
|
|
||||||
|
let rename_resp = app
|
||||||
|
.patch_json(
|
||||||
|
&format!("/api/folders/{}", parent.folder.id),
|
||||||
|
&json!({ "name": "Archive" }),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(rename_resp.status(), StatusCode::NO_CONTENT);
|
||||||
|
|
||||||
|
let root_contents = app.get("/api/folders/root/contents", Some(&token)).await?;
|
||||||
|
assert_eq!(root_contents.status(), StatusCode::OK);
|
||||||
|
let root_body = body_to_vec(root_contents.into_body()).await?;
|
||||||
|
let root: FolderContents = serde_json::from_slice(&root_body)?;
|
||||||
|
let renamed = root
|
||||||
|
.subfolders
|
||||||
|
.iter()
|
||||||
|
.find(|f| f.id == parent.folder.id)
|
||||||
|
.expect("renamed folder present");
|
||||||
|
assert_eq!(renamed.name, "Archive");
|
||||||
|
|
||||||
|
let folders_only = app
|
||||||
|
.get(
|
||||||
|
&format!(
|
||||||
|
"/api/folders/{}/contents?include_documents=false",
|
||||||
|
parent.folder.id
|
||||||
|
),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(folders_only.status(), StatusCode::OK);
|
||||||
|
let folders_only_body = body_to_vec(folders_only.into_body()).await?;
|
||||||
|
let folders_only_contents: FolderContents = serde_json::from_slice(&folders_only_body)?;
|
||||||
|
assert!(folders_only_contents.documents.is_empty());
|
||||||
|
|
||||||
|
let child_contents = app
|
||||||
|
.get(
|
||||||
|
&format!("/api/folders/{}/contents", child.folder.id),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
assert_eq!(child_contents.status(), StatusCode::OK);
|
||||||
|
let child_contents_body = body_to_vec(child_contents.into_body()).await?;
|
||||||
|
let child_details: FolderContents = serde_json::from_slice(&child_contents_body)?;
|
||||||
|
let child_folder = child_details.folder.expect("child folder info");
|
||||||
|
assert_eq!(child_folder.name, "Q1");
|
||||||
|
|
||||||
|
app.cleanup().await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|||||||
@@ -21,11 +21,16 @@ struct DocumentInfo {
|
|||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct TagInfo {
|
struct TagInfo {
|
||||||
label: String,
|
label: String,
|
||||||
|
#[allow(dead_code)]
|
||||||
|
color: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct TagResponse {
|
struct TagResponse {
|
||||||
id: Uuid,
|
id: Uuid,
|
||||||
|
label: String,
|
||||||
|
color: Option<String>,
|
||||||
|
usage_count: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@@ -75,6 +80,53 @@ async fn tag_assignment_flow() -> Result<()> {
|
|||||||
assert_eq!(create_tag.status(), StatusCode::OK);
|
assert_eq!(create_tag.status(), StatusCode::OK);
|
||||||
let body = body_to_vec(create_tag.into_body()).await?;
|
let body = body_to_vec(create_tag.into_body()).await?;
|
||||||
let tag: TagResponse = serde_json::from_slice(&body)?;
|
let tag: TagResponse = serde_json::from_slice(&body)?;
|
||||||
|
assert_eq!(tag.label, "Important");
|
||||||
|
assert_eq!(tag.color.as_deref(), Some("#FF0000"));
|
||||||
|
assert_eq!(tag.usage_count, 0);
|
||||||
|
|
||||||
|
let update = app
|
||||||
|
.patch_json(
|
||||||
|
&format!("/api/tags/{}", tag.id),
|
||||||
|
&serde_json::json!({
|
||||||
|
"label": "Critical",
|
||||||
|
"color": "#00FF00"
|
||||||
|
}),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let updated_status = update.status();
|
||||||
|
let updated_body = body_to_vec(update.into_body()).await?;
|
||||||
|
if updated_status != StatusCode::OK {
|
||||||
|
panic!(
|
||||||
|
"update tag failed: {}",
|
||||||
|
String::from_utf8_lossy(&updated_body)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let updated: TagResponse = serde_json::from_slice(&updated_body)?;
|
||||||
|
assert_eq!(updated.label, "Critical");
|
||||||
|
assert_eq!(updated.color.as_deref(), Some("#00FF00"));
|
||||||
|
assert_eq!(updated.usage_count, 0);
|
||||||
|
|
||||||
|
let clear_color = app
|
||||||
|
.patch_json(
|
||||||
|
&format!("/api/tags/{}", tag.id),
|
||||||
|
&serde_json::json!({
|
||||||
|
"color": null
|
||||||
|
}),
|
||||||
|
Some(&token),
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
let cleared_status = clear_color.status();
|
||||||
|
let cleared_body = body_to_vec(clear_color.into_body()).await?;
|
||||||
|
if cleared_status != StatusCode::OK {
|
||||||
|
panic!(
|
||||||
|
"clear color failed: {}",
|
||||||
|
String::from_utf8_lossy(&cleared_body)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let cleared: TagResponse = serde_json::from_slice(&cleared_body)?;
|
||||||
|
assert_eq!(cleared.color, None);
|
||||||
|
assert_eq!(cleared.usage_count, 0);
|
||||||
|
|
||||||
let assign = app
|
let assign = app
|
||||||
.post_json(
|
.post_json(
|
||||||
@@ -97,7 +149,7 @@ async fn tag_assignment_flow() -> Result<()> {
|
|||||||
let refreshed_body = body_to_vec(refreshed.into_body()).await?;
|
let refreshed_body = body_to_vec(refreshed.into_body()).await?;
|
||||||
let refreshed_detail: DocumentDetail = serde_json::from_slice(&refreshed_body)?;
|
let refreshed_detail: DocumentDetail = serde_json::from_slice(&refreshed_body)?;
|
||||||
assert_eq!(refreshed_detail.document.tags.len(), 1);
|
assert_eq!(refreshed_detail.document.tags.len(), 1);
|
||||||
assert_eq!(refreshed_detail.document.tags[0].label, "Important");
|
assert_eq!(refreshed_detail.document.tags[0].label, "Critical");
|
||||||
|
|
||||||
let remove = app
|
let remove = app
|
||||||
.delete(
|
.delete(
|
||||||
|
|||||||
+59
-4
@@ -2,15 +2,70 @@ services:
|
|||||||
postgres-test:
|
postgres-test:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: paperless
|
POSTGRES_USER: papercrate
|
||||||
POSTGRES_PASSWORD: paperless_test
|
POSTGRES_PASSWORD: papercrate_test
|
||||||
POSTGRES_DB: paperless_test
|
POSTGRES_DB: papercrate_test
|
||||||
ports:
|
ports:
|
||||||
- "5433:5432"
|
- "5433:5432"
|
||||||
tmpfs:
|
tmpfs:
|
||||||
- /var/lib/postgresql/data
|
- /var/lib/postgresql/data
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U paperless"]
|
test: ["CMD-SHELL", "pg_isready -U papercrate -d papercrate_test"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
|
|
||||||
|
quickwit-test:
|
||||||
|
image: quickwit/quickwit:0.8.2
|
||||||
|
command: ["run"]
|
||||||
|
environment:
|
||||||
|
QW_ENABLE_API_AUTH: "false"
|
||||||
|
QW_DATA_DIR: /quickwit/data
|
||||||
|
ports:
|
||||||
|
- "7281:7280"
|
||||||
|
volumes:
|
||||||
|
- quickwit_test_data:/quickwit/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-sf", "http://127.0.0.1:7280/api/v1/version"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
admin-bootstrap:
|
||||||
|
build:
|
||||||
|
context: ./backend
|
||||||
|
depends_on:
|
||||||
|
postgres-test:
|
||||||
|
condition: service_healthy
|
||||||
|
quickwit-test:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgres://papercrate:papercrate_test@postgres-test:5432/papercrate_test
|
||||||
|
DATABASE_MAX_POOL_SIZE: 1
|
||||||
|
S3_BUCKET: documents
|
||||||
|
JWT_SECRET: change-me-super-secret
|
||||||
|
QUICKWIT_ENDPOINT: http://quickwit-test:7280
|
||||||
|
DEFAULT_TENANT_SLUG: admin
|
||||||
|
entrypoint: []
|
||||||
|
command: >
|
||||||
|
/bin/sh -c "
|
||||||
|
echo 'Running database migrations' &&
|
||||||
|
diesel migration run &&
|
||||||
|
echo 'Ensuring tenant admin exists' &&
|
||||||
|
if papercrate-admin list-tenants | grep -q '^admin '; then
|
||||||
|
echo 'tenant admin already exists';
|
||||||
|
else
|
||||||
|
papercrate-admin create-tenant admin;
|
||||||
|
fi &&
|
||||||
|
echo 'Ensuring demo user credentials' &&
|
||||||
|
(papercrate-admin create-user admin adminadmin || papercrate-admin set-password admin adminadmin) &&
|
||||||
|
echo 'Ensuring demo membership' &&
|
||||||
|
papercrate-admin add-user-to-tenant admin admin admin &&
|
||||||
|
echo 'Ensuring Quickwit index for admin tenant' &&
|
||||||
|
papercrate-admin quickwit-create-index admin
|
||||||
|
"
|
||||||
|
user: root
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
quickwit_test_data:
|
||||||
|
|||||||
+61
-6
@@ -1,19 +1,17 @@
|
|||||||
version: '3.8'
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
postgres:
|
postgres:
|
||||||
image: postgres:16-alpine
|
image: postgres:16-alpine
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: paperless
|
POSTGRES_USER: papercrate
|
||||||
POSTGRES_PASSWORD: paperless_dev
|
POSTGRES_PASSWORD: papercrate_dev
|
||||||
POSTGRES_DB: paperless
|
POSTGRES_DB: papercrate
|
||||||
ports:
|
ports:
|
||||||
- "5432:5432"
|
- "5432:5432"
|
||||||
volumes:
|
volumes:
|
||||||
- postgres_data:/var/lib/postgresql/data
|
- postgres_data:/var/lib/postgresql/data
|
||||||
- ./backend/migrations:/docker-entrypoint-initdb.d
|
- ./backend/migrations:/docker-entrypoint-initdb.d
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U paperless"]
|
test: ["CMD-SHELL", "pg_isready -U papercrate"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 5s
|
timeout: 5s
|
||||||
retries: 5
|
retries: 5
|
||||||
@@ -48,6 +46,63 @@ services:
|
|||||||
exit 0;
|
exit 0;
|
||||||
"
|
"
|
||||||
|
|
||||||
|
quickwit:
|
||||||
|
image: quickwit/quickwit:0.8.2
|
||||||
|
command: ["run"]
|
||||||
|
environment:
|
||||||
|
QW_ENABLE_API_AUTH: "false"
|
||||||
|
QW_DATA_DIR: /quickwit/data
|
||||||
|
ports:
|
||||||
|
- "7280:7280"
|
||||||
|
volumes:
|
||||||
|
- quickwit_data:/quickwit/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-sf", "http://127.0.0.1:7280/api/v1/version"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
admin-bootstrap:
|
||||||
|
build:
|
||||||
|
context: ./backend
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
quickwit:
|
||||||
|
condition: service_healthy
|
||||||
|
environment:
|
||||||
|
DATABASE_URL: postgres://papercrate:papercrate_dev@postgres:5432/papercrate
|
||||||
|
DATABASE_MAX_POOL_SIZE: 2
|
||||||
|
AWS_ENDPOINT_URL: http://minio:9000
|
||||||
|
AWS_ACCESS_KEY_ID: minioadmin
|
||||||
|
AWS_SECRET_ACCESS_KEY: minioadmin
|
||||||
|
AWS_REGION: us-east-1
|
||||||
|
S3_BUCKET: documents
|
||||||
|
JWT_SECRET: change-me-super-secret
|
||||||
|
QUICKWIT_ENDPOINT: http://quickwit:7280
|
||||||
|
DEFAULT_TENANT_SLUG: admin
|
||||||
|
entrypoint: []
|
||||||
|
command: >
|
||||||
|
/bin/sh -c "
|
||||||
|
echo 'Running database migrations' &&
|
||||||
|
diesel migration run &&
|
||||||
|
echo 'Ensuring tenant admin exists' &&
|
||||||
|
if papercrate-admin list-tenants | grep -q '^admin '; then
|
||||||
|
echo 'tenant admin already exists';
|
||||||
|
else
|
||||||
|
papercrate-admin create-tenant admin;
|
||||||
|
fi &&
|
||||||
|
echo 'Ensuring demo user credentials' &&
|
||||||
|
(papercrate-admin create-user admin adminadmin || papercrate-admin set-password admin adminadmin) &&
|
||||||
|
echo 'Ensuring demo membership' &&
|
||||||
|
papercrate-admin add-user-to-tenant admin admin admin &&
|
||||||
|
echo 'Ensuring Quickwit index for admin tenant' &&
|
||||||
|
papercrate-admin quickwit-create-index admin
|
||||||
|
"
|
||||||
|
user: root
|
||||||
|
restart: "no"
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
postgres_data:
|
postgres_data:
|
||||||
minio_data:
|
minio_data:
|
||||||
|
quickwit_data:
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
Papercrate REST API
|
||||||
|
===================
|
||||||
|
|
||||||
|
Unless noted otherwise, endpoints below require a valid `Authorization: Bearer <token>` header.
|
||||||
|
|
||||||
|
Authentication
|
||||||
|
--------------
|
||||||
|
- POST /api/auth/login - Exchange username/password for an access token and refresh cookie (public).
|
||||||
|
- POST /api/auth/refresh - Rotate the refresh cookie and return a new access token (public, requires refresh cookie).
|
||||||
|
- POST /api/auth/logout - Revoke the caller's refresh tokens and clear the cookie.
|
||||||
|
- GET /api/auth/me - Return the authenticated principal payload.
|
||||||
|
|
||||||
|
Health
|
||||||
|
------
|
||||||
|
- GET /api/health - Lightweight liveness probe (no authentication required).
|
||||||
|
|
||||||
|
Documents
|
||||||
|
---------
|
||||||
|
- GET /api/documents - List or search documents. Optional filters: `folder_id` (defaults to root when omitted), `include_deleted`, `include_descendants` (defaults to true when a `folder_id` is provided and no other override is supplied), `query` (Quickwit full-text), `tags` (comma-separated tag UUIDs), and `correspondents` (comma-separated correspondent UUIDs). Each entry includes tags, correspondent assignments, and current version info.
|
||||||
|
- GET /api/documents/check?checksum=<sha256> - Lightweight checksum preflight. Returns `exists=false` when no document with the supplied SHA-256 checksum is present; otherwise returns `exists=true` plus the current document metadata.
|
||||||
|
- POST /api/documents - Upload a document via multipart form-data. Required field: `file`. Optional fields: `title`, `folder_id`, JSON `metadata`, JSON array `tag_ids`, JSON array `correspondents` (each with `correspondent_id` and `role`), and `issued_at` (RFC3339). When `title` is supplied, the stored filename becomes `<title><original_extension>`. Include `skip_existing=true` to receive `204 No Content` instead of reusing a matching document.
|
||||||
|
- POST /api/documents/bulk/move - Move multiple documents to a target folder.
|
||||||
|
- POST /api/documents/bulk/tags - Add or remove tags across multiple documents.
|
||||||
|
- POST /api/documents/bulk/correspondents - Bulk correspondent actions. Default `action=add` replaces existing assignments for the provided roles before adding the supplied correspondents; `action=remove` drops the specified correspondent/role pairs.
|
||||||
|
- POST /api/documents/bulk/reanalyze - Queue re-analysis jobs for selected documents.
|
||||||
|
- GET /api/documents/:id - Retrieve metadata and current version details for a document.
|
||||||
|
- PATCH /api/documents/:id - Update document metadata (currently title).
|
||||||
|
- DELETE /api/documents/:id - Soft-delete a document.
|
||||||
|
- GET /api/documents/:id/download - Create a pre-signed download URL for the current version.
|
||||||
|
- PATCH /api/documents/:id/folder - Move a document to another folder.
|
||||||
|
- POST /api/documents/:id/tags - Assign one or more tags to a document.
|
||||||
|
- DELETE /api/documents/:id/tags/:tag_id - Remove a single tag from a document.
|
||||||
|
- POST /api/documents/:id/correspondents - Assign correspondents to roles (`assignments[]` with `correspondent_id` and `role`; optional `replace=true` overwrites existing assignments for those roles). Valid roles: `sender`, `receiver`, `other`.
|
||||||
|
- DELETE /api/documents/:id/correspondents/:correspondent_id - Remove a correspondent assignment (requires `role` query string).
|
||||||
|
|
||||||
|
Document Assets
|
||||||
|
---------------
|
||||||
|
- GET /api/documents/:id/assets - List generated assets for the current version.
|
||||||
|
- POST /api/documents/:id/assets - Request (re)generation of document assets; accepts optional `force` query flag.
|
||||||
|
- GET /api/assets/:asset_id - Fetch asset metadata plus a presigned URL for a range of objects (query params: `start` and `limit`, defaulting to the first object).
|
||||||
|
|
||||||
|
Downloads
|
||||||
|
---------
|
||||||
|
- GET /download/:token - Follow a one-time download token; redirects to a pre-signed URL (public token required).
|
||||||
|
|
||||||
|
Folders
|
||||||
|
-------
|
||||||
|
- POST /api/folders - Create a folder (optionally under a parent).
|
||||||
|
- POST /api/folders/path - Ensure a nested folder path exists, creating missing segments.
|
||||||
|
- GET /api/folders/:id - Fetch folder metadata.
|
||||||
|
- GET /api/folders/:id/contents - List subfolders and documents inside a folder; use `root` for the workspace root.
|
||||||
|
- DELETE /api/folders/:id - Soft-delete a folder.
|
||||||
|
- PATCH /api/folders/:id - Update a folder's parent (`parent_id`) and/or rename it (`name`).
|
||||||
|
- GET /api/docs/openapi.json - Generated OpenAPI specification (JSON).
|
||||||
|
|
||||||
|
Tags
|
||||||
|
----
|
||||||
|
- GET /api/tags - List all tags with usage counts.
|
||||||
|
- POST /api/tags - Create a new tag.
|
||||||
|
- PATCH /api/tags/:id - Update a tag's label or color.
|
||||||
|
- DELETE /api/tags/:id - Remove a tag; fails with 400 if still assigned to any document.
|
||||||
|
|
||||||
|
Correspondents
|
||||||
|
--------------
|
||||||
|
- GET /api/correspondents - List correspondents with usage totals and per-role counts (roles: `sender`, `receiver`, `other`).
|
||||||
|
- POST /api/correspondents - Create a correspondent (name + optional metadata JSON).
|
||||||
|
- PATCH /api/correspondents/:id - Update name and/or metadata.
|
||||||
|
- DELETE /api/correspondents/:id - Remove a correspondent; fails with 400 if referenced by any document.
|
||||||
+2
-2
@@ -6,10 +6,10 @@ The backend integration tests talk to a real Postgres database. To spin up an ep
|
|||||||
docker compose -f docker-compose.test.yml up -d
|
docker compose -f docker-compose.test.yml up -d
|
||||||
```
|
```
|
||||||
|
|
||||||
This starts Postgres on port `5433` with the database/user both named `paperless` and password `paperless_test`. Point the test harness at it:
|
This starts Postgres on port `5433` with the database/user both named `papercrate` and password `papercrate_test`. Point the test harness at it:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
export TEST_DATABASE_URL=postgres://paperless:paperless_test@localhost:5433/paperless_test
|
export TEST_DATABASE_URL=postgres://papercrate:papercrate_test@localhost:5433/papercrate_test
|
||||||
```
|
```
|
||||||
|
|
||||||
Run the tests as usual:
|
Run the tests as usual:
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# syntax=docker/dockerfile:1
|
||||||
|
|
||||||
|
FROM node:20-alpine AS build
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY package.json package-lock.json ./
|
||||||
|
RUN npm ci --no-audit --no-fund
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
FROM nginx:alpine
|
||||||
|
WORKDIR /usr/share/nginx/html
|
||||||
|
|
||||||
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
||||||
|
COPY --from=build /app/dist ./
|
||||||
|
|
||||||
|
ENV API_BASE_URL=""
|
||||||
|
|
||||||
|
COPY docker-entrypoint.sh /docker-entrypoint.sh
|
||||||
|
RUN chmod +x /docker-entrypoint.sh
|
||||||
|
|
||||||
|
EXPOSE 80
|
||||||
|
ENTRYPOINT ["/docker-entrypoint.sh"]
|
||||||
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
+6
-2
@@ -1,6 +1,6 @@
|
|||||||
# Paperless-NEO Frontend
|
# Papercrate Frontend
|
||||||
|
|
||||||
A minimal Webpack-powered SPA to interact with the Paperless-NEO Milestone 1 backend.
|
A minimal Webpack-powered SPA to interact with the Papercrate Milestone 1 backend.
|
||||||
|
|
||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
@@ -41,3 +41,7 @@ npm run build
|
|||||||
- Tag management (create/assign/remove) from the detail panel
|
- Tag management (create/assign/remove) from the detail panel
|
||||||
- Login via the seeded admin account (`admin` / `adminadmin`) with stored JWT session
|
- Login via the seeded admin account (`admin` / `adminadmin`) with stored JWT session
|
||||||
- Inline status banner for quick feedback on API interactions
|
- Inline status banner for quick feedback on API interactions
|
||||||
|
|
||||||
|
## Assets
|
||||||
|
|
||||||
|
- The folder icon (`src/assets/folder.svg`) is derived from the Adwaita icon theme by the [GNOME Project](http://www.gnome.org/).
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -euo pipefail
|
||||||
|
|
||||||
|
API_BASE_URL_TRIMMED="${API_BASE_URL:-}"
|
||||||
|
API_BASE_URL_TRIMMED="${API_BASE_URL_TRIMMED%%/}"
|
||||||
|
|
||||||
|
cat <<CONFIG > /usr/share/nginx/html/config.js
|
||||||
|
window.__PAPERCRATE_API_BASE_URL = "${API_BASE_URL_TRIMMED}";
|
||||||
|
CONFIG
|
||||||
|
|
||||||
|
exec "$@"
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
server {
|
||||||
|
listen 80;
|
||||||
|
server_name _;
|
||||||
|
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
index index.html;
|
||||||
|
|
||||||
|
location / {
|
||||||
|
try_files $uri /index.html;
|
||||||
|
}
|
||||||
|
}
|
||||||
Generated
+756
-2
@@ -1,13 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "paperless-neo-frontend",
|
"name": "papercrate-frontend",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "paperless-neo-frontend",
|
"name": "papercrate-frontend",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@tabler/icons-react": "3.11.0",
|
||||||
"axios": "1.7.7",
|
"axios": "1.7.7",
|
||||||
"react": "18.3.1",
|
"react": "18.3.1",
|
||||||
"react-dom": "18.3.1",
|
"react-dom": "18.3.1",
|
||||||
@@ -17,6 +18,7 @@
|
|||||||
"@babel/core": "7.26.0",
|
"@babel/core": "7.26.0",
|
||||||
"@babel/preset-env": "7.26.0",
|
"@babel/preset-env": "7.26.0",
|
||||||
"@babel/preset-react": "7.26.3",
|
"@babel/preset-react": "7.26.3",
|
||||||
|
"@svgr/webpack": "8.1.0",
|
||||||
"babel-loader": "9.2.1",
|
"babel-loader": "9.2.1",
|
||||||
"css-loader": "7.1.2",
|
"css-loader": "7.1.2",
|
||||||
"dotenv": "16.4.5",
|
"dotenv": "16.4.5",
|
||||||
@@ -640,6 +642,22 @@
|
|||||||
"@babel/core": "^7.0.0-0"
|
"@babel/core": "^7.0.0-0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@babel/plugin-syntax-typescript": {
|
||||||
|
"version": "7.27.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.27.1.tgz",
|
||||||
|
"integrity": "sha512-xfYCBMxveHrRMnAWl1ZlPXOZjzkN82THFvLhQhFXFt81Z5HnN+EtUkZhv/zcKpmT3fzmWZB0ywiBrbC3vogbwQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/helper-plugin-utils": "^7.27.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.9.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@babel/plugin-syntax-unicode-sets-regex": {
|
"node_modules/@babel/plugin-syntax-unicode-sets-regex": {
|
||||||
"version": "7.18.6",
|
"version": "7.18.6",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-unicode-sets-regex/-/plugin-syntax-unicode-sets-regex-7.18.6.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/plugin-syntax-unicode-sets-regex/-/plugin-syntax-unicode-sets-regex-7.18.6.tgz",
|
||||||
@@ -1299,6 +1317,22 @@
|
|||||||
"@babel/core": "^7.0.0-0"
|
"@babel/core": "^7.0.0-0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@babel/plugin-transform-react-constant-elements": {
|
||||||
|
"version": "7.27.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-constant-elements/-/plugin-transform-react-constant-elements-7.27.1.tgz",
|
||||||
|
"integrity": "sha512-edoidOjl/ZxvYo4lSBOQGDSyToYVkTAwyVoa2tkuYTSmjrB1+uAedoL5iROVLXkxH+vRgA7uP4tMg2pUJpZ3Ug==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/helper-plugin-utils": "^7.27.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.9.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@babel/plugin-transform-react-display-name": {
|
"node_modules/@babel/plugin-transform-react-display-name": {
|
||||||
"version": "7.28.0",
|
"version": "7.28.0",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-display-name/-/plugin-transform-react-display-name-7.28.0.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-react-display-name/-/plugin-transform-react-display-name-7.28.0.tgz",
|
||||||
@@ -1498,6 +1532,26 @@
|
|||||||
"@babel/core": "^7.0.0-0"
|
"@babel/core": "^7.0.0-0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@babel/plugin-transform-typescript": {
|
||||||
|
"version": "7.28.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.28.0.tgz",
|
||||||
|
"integrity": "sha512-4AEiDEBPIZvLQaWlc9liCavE0xRM0dNca41WtBeM3jgFptfUOSG9z0uteLhq6+3rq+WB6jIvUwKDTpXEHPJ2Vg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/helper-annotate-as-pure": "^7.27.3",
|
||||||
|
"@babel/helper-create-class-features-plugin": "^7.27.1",
|
||||||
|
"@babel/helper-plugin-utils": "^7.27.1",
|
||||||
|
"@babel/helper-skip-transparent-expression-wrappers": "^7.27.1",
|
||||||
|
"@babel/plugin-syntax-typescript": "^7.27.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.9.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@babel/plugin-transform-unicode-escapes": {
|
"node_modules/@babel/plugin-transform-unicode-escapes": {
|
||||||
"version": "7.27.1",
|
"version": "7.27.1",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.27.1.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/plugin-transform-unicode-escapes/-/plugin-transform-unicode-escapes-7.27.1.tgz",
|
||||||
@@ -1695,6 +1749,26 @@
|
|||||||
"@babel/core": "^7.0.0-0"
|
"@babel/core": "^7.0.0-0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@babel/preset-typescript": {
|
||||||
|
"version": "7.27.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@babel/preset-typescript/-/preset-typescript-7.27.1.tgz",
|
||||||
|
"integrity": "sha512-l7WfQfX0WK4M0v2RudjuQK4u99BS6yLHYEmdtVPP7lKV013zr9DygFuWNlnbvQ9LR+LS0Egz/XAvGx5U9MX0fQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/helper-plugin-utils": "^7.27.1",
|
||||||
|
"@babel/helper-validator-option": "^7.27.1",
|
||||||
|
"@babel/plugin-syntax-jsx": "^7.27.1",
|
||||||
|
"@babel/plugin-transform-modules-commonjs": "^7.27.1",
|
||||||
|
"@babel/plugin-transform-typescript": "^7.27.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6.9.0"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@babel/template": {
|
"node_modules/@babel/template": {
|
||||||
"version": "7.27.2",
|
"version": "7.27.2",
|
||||||
"resolved": "https://registry.npmjs.org/@babel/template/-/template-7.27.2.tgz",
|
"resolved": "https://registry.npmjs.org/@babel/template/-/template-7.27.2.tgz",
|
||||||
@@ -1963,6 +2037,326 @@
|
|||||||
"node": ">=14.0.0"
|
"node": ">=14.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-add-jsx-attribute": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-add-jsx-attribute/-/babel-plugin-add-jsx-attribute-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-b9MIk7yhdS1pMCZM8VeNfUlSKVRhsHZNMl5O9SfaX0l0t5wjdgu4IDzGB8bpnGBBOjGST3rRFVsaaEtI4W6f7g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-remove-jsx-attribute": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-remove-jsx-attribute/-/babel-plugin-remove-jsx-attribute-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-BcCkm/STipKvbCl6b7QFrMh/vx00vIP63k2eM66MfHJzPr6O2U0jYEViXkHJWqXqQYjdeA9cuCl5KWmlwjDvbA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-remove-jsx-empty-expression": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-remove-jsx-empty-expression/-/babel-plugin-remove-jsx-empty-expression-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-5BcGCBfBxB5+XSDSWnhTThfI9jcO5f0Ai2V24gZpG+wXF14BzwxxdDb4g6trdOux0rhibGs385BeFMSmxtS3uA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-replace-jsx-attribute-value": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-replace-jsx-attribute-value/-/babel-plugin-replace-jsx-attribute-value-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-KVQ+PtIjb1BuYT3ht8M5KbzWBhdAjjUPdlMtpuw/VjT8coTrItWX6Qafl9+ji831JaJcu6PJNKCV0bp01lBNzQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-svg-dynamic-title": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-svg-dynamic-title/-/babel-plugin-svg-dynamic-title-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-omNiKqwjNmOQJ2v6ge4SErBbkooV2aAWwaPFs2vUY7p7GhVkzRkJ00kILXQvRhA6miHnNpXv7MRnnSjdRjK8og==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-svg-em-dimensions": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-svg-em-dimensions/-/babel-plugin-svg-em-dimensions-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-mURHYnu6Iw3UBTbhGwE/vsngtCIbHE43xCRK7kCw4t01xyGqb2Pd+WXekRRoFOBIY29ZoOhUCTEweDMdrjfi9g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-transform-react-native-svg": {
|
||||||
|
"version": "8.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-transform-react-native-svg/-/babel-plugin-transform-react-native-svg-8.1.0.tgz",
|
||||||
|
"integrity": "sha512-Tx8T58CHo+7nwJ+EhUwx3LfdNSG9R2OKfaIXXs5soiy5HtgoAEkDay9LIimLOcG8dJQH1wPZp/cnAv6S9CrR1Q==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-plugin-transform-svg-component": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-plugin-transform-svg-component/-/babel-plugin-transform-svg-component-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-DFx8xa3cZXTdb/k3kfPeaixecQLgKh5NVBMwD0AQxOzcZawK4oo1Jh9LbrcACUivsCA7TLG8eeWgrDXjTMhRmw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/babel-preset": {
|
||||||
|
"version": "8.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/babel-preset/-/babel-preset-8.1.0.tgz",
|
||||||
|
"integrity": "sha512-7EYDbHE7MxHpv4sxvnVPngw5fuR6pw79SkcrILHJ/iMpuKySNCl5W1qcwPEpU+LgyRXOaAFgH0KhwD18wwg6ug==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@svgr/babel-plugin-add-jsx-attribute": "8.0.0",
|
||||||
|
"@svgr/babel-plugin-remove-jsx-attribute": "8.0.0",
|
||||||
|
"@svgr/babel-plugin-remove-jsx-empty-expression": "8.0.0",
|
||||||
|
"@svgr/babel-plugin-replace-jsx-attribute-value": "8.0.0",
|
||||||
|
"@svgr/babel-plugin-svg-dynamic-title": "8.0.0",
|
||||||
|
"@svgr/babel-plugin-svg-em-dimensions": "8.0.0",
|
||||||
|
"@svgr/babel-plugin-transform-react-native-svg": "8.1.0",
|
||||||
|
"@svgr/babel-plugin-transform-svg-component": "8.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@babel/core": "^7.0.0-0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/core": {
|
||||||
|
"version": "8.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/core/-/core-8.1.0.tgz",
|
||||||
|
"integrity": "sha512-8QqtOQT5ACVlmsvKOJNEaWmRPmcojMOzCz4Hs2BGG/toAp/K38LcsMRyLp349glq5AzJbCEeimEoxaX6v/fLrA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/core": "^7.21.3",
|
||||||
|
"@svgr/babel-preset": "8.1.0",
|
||||||
|
"camelcase": "^6.2.0",
|
||||||
|
"cosmiconfig": "^8.1.3",
|
||||||
|
"snake-case": "^3.0.4"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/hast-util-to-babel-ast": {
|
||||||
|
"version": "8.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/hast-util-to-babel-ast/-/hast-util-to-babel-ast-8.0.0.tgz",
|
||||||
|
"integrity": "sha512-EbDKwO9GpfWP4jN9sGdYwPBU0kdomaPIL2Eu4YwmgP+sJeXT+L7bMwJUBnhzfH8Q2qMBqZ4fJwpCyYsAN3mt2Q==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/types": "^7.21.3",
|
||||||
|
"entities": "^4.4.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/hast-util-to-babel-ast/node_modules/entities": {
|
||||||
|
"version": "4.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
|
||||||
|
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/fb55/entities?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/plugin-jsx": {
|
||||||
|
"version": "8.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/plugin-jsx/-/plugin-jsx-8.1.0.tgz",
|
||||||
|
"integrity": "sha512-0xiIyBsLlr8quN+WyuxooNW9RJ0Dpr8uOnH/xrCVO8GLUcwHISwj1AG0k+LFzteTkAA0GbX0kj9q6Dk70PTiPA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/core": "^7.21.3",
|
||||||
|
"@svgr/babel-preset": "8.1.0",
|
||||||
|
"@svgr/hast-util-to-babel-ast": "8.0.0",
|
||||||
|
"svg-parser": "^2.0.4"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@svgr/core": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/plugin-svgo": {
|
||||||
|
"version": "8.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/plugin-svgo/-/plugin-svgo-8.1.0.tgz",
|
||||||
|
"integrity": "sha512-Ywtl837OGO9pTLIN/onoWLmDQ4zFUycI1g76vuKGEz6evR/ZTJlJuz3G/fIkb6OVBJ2g0o6CGJzaEjfmEo3AHA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"cosmiconfig": "^8.1.3",
|
||||||
|
"deepmerge": "^4.3.1",
|
||||||
|
"svgo": "^3.0.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"@svgr/core": "*"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@svgr/webpack": {
|
||||||
|
"version": "8.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@svgr/webpack/-/webpack-8.1.0.tgz",
|
||||||
|
"integrity": "sha512-LnhVjMWyMQV9ZmeEy26maJk+8HTIbd59cH4F2MJ439k9DqejRisfFNGAPvRYlKETuh9LrImlS8aKsBgKjMA8WA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/core": "^7.21.3",
|
||||||
|
"@babel/plugin-transform-react-constant-elements": "^7.21.3",
|
||||||
|
"@babel/preset-env": "^7.20.2",
|
||||||
|
"@babel/preset-react": "^7.18.6",
|
||||||
|
"@babel/preset-typescript": "^7.21.0",
|
||||||
|
"@svgr/core": "8.1.0",
|
||||||
|
"@svgr/plugin-jsx": "8.1.0",
|
||||||
|
"@svgr/plugin-svgo": "8.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/gregberge"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@tabler/icons": {
|
||||||
|
"version": "3.11.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@tabler/icons/-/icons-3.11.0.tgz",
|
||||||
|
"integrity": "sha512-/vZinJNvCYhdAB+RUsyCpanSPuOEKHHIZi4Uu0Bw7ilewHnQhCWUPrT704uHCRli2ROl7spADPmWzAqOganA5A==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/codecalm"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@tabler/icons-react": {
|
||||||
|
"version": "3.11.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@tabler/icons-react/-/icons-react-3.11.0.tgz",
|
||||||
|
"integrity": "sha512-xHNBi9mns1slvqos+7LkP3ube4CjWrANMbxMaorzwzO9J/+y1sAEG/sN8CV8FmtpYW/9/gDR+OWCjjLLg0RmAw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@tabler/icons": "3.11.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "github",
|
||||||
|
"url": "https://github.com/sponsors/codecalm"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">= 16"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@trysound/sax": {
|
||||||
|
"version": "0.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/@trysound/sax/-/sax-0.2.0.tgz",
|
||||||
|
"integrity": "sha512-L7z9BgrNEcYyUYtF+HaEfiS5ebkh9jXqbszz7pC0hRBPaatV0XjSD3+eHrpqFemQfgwiFF0QPIarnIihIDn7OA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "ISC",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10.13.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@types/body-parser": {
|
"node_modules/@types/body-parser": {
|
||||||
"version": "1.19.6",
|
"version": "1.19.6",
|
||||||
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz",
|
"resolved": "https://registry.npmjs.org/@types/body-parser/-/body-parser-1.19.6.tgz",
|
||||||
@@ -2568,6 +2962,13 @@
|
|||||||
"node": ">= 8"
|
"node": ">= 8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/argparse": {
|
||||||
|
"version": "2.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/argparse/-/argparse-2.0.1.tgz",
|
||||||
|
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "Python-2.0"
|
||||||
|
},
|
||||||
"node_modules/array-flatten": {
|
"node_modules/array-flatten": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
|
||||||
@@ -2902,6 +3303,16 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/callsites": {
|
||||||
|
"version": "3.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/callsites/-/callsites-3.1.0.tgz",
|
||||||
|
"integrity": "sha512-P8BjAsXvZS+VIDUI11hHCQEv74YT67YUi5JJFNWIqL235sBmjX4+qx9Muvls5ivyNENctx46xQLQ3aTuE7ssaQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/camel-case": {
|
"node_modules/camel-case": {
|
||||||
"version": "4.1.2",
|
"version": "4.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/camel-case/-/camel-case-4.1.2.tgz",
|
||||||
@@ -2913,6 +3324,19 @@
|
|||||||
"tslib": "^2.0.3"
|
"tslib": "^2.0.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/camelcase": {
|
||||||
|
"version": "6.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/camelcase/-/camelcase-6.3.0.tgz",
|
||||||
|
"integrity": "sha512-Gmy6FhYlCY7uOElZUSbxo2UCDH8owEk996gkbrpsgGtrJLM3J7jGxl9Ic7Qwwj4ivOE5AWZWRMecDdF7hqGjFA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=10"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/caniuse-lite": {
|
"node_modules/caniuse-lite": {
|
||||||
"version": "1.0.30001749",
|
"version": "1.0.30001749",
|
||||||
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001749.tgz",
|
"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001749.tgz",
|
||||||
@@ -3143,6 +3567,33 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/cosmiconfig": {
|
||||||
|
"version": "8.3.6",
|
||||||
|
"resolved": "https://registry.npmjs.org/cosmiconfig/-/cosmiconfig-8.3.6.tgz",
|
||||||
|
"integrity": "sha512-kcZ6+W5QzcJ3P1Mt+83OUv/oHFqZHIx8DuxG6eZ5RGMERoLqp4BuGjhHLYGK+Kf5XVkQvqBSmAy/nGWN3qDgEA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"import-fresh": "^3.3.0",
|
||||||
|
"js-yaml": "^4.1.0",
|
||||||
|
"parse-json": "^5.2.0",
|
||||||
|
"path-type": "^4.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/d-fischer"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"typescript": ">=4.9.5"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"typescript": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/cross-spawn": {
|
"node_modules/cross-spawn": {
|
||||||
"version": "7.0.6",
|
"version": "7.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||||
@@ -3211,6 +3662,20 @@
|
|||||||
"url": "https://github.com/sponsors/fb55"
|
"url": "https://github.com/sponsors/fb55"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/css-tree": {
|
||||||
|
"version": "2.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.3.1.tgz",
|
||||||
|
"integrity": "sha512-6Fv1DV/TYw//QF5IzQdqsNDjx/wc8TrMBZsqjL9eW01tWb7R7k/mq+/VXfJCl7SoD5emsJop9cOByJZfs8hYIw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"mdn-data": "2.0.30",
|
||||||
|
"source-map-js": "^1.0.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^10 || ^12.20.0 || ^14.13.0 || >=15.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/css-what": {
|
"node_modules/css-what": {
|
||||||
"version": "6.2.2",
|
"version": "6.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/css-what/-/css-what-6.2.2.tgz",
|
||||||
@@ -3237,6 +3702,42 @@
|
|||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/csso": {
|
||||||
|
"version": "5.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/csso/-/csso-5.0.5.tgz",
|
||||||
|
"integrity": "sha512-0LrrStPOdJj+SPCCrGhzryycLjwcgUSHBtxNA8aIDxf0GLsRh1cKYhB00Gd1lDOS4yGH69+SNn13+TWbVHETFQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"css-tree": "~2.2.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^10 || ^12.20.0 || ^14.13.0 || >=15.0.0",
|
||||||
|
"npm": ">=7.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/csso/node_modules/css-tree": {
|
||||||
|
"version": "2.2.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/css-tree/-/css-tree-2.2.1.tgz",
|
||||||
|
"integrity": "sha512-OA0mILzGc1kCOCSJerOeqDxDQ4HOh+G8NbOJFOTgOCzpw7fCBubk0fEyxp8AgOL/jvLgYA/uV0cMbe43ElF1JA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"mdn-data": "2.0.28",
|
||||||
|
"source-map-js": "^1.0.1"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": "^10 || ^12.20.0 || ^14.13.0 || >=15.0.0",
|
||||||
|
"npm": ">=7.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/csso/node_modules/mdn-data": {
|
||||||
|
"version": "2.0.28",
|
||||||
|
"resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.28.tgz",
|
||||||
|
"integrity": "sha512-aylIc7Z9y4yzHYAJNuESG3hfhC+0Ibp/MAMiaOZgNv4pmEdFyfZhhhny4MNiAfWdBQ1RQ2mfDWmM1x8SvGyp8g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "CC0-1.0"
|
||||||
|
},
|
||||||
"node_modules/debug": {
|
"node_modules/debug": {
|
||||||
"version": "2.6.9",
|
"version": "2.6.9",
|
||||||
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
"resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz",
|
||||||
@@ -3247,6 +3748,16 @@
|
|||||||
"ms": "2.0.0"
|
"ms": "2.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/deepmerge": {
|
||||||
|
"version": "4.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/deepmerge/-/deepmerge-4.3.1.tgz",
|
||||||
|
"integrity": "sha512-3sUqbMEc77XqpdNO7FRyRog+eW3ph+GYCbj+rK+uYyRMuwsVy0rMiVtPn+QJlKFvWP/1PYpapqYn0Me2knFn+A==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.10.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/default-browser": {
|
"node_modules/default-browser": {
|
||||||
"version": "5.2.1",
|
"version": "5.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/default-browser/-/default-browser-5.2.1.tgz",
|
"resolved": "https://registry.npmjs.org/default-browser/-/default-browser-5.2.1.tgz",
|
||||||
@@ -3508,6 +4019,16 @@
|
|||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/error-ex": {
|
||||||
|
"version": "1.3.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.4.tgz",
|
||||||
|
"integrity": "sha512-sqQamAnR14VgCr1A618A3sGrygcpK+HEbenA/HiEAkkUwcZIIB/tgWqHFxWgOyDh4nB4JCRimh79dR5Ywc9MDQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"is-arrayish": "^0.2.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/es-define-property": {
|
"node_modules/es-define-property": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
|
||||||
@@ -4420,6 +4941,33 @@
|
|||||||
"postcss": "^8.1.0"
|
"postcss": "^8.1.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/import-fresh": {
|
||||||
|
"version": "3.3.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/import-fresh/-/import-fresh-3.3.1.tgz",
|
||||||
|
"integrity": "sha512-TR3KfrTZTYLPB6jUjfx6MF9WcWrHL9su5TObK4ZkYgBdWKPOFoSoQIdEuTuR82pmtxH2spWG9h6etwfr1pLBqQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"parent-module": "^1.0.0",
|
||||||
|
"resolve-from": "^4.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/import-fresh/node_modules/resolve-from": {
|
||||||
|
"version": "4.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-4.0.0.tgz",
|
||||||
|
"integrity": "sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/import-local": {
|
"node_modules/import-local": {
|
||||||
"version": "3.2.0",
|
"version": "3.2.0",
|
||||||
"resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/import-local/-/import-local-3.2.0.tgz",
|
||||||
@@ -4467,6 +5015,13 @@
|
|||||||
"node": ">= 10"
|
"node": ">= 10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/is-arrayish": {
|
||||||
|
"version": "0.2.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/is-arrayish/-/is-arrayish-0.2.1.tgz",
|
||||||
|
"integrity": "sha512-zz06S8t0ozoDXMG+ube26zeCTNXcKIPJZJi8hBrF4idCLms4CG9QtK7qBl1boi5ODzFpjswb5JPmHCbMpjaYzg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/is-binary-path": {
|
"node_modules/is-binary-path": {
|
||||||
"version": "2.1.0",
|
"version": "2.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",
|
||||||
@@ -4664,6 +5219,19 @@
|
|||||||
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
|
"integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/js-yaml": {
|
||||||
|
"version": "4.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz",
|
||||||
|
"integrity": "sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"argparse": "^2.0.1"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"js-yaml": "bin/js-yaml.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/jsesc": {
|
"node_modules/jsesc": {
|
||||||
"version": "3.1.0",
|
"version": "3.1.0",
|
||||||
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz",
|
"resolved": "https://registry.npmjs.org/jsesc/-/jsesc-3.1.0.tgz",
|
||||||
@@ -4725,6 +5293,13 @@
|
|||||||
"shell-quote": "^1.8.3"
|
"shell-quote": "^1.8.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/lines-and-columns": {
|
||||||
|
"version": "1.2.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz",
|
||||||
|
"integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/loader-runner": {
|
"node_modules/loader-runner": {
|
||||||
"version": "4.3.1",
|
"version": "4.3.1",
|
||||||
"resolved": "https://registry.npmjs.org/loader-runner/-/loader-runner-4.3.1.tgz",
|
"resolved": "https://registry.npmjs.org/loader-runner/-/loader-runner-4.3.1.tgz",
|
||||||
@@ -4807,6 +5382,13 @@
|
|||||||
"node": ">= 0.4"
|
"node": ">= 0.4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/mdn-data": {
|
||||||
|
"version": "2.0.30",
|
||||||
|
"resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.30.tgz",
|
||||||
|
"integrity": "sha512-GaqWWShW4kv/G9IEucWScBx9G1/vsFZZJUO+tD26M8J8z3Kw5RDQjaoZe03YAClgeS/SWPOcb4nkFBTEi5DUEA==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "CC0-1.0"
|
||||||
|
},
|
||||||
"node_modules/media-typer": {
|
"node_modules/media-typer": {
|
||||||
"version": "0.3.0",
|
"version": "0.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz",
|
||||||
@@ -5156,6 +5738,38 @@
|
|||||||
"tslib": "^2.0.3"
|
"tslib": "^2.0.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/parent-module": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/parent-module/-/parent-module-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-GQ2EWRpQV8/o+Aw8YqtfZZPfNRWZYkbidE9k5rpl/hC3vtHHBfGm2Ifi6qWV+coDGkrUKZAxE3Lot5kcsRlh+g==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"callsites": "^3.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=6"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/parse-json": {
|
||||||
|
"version": "5.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/parse-json/-/parse-json-5.2.0.tgz",
|
||||||
|
"integrity": "sha512-ayCKvm/phCGxOkYRSCM82iDwct8/EonSEgCSxWxD7ve6jHggsFl4fZVQBPRNgQoKiuV/odhFrGzQXZwbifC8Rg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@babel/code-frame": "^7.0.0",
|
||||||
|
"error-ex": "^1.3.1",
|
||||||
|
"json-parse-even-better-errors": "^2.3.0",
|
||||||
|
"lines-and-columns": "^1.1.6"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/sindresorhus"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/parseurl": {
|
"node_modules/parseurl": {
|
||||||
"version": "1.3.3",
|
"version": "1.3.3",
|
||||||
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
|
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz",
|
||||||
@@ -5211,6 +5825,16 @@
|
|||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
|
"node_modules/path-type": {
|
||||||
|
"version": "4.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/path-type/-/path-type-4.0.0.tgz",
|
||||||
|
"integrity": "sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/picocolors": {
|
"node_modules/picocolors": {
|
||||||
"version": "1.1.1",
|
"version": "1.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz",
|
||||||
@@ -6090,6 +6714,17 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/snake-case": {
|
||||||
|
"version": "3.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/snake-case/-/snake-case-3.0.4.tgz",
|
||||||
|
"integrity": "sha512-LAOh4z89bGQvl9pFfNF8V146i7o7/CqFPbqzYgP+yYzDIDeS9HaNFtXABamRW+AQzEVODcvE79ljJ+8a9YSdMg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"dot-case": "^3.0.4",
|
||||||
|
"tslib": "^2.0.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/sockjs": {
|
"node_modules/sockjs": {
|
||||||
"version": "0.3.24",
|
"version": "0.3.24",
|
||||||
"resolved": "https://registry.npmjs.org/sockjs/-/sockjs-0.3.24.tgz",
|
"resolved": "https://registry.npmjs.org/sockjs/-/sockjs-0.3.24.tgz",
|
||||||
@@ -6294,6 +6929,125 @@
|
|||||||
"url": "https://github.com/sponsors/ljharb"
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/svg-parser": {
|
||||||
|
"version": "2.0.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/svg-parser/-/svg-parser-2.0.4.tgz",
|
||||||
|
"integrity": "sha512-e4hG1hRwoOdRb37cIMSgzNsxyzKfayW6VOflrwvR+/bzrkyxY/31WkbgnQpgtrNp1SdpJvpUAGTa/ZoiPNDuRQ==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/svgo": {
|
||||||
|
"version": "3.3.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/svgo/-/svgo-3.3.2.tgz",
|
||||||
|
"integrity": "sha512-OoohrmuUlBs8B8o6MB2Aevn+pRIH9zDALSR+6hhqVfa6fRwG/Qw9VUMSMW9VNg2CFc/MTIfabtdOVl9ODIJjpw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@trysound/sax": "0.2.0",
|
||||||
|
"commander": "^7.2.0",
|
||||||
|
"css-select": "^5.1.0",
|
||||||
|
"css-tree": "^2.3.1",
|
||||||
|
"css-what": "^6.1.0",
|
||||||
|
"csso": "^5.0.5",
|
||||||
|
"picocolors": "^1.0.0"
|
||||||
|
},
|
||||||
|
"bin": {
|
||||||
|
"svgo": "bin/svgo"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.0.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"type": "opencollective",
|
||||||
|
"url": "https://opencollective.com/svgo"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/svgo/node_modules/commander": {
|
||||||
|
"version": "7.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/commander/-/commander-7.2.0.tgz",
|
||||||
|
"integrity": "sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 10"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/svgo/node_modules/css-select": {
|
||||||
|
"version": "5.2.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/css-select/-/css-select-5.2.2.tgz",
|
||||||
|
"integrity": "sha512-TizTzUddG/xYLA3NXodFM0fSbNizXjOKhqiQQwvhlspadZokn1KDy0NZFS0wuEubIYAV5/c1/lAr0TaaFXEXzw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"boolbase": "^1.0.0",
|
||||||
|
"css-what": "^6.1.0",
|
||||||
|
"domhandler": "^5.0.2",
|
||||||
|
"domutils": "^3.0.1",
|
||||||
|
"nth-check": "^2.0.1"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/fb55"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/svgo/node_modules/dom-serializer": {
|
||||||
|
"version": "2.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/dom-serializer/-/dom-serializer-2.0.0.tgz",
|
||||||
|
"integrity": "sha512-wIkAryiqt/nV5EQKqQpo3SToSOV9J0DnbJqwK7Wv/Trc92zIAYZ4FlMu+JPFW1DfGFt81ZTCGgDEabffXeLyJg==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"domelementtype": "^2.3.0",
|
||||||
|
"domhandler": "^5.0.2",
|
||||||
|
"entities": "^4.2.0"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/cheeriojs/dom-serializer?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/svgo/node_modules/domhandler": {
|
||||||
|
"version": "5.0.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/domhandler/-/domhandler-5.0.3.tgz",
|
||||||
|
"integrity": "sha512-cgwlv/1iFQiFnU96XXgROh8xTeetsnJiDsTc7TYCLFd9+/WNkIqPTxiM/8pSd8VIrhXGTf1Ny1q1hquVqDJB5w==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"domelementtype": "^2.3.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/fb55/domhandler?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/svgo/node_modules/domutils": {
|
||||||
|
"version": "3.2.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/domutils/-/domutils-3.2.2.tgz",
|
||||||
|
"integrity": "sha512-6kZKyUajlDuqlHKVX1w7gyslj9MPIXzIFiz/rGu35uC1wMi+kMhQwGhl4lt9unC9Vb9INnY9Z3/ZA3+FhASLaw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"dependencies": {
|
||||||
|
"dom-serializer": "^2.0.0",
|
||||||
|
"domelementtype": "^2.3.0",
|
||||||
|
"domhandler": "^5.0.3"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/fb55/domutils?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/svgo/node_modules/entities": {
|
||||||
|
"version": "4.5.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/entities/-/entities-4.5.0.tgz",
|
||||||
|
"integrity": "sha512-V0hjH4dGPh9Ao5p0MoRY6BVqtwCjhz6vI5LT8AJ55H+4g9/4vbHx1I54fS0XuclLhDHArPQCiMjDxjaL8fPxhw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "BSD-2-Clause",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.12"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/fb55/entities?sponsor=1"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/tapable": {
|
"node_modules/tapable": {
|
||||||
"version": "2.3.0",
|
"version": "2.3.0",
|
||||||
"resolved": "https://registry.npmjs.org/tapable/-/tapable-2.3.0.tgz",
|
"resolved": "https://registry.npmjs.org/tapable/-/tapable-2.3.0.tgz",
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
{
|
{
|
||||||
"name": "paperless-neo-frontend",
|
"name": "papercrate-frontend",
|
||||||
"version": "0.1.0",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "Rudimentary Webpack SPA for Paperless-NEO Milestone 1",
|
"description": "Rudimentary Webpack SPA for Papercrate Milestone 1",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "webpack serve --mode development --open",
|
"dev": "webpack serve --mode development --open",
|
||||||
"build": "webpack --mode production",
|
"build": "webpack --mode production",
|
||||||
"lint": "echo \"No linting configured\""
|
"lint": "echo \"No linting configured\""
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@tabler/icons-react": "3.11.0",
|
||||||
"axios": "1.7.7",
|
"axios": "1.7.7",
|
||||||
"react": "18.3.1",
|
"react": "18.3.1",
|
||||||
"react-dom": "18.3.1",
|
"react-dom": "18.3.1",
|
||||||
@@ -25,6 +26,7 @@
|
|||||||
"style-loader": "4.0.0",
|
"style-loader": "4.0.0",
|
||||||
"webpack": "5.95.0",
|
"webpack": "5.95.0",
|
||||||
"webpack-cli": "5.1.4",
|
"webpack-cli": "5.1.4",
|
||||||
"webpack-dev-server": "5.1.0"
|
"webpack-dev-server": "5.1.0",
|
||||||
|
"@svgr/webpack": "8.1.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
window.__PAPERCRATE_API_BASE_URL = window.__PAPERCRATE_API_BASE_URL || '';
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import React from 'react';
|
||||||
|
|
||||||
|
export const AppShellContext = React.createContext(null);
|
||||||
|
|
||||||
|
export const useAppShell = () => {
|
||||||
|
const context = React.useContext(AppShellContext);
|
||||||
|
if (!context) {
|
||||||
|
throw new Error('AppShellContext not found. Ensure routes are nested under AppLayout.');
|
||||||
|
}
|
||||||
|
return context;
|
||||||
|
};
|
||||||
|
|
||||||
@@ -0,0 +1,419 @@
|
|||||||
|
export const getAssetFromGroup = (assets, assetType) => {
|
||||||
|
if (!assetType || !assets) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(assets)) {
|
||||||
|
return assets.find((entry) => entry?.asset_type === assetType) || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return assets?.[assetType] || null;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const getAssetFromVersion = (currentVersion, assetType) => {
|
||||||
|
if (!currentVersion) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return getAssetFromGroup(currentVersion.assets, assetType);
|
||||||
|
};
|
||||||
|
|
||||||
|
const normalizeAssetObjects = (objects) => {
|
||||||
|
if (!Array.isArray(objects)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
return objects
|
||||||
|
.filter((entry) => Number.isInteger(entry?.ordinal))
|
||||||
|
.slice()
|
||||||
|
.sort((a, b) => a.ordinal - b.ordinal);
|
||||||
|
};
|
||||||
|
|
||||||
|
const mergeAssetObjects = (existingObjects, incomingObjects) => {
|
||||||
|
const merged = new Map();
|
||||||
|
|
||||||
|
normalizeAssetObjects(existingObjects).forEach((entry) => {
|
||||||
|
merged.set(entry.ordinal, { ...entry });
|
||||||
|
});
|
||||||
|
|
||||||
|
normalizeAssetObjects(incomingObjects).forEach((entry) => {
|
||||||
|
const current = merged.get(entry.ordinal) || {};
|
||||||
|
merged.set(entry.ordinal, { ...current, ...entry });
|
||||||
|
});
|
||||||
|
|
||||||
|
return [...merged.entries()]
|
||||||
|
.sort((a, b) => a[0] - b[0])
|
||||||
|
.map(([, value]) => value);
|
||||||
|
};
|
||||||
|
|
||||||
|
export class AssetView {
|
||||||
|
constructor(asset) {
|
||||||
|
this.asset = asset || null;
|
||||||
|
this._objectsRef = null;
|
||||||
|
this._sortedObjects = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
getCardinality() {
|
||||||
|
if (!this.asset) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
const reported = Number(this.asset.cardinality);
|
||||||
|
if (Number.isFinite(reported) && reported > 0) {
|
||||||
|
return reported;
|
||||||
|
}
|
||||||
|
|
||||||
|
const objectsCount = this.getObjects().length;
|
||||||
|
if (objectsCount > 0) {
|
||||||
|
return objectsCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.asset.metadata ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
getObjects() {
|
||||||
|
if (!this.asset || !Array.isArray(this.asset.objects) || this.asset.objects.length === 0) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this._objectsRef === this.asset.objects) {
|
||||||
|
return this._sortedObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
this._objectsRef = this.asset.objects;
|
||||||
|
this._sortedObjects = normalizeAssetObjects(this.asset.objects);
|
||||||
|
return this._sortedObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
getObject(ordinal = 1) {
|
||||||
|
const fromObjects = this.getObjects().find((entry) => entry.ordinal === ordinal);
|
||||||
|
if (fromObjects) {
|
||||||
|
return fromObjects;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ordinal === 1 && this.asset) {
|
||||||
|
if (this.asset.url || this.asset.metadata) {
|
||||||
|
return {
|
||||||
|
ordinal: 1,
|
||||||
|
url: this.asset.url || null,
|
||||||
|
metadata: this.asset.metadata || null,
|
||||||
|
expires_at: this.asset.expiresAt ?? null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPrimaryObject() {
|
||||||
|
return this.getObject(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
getPrimaryMetadata() {
|
||||||
|
return this.getPrimaryObject()?.metadata || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
getPrimaryUrl() {
|
||||||
|
return this.getPrimaryObject()?.url || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
hasObject(ordinal) {
|
||||||
|
return Boolean(this.getObject(ordinal));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createAssetView = (asset) => new AssetView(asset);
|
||||||
|
|
||||||
|
export const resolveDocumentAssetUrl = (
|
||||||
|
doc,
|
||||||
|
type,
|
||||||
|
{ ensureAssetUrl, getAsset, ensureOptions, objectOrdinal = 1 } = {},
|
||||||
|
) => {
|
||||||
|
if (!doc || !type) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const asset = typeof getAsset === 'function' ? getAsset(doc, type) : null;
|
||||||
|
if (!asset) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const view = createAssetView(asset);
|
||||||
|
const object = view.getObject(objectOrdinal);
|
||||||
|
const url = object?.url || (objectOrdinal === 1 ? view.getPrimaryUrl() : null);
|
||||||
|
const expiresAt = typeof object?.expires_at === 'number'
|
||||||
|
? object.expires_at
|
||||||
|
: objectOrdinal === 1 && typeof asset.expiresAt === 'number'
|
||||||
|
? asset.expiresAt
|
||||||
|
: null;
|
||||||
|
const now = Date.now();
|
||||||
|
if (url && (!expiresAt || expiresAt > now)) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
if (doc.id && asset.id && typeof ensureAssetUrl === 'function') {
|
||||||
|
const force = Boolean(url && expiresAt && expiresAt <= now);
|
||||||
|
const options = {
|
||||||
|
force,
|
||||||
|
start: objectOrdinal,
|
||||||
|
limit: 1,
|
||||||
|
...(ensureOptions || {}),
|
||||||
|
};
|
||||||
|
if (!options.start) {
|
||||||
|
options.start = objectOrdinal;
|
||||||
|
}
|
||||||
|
if (!options.limit) {
|
||||||
|
options.limit = 1;
|
||||||
|
}
|
||||||
|
ensureAssetUrl(doc.id, asset, options).catch(() => {});
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AssetManager {
|
||||||
|
constructor({ api, assetPresignTtlMs }) {
|
||||||
|
this.api = api;
|
||||||
|
this.assetPresignTtlMs = assetPresignTtlMs;
|
||||||
|
this.assetCache = new Map();
|
||||||
|
this.assetInflight = new Map();
|
||||||
|
}
|
||||||
|
|
||||||
|
setApi(api) {
|
||||||
|
this.api = api;
|
||||||
|
}
|
||||||
|
|
||||||
|
rememberAsset(entry) {
|
||||||
|
if (entry?.id) {
|
||||||
|
this.assetCache.set(entry.id, entry);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hydrateAsset(asset) {
|
||||||
|
if (!asset || !asset.id) {
|
||||||
|
return asset;
|
||||||
|
}
|
||||||
|
const cached = this.assetCache.get(asset.id);
|
||||||
|
if (!cached) {
|
||||||
|
const normalized = mergeAssetObjects(null, asset.objects);
|
||||||
|
if (normalized.length) {
|
||||||
|
return { ...asset, objects: normalized };
|
||||||
|
}
|
||||||
|
return asset;
|
||||||
|
}
|
||||||
|
const merged = { ...cached, ...asset };
|
||||||
|
if (cached.url && !asset.url) {
|
||||||
|
merged.url = cached.url;
|
||||||
|
}
|
||||||
|
if (cached.expiresAt) {
|
||||||
|
const cachedExpires = Number(cached.expiresAt) || null;
|
||||||
|
const assetExpires = Number(asset.expiresAt) || null;
|
||||||
|
if (!assetExpires || (cachedExpires && cachedExpires > assetExpires)) {
|
||||||
|
merged.expiresAt = cachedExpires;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const mergedObjects = mergeAssetObjects(cached.objects, asset.objects);
|
||||||
|
if (mergedObjects.length) {
|
||||||
|
merged.objects = mergedObjects;
|
||||||
|
}
|
||||||
|
return merged;
|
||||||
|
}
|
||||||
|
|
||||||
|
hydrateDocument(document) {
|
||||||
|
if (!document) {
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentVersion = document.current_version || null;
|
||||||
|
if (!currentVersion) {
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
let changed = false;
|
||||||
|
let nextAssets = currentVersion.assets;
|
||||||
|
|
||||||
|
if (nextAssets && !Array.isArray(nextAssets)) {
|
||||||
|
const hydrated = {};
|
||||||
|
Object.keys(nextAssets).forEach((key) => {
|
||||||
|
hydrated[key] = this.hydrateAsset(nextAssets[key]);
|
||||||
|
if (hydrated[key] !== nextAssets[key]) {
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (changed) {
|
||||||
|
nextAssets = { ...nextAssets, ...hydrated };
|
||||||
|
}
|
||||||
|
} else if (Array.isArray(nextAssets)) {
|
||||||
|
const hydratedList = nextAssets.map((item) => this.hydrateAsset(item));
|
||||||
|
if (
|
||||||
|
hydratedList.length !== nextAssets.length ||
|
||||||
|
hydratedList.some((item, index) => item !== nextAssets[index])
|
||||||
|
) {
|
||||||
|
changed = true;
|
||||||
|
nextAssets = hydratedList;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!changed) {
|
||||||
|
return document;
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextCurrentVersion = { ...currentVersion, assets: nextAssets };
|
||||||
|
return { ...document, current_version: nextCurrentVersion };
|
||||||
|
}
|
||||||
|
|
||||||
|
hydrateDocuments(documents) {
|
||||||
|
if (!Array.isArray(documents)) {
|
||||||
|
return documents;
|
||||||
|
}
|
||||||
|
return documents.map((doc) => this.hydrateDocument(doc));
|
||||||
|
}
|
||||||
|
|
||||||
|
hydrateDetail(detail) {
|
||||||
|
if (!detail) {
|
||||||
|
return detail;
|
||||||
|
}
|
||||||
|
let changed = false;
|
||||||
|
const next = { ...detail };
|
||||||
|
|
||||||
|
if (detail.document) {
|
||||||
|
const hydratedDocument = this.hydrateDocument(detail.document);
|
||||||
|
if (hydratedDocument !== detail.document) {
|
||||||
|
next.document = hydratedDocument;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Array.isArray(detail.assets)) {
|
||||||
|
const hydratedAssets = detail.assets.map((item) => this.hydrateAsset(item));
|
||||||
|
if (
|
||||||
|
hydratedAssets.length !== detail.assets.length ||
|
||||||
|
hydratedAssets.some((item, index) => item !== detail.assets[index])
|
||||||
|
) {
|
||||||
|
next.assets = hydratedAssets;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return changed ? next : detail;
|
||||||
|
}
|
||||||
|
|
||||||
|
hydrateFolderContents(contents) {
|
||||||
|
if (!contents) {
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
const next = { ...contents };
|
||||||
|
if (Array.isArray(contents.documents)) {
|
||||||
|
next.documents = this.hydrateDocuments(contents.documents);
|
||||||
|
}
|
||||||
|
if (contents.document) {
|
||||||
|
next.document = this.hydrateDocument(contents.document);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
}
|
||||||
|
|
||||||
|
ensureAsset(documentId, asset, { force = false, start = null, limit = null } = {}) {
|
||||||
|
if (!documentId || !asset?.id) {
|
||||||
|
return Promise.resolve(asset || null);
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestedStart = Number.isInteger(start) && start > 0 ? start : 1;
|
||||||
|
const requestedLimit = Number.isInteger(limit) && limit > 0 ? limit : 1;
|
||||||
|
const requestedEnd = requestedStart + requestedLimit - 1;
|
||||||
|
|
||||||
|
const baseAsset = this.assetCache.get(asset.id) || asset;
|
||||||
|
const view = createAssetView(baseAsset);
|
||||||
|
const assetExpiresAt = typeof baseAsset.expiresAt === 'number' ? baseAsset.expiresAt : null;
|
||||||
|
const now = Date.now();
|
||||||
|
|
||||||
|
const isOrdinalSatisfied = (ordinal) => {
|
||||||
|
const object = view.getObject(ordinal);
|
||||||
|
if (!object) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!object.url) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (typeof object.expires_at === 'number') {
|
||||||
|
return object.expires_at > now;
|
||||||
|
}
|
||||||
|
if (ordinal === 1 && baseAsset.url && (!assetExpiresAt || assetExpiresAt > now)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
let needsFetch = force;
|
||||||
|
if (!needsFetch) {
|
||||||
|
for (let ordinal = requestedStart; ordinal <= requestedEnd; ordinal += 1) {
|
||||||
|
if (!isOrdinalSatisfied(ordinal)) {
|
||||||
|
needsFetch = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!needsFetch) {
|
||||||
|
this.rememberAsset(baseAsset);
|
||||||
|
return Promise.resolve(baseAsset);
|
||||||
|
}
|
||||||
|
|
||||||
|
const inflightKey = `${documentId}:${asset.id}:${start ?? 'd'}:${limit ?? 'd'}`;
|
||||||
|
if (!force && this.assetInflight.has(inflightKey)) {
|
||||||
|
return this.assetInflight.get(inflightKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.api) {
|
||||||
|
return Promise.reject(new Error('AssetManager API client is not configured.'));
|
||||||
|
}
|
||||||
|
|
||||||
|
const params = {};
|
||||||
|
if (Number.isInteger(start) && start > 0) {
|
||||||
|
params.start = start;
|
||||||
|
}
|
||||||
|
if (Number.isInteger(limit) && limit > 0) {
|
||||||
|
params.limit = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
const requestConfig = Object.keys(params).length ? { params } : undefined;
|
||||||
|
|
||||||
|
const request = this.api
|
||||||
|
.get(`/assets/${asset.id}`, requestConfig)
|
||||||
|
.then(({ data }) => {
|
||||||
|
const incomingObjects = Array.isArray(data.objects) ? data.objects : [];
|
||||||
|
const cachedEntry = this.assetCache.get(asset.id) || baseAsset;
|
||||||
|
const mergedObjects = mergeAssetObjects(cachedEntry?.objects, incomingObjects);
|
||||||
|
const combined = { ...cachedEntry, ...asset, ...data, objects: mergedObjects };
|
||||||
|
const view = createAssetView(combined);
|
||||||
|
const primaryObject = view.getPrimaryObject();
|
||||||
|
const expiresAt = typeof primaryObject?.expires_at === 'number'
|
||||||
|
? primaryObject.expires_at
|
||||||
|
: Date.now() + this.assetPresignTtlMs;
|
||||||
|
const cardinality = (() => {
|
||||||
|
const reported = Number(data.cardinality ?? asset.cardinality ?? cachedEntry?.cardinality);
|
||||||
|
const objectsCount = mergedObjects.length;
|
||||||
|
if (Number.isFinite(reported) && reported > 0) {
|
||||||
|
return Math.max(reported, objectsCount) || null;
|
||||||
|
}
|
||||||
|
return objectsCount || null;
|
||||||
|
})();
|
||||||
|
|
||||||
|
const entry = {
|
||||||
|
...combined,
|
||||||
|
cardinality,
|
||||||
|
url: view.getPrimaryUrl(),
|
||||||
|
expiresAt,
|
||||||
|
};
|
||||||
|
|
||||||
|
this.rememberAsset(entry);
|
||||||
|
return entry;
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
this.assetInflight.delete(inflightKey);
|
||||||
|
});
|
||||||
|
|
||||||
|
this.assetInflight.set(inflightKey, request);
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
|
||||||
|
reset() {
|
||||||
|
this.assetCache.clear();
|
||||||
|
this.assetInflight.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default AssetManager;
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<svg viewBox="0 0 128 128" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||||
|
<linearGradient id="a" gradientTransform="matrix(0.45451 0 0 0.455522 -1210.292114 616.172607)" gradientUnits="userSpaceOnUse" x1="2689.251953" x2="2918.069824" y1="-1106.802979" y2="-1106.802979">
|
||||||
|
<stop offset="0" stop-color="#62a0ea"/>
|
||||||
|
<stop offset="0.0576991" stop-color="#afd4ff"/>
|
||||||
|
<stop offset="0.122204" stop-color="#62a0ea"/>
|
||||||
|
<stop offset="0.873306" stop-color="#62a0ea"/>
|
||||||
|
<stop offset="0.955997" stop-color="#c0d5ea"/>
|
||||||
|
<stop offset="1" stop-color="#62a0ea"/>
|
||||||
|
</linearGradient>
|
||||||
|
<path d="m 21.976562 12 c -5.527343 0 -9.976562 4.460938 -9.976562 10 v 86.03125 c 0 5.542969 4.449219 10 9.976562 10 h 84.042969 c 5.53125 0 9.980469 -4.457031 9.980469 -10 v -72.085938 c 0 -6.628906 -5.359375 -12 -11.972656 -12 h -46.027344 c -2.453125 0 -4.695312 -1.386718 -5.796875 -3.582031 l -1.503906 -2.992187 c -1.65625 -3.292969 -5.019531 -5.371094 -8.699219 -5.371094 z m 0 0" fill="#438de6"/>
|
||||||
|
<path d="m 65.976562 36 c -2.746093 0 -5.226562 1.101562 -7.027343 2.890625 c -2.273438 2.253906 -5.382813 5.109375 -8.632813 5.109375 h -28.339844 c -5.527343 0 -9.976562 4.460938 -9.976562 10 v 54.03125 c 0 5.542969 4.449219 10 9.976562 10 h 84.042969 c 5.53125 0 9.980469 -4.457031 9.980469 -10 v -62.03125 c 0 -5.539062 -4.449219 -10 -9.980469 -10 z m 0 0" fill="url(#a)"/>
|
||||||
|
<path d="m 65.976562 32 c -2.746093 0 -5.226562 1.101562 -7.027343 2.890625 c -2.273438 2.253906 -5.382813 5.109375 -8.632813 5.109375 h -28.339844 c -5.527343 0 -9.976562 4.460938 -9.976562 10 v 55.976562 c 0 5.539063 4.449219 10 9.976562 10 h 84.042969 c 5.53125 0 9.980469 -4.460937 9.980469 -10 v -63.976562 c 0 -5.539062 -4.449219 -10 -9.980469 -10 z m 0 0" fill="#a4caee"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 1.8 KiB |
@@ -0,0 +1,19 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="100%" height="100%" viewBox="0 0 128 128" xml:space="preserve" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
|
||||||
|
<defs>
|
||||||
|
<filter id="cornerShadow" x="-20%" y="-20%" width="200%" height="200%">
|
||||||
|
<feDropShadow dx="-4" dy="4" stdDeviation="8" flood-opacity="0.24"/>
|
||||||
|
</filter>
|
||||||
|
<linearGradient id="cornerGradient" gradientUnits="userSpaceOnUse" x1="32" y1="0" x2="0" y2="64">
|
||||||
|
<stop offset="0%" stop-color="#ffffff"/>
|
||||||
|
<stop offset="100%" stop-color="#f3f3f3"/>
|
||||||
|
</linearGradient>
|
||||||
|
</defs>
|
||||||
|
<g filter="url(#cornerShadow)">
|
||||||
|
<g transform="matrix(-1.81626,-1.81626,0.460023,-0.460023,180.663,203.387)">
|
||||||
|
<path d="M70.488,10.11L89.954,86.966L51.022,86.966L70.488,10.11Z" fill="url(#cornerGradient)"/>
|
||||||
|
</g>
|
||||||
|
<g transform="matrix(1.81626,1.81626,-0.460023,0.460023,4.6259,-132.676)">
|
||||||
|
<path d="M70.488,10.11L89.954,86.966L51.022,86.966L70.488,10.11Z" fill="#fdfdfd"/>
|
||||||
|
</g>
|
||||||
|
</g>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 1.1 KiB |
@@ -0,0 +1,3 @@
|
|||||||
|
export const CORRESPONDENT_ROLES = ['sender', 'receiver', 'other'];
|
||||||
|
|
||||||
|
export default CORRESPONDENT_ROLES;
|
||||||
@@ -0,0 +1,240 @@
|
|||||||
|
import React, { useCallback, useState } from 'react';
|
||||||
|
|
||||||
|
function CorrespondentsPanel({
|
||||||
|
correspondents = [],
|
||||||
|
onRefresh,
|
||||||
|
onCreate,
|
||||||
|
onUpdate,
|
||||||
|
onDelete,
|
||||||
|
onNotify,
|
||||||
|
}) {
|
||||||
|
const [editingId, setEditingId] = useState(null);
|
||||||
|
const [draftName, setDraftName] = useState('');
|
||||||
|
const [createName, setCreateName] = useState('');
|
||||||
|
const [saving, setSaving] = useState(false);
|
||||||
|
const [creating, setCreating] = useState(false);
|
||||||
|
const [deletingId, setDeletingId] = useState(null);
|
||||||
|
|
||||||
|
const startEdit = useCallback((correspondent) => {
|
||||||
|
setEditingId(correspondent.id);
|
||||||
|
setDraftName(correspondent.name || '');
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const cancelEdit = useCallback(() => {
|
||||||
|
setEditingId(null);
|
||||||
|
setDraftName('');
|
||||||
|
setSaving(false);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const handleSave = useCallback(async () => {
|
||||||
|
if (!editingId) return;
|
||||||
|
const trimmed = draftName.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
onNotify?.('Correspondent name cannot be empty.', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
setSaving(true);
|
||||||
|
try {
|
||||||
|
await onUpdate(editingId, { name: trimmed });
|
||||||
|
cancelEdit();
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {}
|
||||||
|
}, [editingId, draftName, onUpdate, cancelEdit, onNotify]);
|
||||||
|
|
||||||
|
const handleDelete = useCallback(
|
||||||
|
async (correspondent) => {
|
||||||
|
if (!correspondent?.id) return;
|
||||||
|
setDeletingId(correspondent.id);
|
||||||
|
try {
|
||||||
|
await onDelete(correspondent.id);
|
||||||
|
if (editingId === correspondent.id) {
|
||||||
|
cancelEdit();
|
||||||
|
}
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
setDeletingId(null);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[onDelete, editingId, cancelEdit],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleCreate = useCallback(
|
||||||
|
async (event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
const trimmed = createName.trim();
|
||||||
|
if (!trimmed) {
|
||||||
|
onNotify?.('Correspondent name cannot be empty.', 'error');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
setCreating(true);
|
||||||
|
try {
|
||||||
|
await onCreate({ name: trimmed });
|
||||||
|
setCreateName('');
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
} finally {
|
||||||
|
setCreating(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[createName, onCreate, onNotify],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleKeyDown = useCallback(
|
||||||
|
(event) => {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
event.preventDefault();
|
||||||
|
handleSave();
|
||||||
|
} else if (event.key === 'Escape') {
|
||||||
|
event.preventDefault();
|
||||||
|
cancelEdit();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[handleSave, cancelEdit],
|
||||||
|
);
|
||||||
|
|
||||||
|
const renderUsage = useCallback((usage) => {
|
||||||
|
if (!usage) {
|
||||||
|
return '0';
|
||||||
|
}
|
||||||
|
const total = typeof usage.total === 'number' ? usage.total : 0;
|
||||||
|
const entries = usage.by_role ? Object.entries(usage.by_role) : [];
|
||||||
|
if (!entries.length) {
|
||||||
|
return total.toString();
|
||||||
|
}
|
||||||
|
const roleSummary = entries
|
||||||
|
.map(([role, count]) => `${role}: ${count}`)
|
||||||
|
.join(', ');
|
||||||
|
return `${total} (${roleSummary})`;
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section className="correspondents-panel column">
|
||||||
|
<div className="column-header">
|
||||||
|
<div className="column-header__titles">
|
||||||
|
<h2>Correspondents</h2>
|
||||||
|
<div className="column-subtitle">{correspondents.length} total</div>
|
||||||
|
</div>
|
||||||
|
<div className="header-actions correspondents-actions">
|
||||||
|
<form className="correspondents-actions__form" onSubmit={handleCreate}>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="New correspondent name"
|
||||||
|
value={createName}
|
||||||
|
onChange={(event) => setCreateName(event.target.value)}
|
||||||
|
disabled={creating}
|
||||||
|
/>
|
||||||
|
<button type="submit" disabled={creating || !createName.trim()}>
|
||||||
|
{creating ? 'Creating…' : 'Create'}
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
|
<button
|
||||||
|
className="secondary"
|
||||||
|
type="button"
|
||||||
|
onClick={onRefresh}
|
||||||
|
disabled={saving || creating || Boolean(deletingId)}
|
||||||
|
>
|
||||||
|
Refresh
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="column-body tags-panel__body">
|
||||||
|
{correspondents.length === 0 ? (
|
||||||
|
<div className="empty-state">No correspondents created yet.</div>
|
||||||
|
) : (
|
||||||
|
<div className="tags-table">
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">Name</th>
|
||||||
|
<th scope="col" className="numeric">
|
||||||
|
Usage
|
||||||
|
</th>
|
||||||
|
<th scope="col" className="actions">
|
||||||
|
Actions
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{correspondents.map((correspondent) => {
|
||||||
|
const isEditing = editingId === correspondent.id;
|
||||||
|
return (
|
||||||
|
<tr key={correspondent.id} className={isEditing ? 'editing' : ''}>
|
||||||
|
<td className="tags-table__label">
|
||||||
|
{isEditing ? (
|
||||||
|
<input
|
||||||
|
className="tags-table__label-input"
|
||||||
|
value={draftName}
|
||||||
|
onChange={(event) => setDraftName(event.target.value)}
|
||||||
|
onKeyDown={handleKeyDown}
|
||||||
|
disabled={saving}
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<span>{correspondent.name}</span>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
<td className="numeric">{renderUsage(correspondent.usage)}</td>
|
||||||
|
<td className="actions">
|
||||||
|
{isEditing ? (
|
||||||
|
<div className="tags-table__edit-controls">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
onClick={handleSave}
|
||||||
|
disabled={saving}
|
||||||
|
>
|
||||||
|
Save
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
onClick={cancelEdit}
|
||||||
|
disabled={saving}
|
||||||
|
>
|
||||||
|
Cancel
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<div className="tags-table__row-actions">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
onClick={() => startEdit(correspondent)}
|
||||||
|
disabled={deletingId === correspondent.id}
|
||||||
|
>
|
||||||
|
Edit
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="danger"
|
||||||
|
onClick={() => handleDelete(correspondent)}
|
||||||
|
disabled={deletingId === correspondent.id}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default CorrespondentsPanel;
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,840 @@
|
|||||||
|
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
|
||||||
|
import { getAssetFromVersion, resolveDocumentAssetUrl, createAssetView } from '../asset_manager';
|
||||||
|
import { getTagColorStyle } from '../utils/colors';
|
||||||
|
import { DownloadIcon, EditIcon, ViewListIcon, ViewGridIcon, FolderIcon } from '../ui/icons';
|
||||||
|
|
||||||
|
const TAG_MIME_TYPES = ['application/x-papercrate-tag', 'text/papercrate-tag'];
|
||||||
|
const DEFAULT_GRID_ICON_SIZE = 96;
|
||||||
|
const DEFAULT_GRID_TITLE_SIZE = '11px';
|
||||||
|
const LIST_ICON_SIZE = 48;
|
||||||
|
|
||||||
|
const getPageCount = (doc) =>
|
||||||
|
Number.isFinite(doc?.current_version?.metadata?.page_count)
|
||||||
|
? doc.current_version.metadata.page_count
|
||||||
|
: null;
|
||||||
|
|
||||||
|
const DocumentThumbnailImage = ({
|
||||||
|
document,
|
||||||
|
ensureAssetUrl,
|
||||||
|
getDocumentAsset,
|
||||||
|
alt,
|
||||||
|
maxSize = LIST_ICON_SIZE,
|
||||||
|
}) => {
|
||||||
|
const resolvedMaxSize = Math.max(1, Math.round(maxSize || 1));
|
||||||
|
const thumbnailAsset = useMemo(
|
||||||
|
() => getAssetFromVersion(document?.current_version, 'thumbnail'),
|
||||||
|
[document?.current_version],
|
||||||
|
);
|
||||||
|
const thumbnailView = useMemo(() => createAssetView(thumbnailAsset), [thumbnailAsset]);
|
||||||
|
const primaryMetadata = thumbnailView.getPrimaryMetadata() || {};
|
||||||
|
const assetWidth = Number(primaryMetadata?.width);
|
||||||
|
const assetHeight = Number(primaryMetadata?.height);
|
||||||
|
|
||||||
|
const dimensions = useMemo(() => {
|
||||||
|
if (!Number.isFinite(assetWidth) || assetWidth <= 0 || !Number.isFinite(assetHeight) || assetHeight <= 0) {
|
||||||
|
return { width: resolvedMaxSize, height: resolvedMaxSize };
|
||||||
|
}
|
||||||
|
const scale = Math.min(1, resolvedMaxSize / assetWidth, resolvedMaxSize / assetHeight);
|
||||||
|
return {
|
||||||
|
width: Math.max(1, Math.round(assetWidth * scale)),
|
||||||
|
height: Math.max(1, Math.round(assetHeight * scale)),
|
||||||
|
};
|
||||||
|
}, [assetWidth, assetHeight, resolvedMaxSize]);
|
||||||
|
|
||||||
|
const innerStyle = useMemo(
|
||||||
|
() => ({ width: `${dimensions.width}px`, height: `${dimensions.height}px` }),
|
||||||
|
[dimensions.height, dimensions.width],
|
||||||
|
);
|
||||||
|
const url = useMemo(
|
||||||
|
() =>
|
||||||
|
resolveDocumentAssetUrl(document, 'thumbnail', {
|
||||||
|
ensureAssetUrl,
|
||||||
|
getAsset: getDocumentAsset,
|
||||||
|
}),
|
||||||
|
[document, ensureAssetUrl, getDocumentAsset],
|
||||||
|
);
|
||||||
|
|
||||||
|
const pageCount = getPageCount(document);
|
||||||
|
const showMultiPageBadge = Number.isFinite(pageCount) && pageCount > 1;
|
||||||
|
const innerClasses = ['document-thumbnail-inner'];
|
||||||
|
if (showMultiPageBadge) {
|
||||||
|
innerClasses.push('document-thumbnail-inner--multipage');
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="document-thumbnail-wrapper">
|
||||||
|
<div className={innerClasses.join(' ')} style={innerStyle}>
|
||||||
|
{url ? (
|
||||||
|
<img
|
||||||
|
src={url}
|
||||||
|
alt={alt || ''}
|
||||||
|
className="document-thumbnail"
|
||||||
|
draggable={false}
|
||||||
|
onDragStart={(event) => event.preventDefault()}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
<div className="thumb-placeholder">DOC</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const DocumentsTable = ({
|
||||||
|
currentFolderName,
|
||||||
|
breadcrumbs,
|
||||||
|
onRefresh,
|
||||||
|
onShowSkeuoWorkspace = () => {},
|
||||||
|
onRequestCreateFolder,
|
||||||
|
creatingFolder = false,
|
||||||
|
subfolders,
|
||||||
|
documents,
|
||||||
|
searchResults,
|
||||||
|
isFilterActive,
|
||||||
|
onFolderSelect,
|
||||||
|
onFolderDrop,
|
||||||
|
onFolderDragOver,
|
||||||
|
onFolderDragLeave,
|
||||||
|
onFolderDragStart,
|
||||||
|
onFolderDragEnd,
|
||||||
|
draggedFolderId,
|
||||||
|
onFolderDelete,
|
||||||
|
selectedFolderIds = [],
|
||||||
|
onFolderRowClick,
|
||||||
|
onDocumentRowClick,
|
||||||
|
onDocumentOpen,
|
||||||
|
selectedDocumentIds,
|
||||||
|
focusedDocumentId,
|
||||||
|
focusedRowKey,
|
||||||
|
draggingDocumentIds = [],
|
||||||
|
onDocumentDragStart,
|
||||||
|
onDocumentDragEnd,
|
||||||
|
onDocumentDelete,
|
||||||
|
onFolderRename,
|
||||||
|
onDocumentRename,
|
||||||
|
tagLookupById,
|
||||||
|
onDocumentListFocus,
|
||||||
|
onDocumentListKeyDown,
|
||||||
|
onFocusedRowChange,
|
||||||
|
ensureAssetUrl = null,
|
||||||
|
getDocumentAsset = () => null,
|
||||||
|
getDownloadHref,
|
||||||
|
onTagClick,
|
||||||
|
isSearchLoading = false,
|
||||||
|
onDocumentTagDrop,
|
||||||
|
viewMode = 'list',
|
||||||
|
onViewModeChange,
|
||||||
|
onClearSelection,
|
||||||
|
}) => {
|
||||||
|
const showingSearchResults = searchResults !== null;
|
||||||
|
const rows = showingSearchResults ? searchResults : documents;
|
||||||
|
|
||||||
|
const selectedSet = useMemo(
|
||||||
|
() => new Set(selectedDocumentIds),
|
||||||
|
[selectedDocumentIds],
|
||||||
|
);
|
||||||
|
const selectedFolderSet = useMemo(
|
||||||
|
() => new Set(selectedFolderIds || []),
|
||||||
|
[selectedFolderIds],
|
||||||
|
);
|
||||||
|
const draggingSet = useMemo(
|
||||||
|
() => new Set(draggingDocumentIds || []),
|
||||||
|
[draggingDocumentIds],
|
||||||
|
);
|
||||||
|
const scrollRef = useRef(null);
|
||||||
|
const isGridView = viewMode === 'grid';
|
||||||
|
const gridIconSize = DEFAULT_GRID_ICON_SIZE;
|
||||||
|
const handleSetViewMode = useCallback(
|
||||||
|
(nextMode) => {
|
||||||
|
if (!onViewModeChange) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onViewModeChange(nextMode);
|
||||||
|
if (scrollRef.current) {
|
||||||
|
scrollRef.current.scrollTop = 0;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[onViewModeChange],
|
||||||
|
);
|
||||||
|
const isTagDragEvent = useCallback((event) => {
|
||||||
|
const types = Array.from(event.dataTransfer?.types || []);
|
||||||
|
return TAG_MIME_TYPES.some((type) => types.includes(type));
|
||||||
|
}, []);
|
||||||
|
const ensureFocusedRowVisible = useCallback(() => {
|
||||||
|
if (!focusedRowKey) return;
|
||||||
|
const container = scrollRef.current;
|
||||||
|
if (!container) return;
|
||||||
|
let selector = null;
|
||||||
|
if (focusedRowKey.startsWith('document:')) {
|
||||||
|
selector = `#document-row-${focusedRowKey.slice('document:'.length)}`;
|
||||||
|
} else if (focusedRowKey.startsWith('folder:')) {
|
||||||
|
selector = `#folder-row-${focusedRowKey.slice('folder:'.length)}`;
|
||||||
|
}
|
||||||
|
if (!selector) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const row = container.querySelector(selector);
|
||||||
|
if (!row || !container.contains(row)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const header = container.querySelector('thead');
|
||||||
|
const headerHeight = header ? header.getBoundingClientRect().height : 0;
|
||||||
|
const rowTop = row.offsetTop;
|
||||||
|
const rowBottom = rowTop + row.offsetHeight;
|
||||||
|
const visibleTop = container.scrollTop + headerHeight;
|
||||||
|
const visibleBottom = container.scrollTop + container.clientHeight;
|
||||||
|
|
||||||
|
if (rowTop < visibleTop) {
|
||||||
|
container.scrollTop = Math.max(rowTop - headerHeight, 0);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (rowBottom > visibleBottom) {
|
||||||
|
const nextScrollTop = rowBottom - container.clientHeight;
|
||||||
|
container.scrollTop = Math.max(nextScrollTop, 0);
|
||||||
|
}
|
||||||
|
}, [focusedRowKey]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
ensureFocusedRowVisible();
|
||||||
|
}, [ensureFocusedRowVisible]);
|
||||||
|
|
||||||
|
const activeDescendantId = useMemo(() => {
|
||||||
|
if (!focusedRowKey) return undefined;
|
||||||
|
if (focusedRowKey.startsWith('document:')) {
|
||||||
|
return `document-row-${focusedRowKey.slice('document:'.length)}`;
|
||||||
|
}
|
||||||
|
if (focusedRowKey.startsWith('folder:')) {
|
||||||
|
return `folder-row-${focusedRowKey.slice('folder:'.length)}`;
|
||||||
|
}
|
||||||
|
return undefined;
|
||||||
|
}, [focusedRowKey]);
|
||||||
|
|
||||||
|
const handleDocumentTagDragOver = useCallback(
|
||||||
|
(event) => {
|
||||||
|
if (!isTagDragEvent(event)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
event.dataTransfer.dropEffect = 'copy';
|
||||||
|
event.currentTarget.classList.add('tag-drop-target');
|
||||||
|
},
|
||||||
|
[isTagDragEvent],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleDocumentTagDragLeave = useCallback(
|
||||||
|
(event) => {
|
||||||
|
if (!isTagDragEvent(event)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.relatedTarget && event.currentTarget.contains(event.relatedTarget)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.currentTarget.classList.remove('tag-drop-target');
|
||||||
|
},
|
||||||
|
[isTagDragEvent],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleDocumentTagDrop = useCallback(
|
||||||
|
(event, documentId) => {
|
||||||
|
if (!isTagDragEvent(event)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
event.currentTarget.classList.remove('tag-drop-target');
|
||||||
|
const payload =
|
||||||
|
event.dataTransfer.getData('application/x-papercrate-tag') ||
|
||||||
|
event.dataTransfer.getData('text/papercrate-tag');
|
||||||
|
if (!payload) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const parsed = JSON.parse(payload);
|
||||||
|
if (parsed?.id && onDocumentTagDrop) {
|
||||||
|
onDocumentTagDrop(documentId, parsed);
|
||||||
|
}
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {}
|
||||||
|
},
|
||||||
|
[isTagDragEvent, onDocumentTagDrop],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleGridBackgroundClick = useCallback(
|
||||||
|
(event) => {
|
||||||
|
if (event.target !== event.currentTarget) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onClearSelection?.();
|
||||||
|
},
|
||||||
|
[onClearSelection],
|
||||||
|
);
|
||||||
|
|
||||||
|
const showDefaultEmptyState = !showingSearchResults && !subfolders.length && rows.length === 0;
|
||||||
|
const showListSearchEmptyState =
|
||||||
|
showingSearchResults && rows.length === 0 && !isGridView && !isSearchLoading;
|
||||||
|
const showGridSearchEmptyState =
|
||||||
|
isGridView && showingSearchResults && rows.length === 0 && !isSearchLoading;
|
||||||
|
const showSearchHint = showingSearchResults && rows.length > 0;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section
|
||||||
|
className={`documents-panel column documents-panel--view-${isGridView ? 'grid' : 'list'}`}
|
||||||
|
>
|
||||||
|
<div className="column-header">
|
||||||
|
<div className="column-header__titles">
|
||||||
|
<nav className="breadcrumb" aria-label="Folder breadcrumbs">
|
||||||
|
{breadcrumbs.map((crumb, index) => {
|
||||||
|
const isLast = index === breadcrumbs.length - 1;
|
||||||
|
return (
|
||||||
|
<span key={crumb.id} className="breadcrumb-item">
|
||||||
|
{isLast ? (
|
||||||
|
<span className="breadcrumb-current">{crumb.name}</span>
|
||||||
|
) : (
|
||||||
|
<a
|
||||||
|
href="#"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
onFolderSelect(crumb.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{crumb.name}
|
||||||
|
</a>
|
||||||
|
)}
|
||||||
|
{!isLast && <span className="breadcrumb-separator">›</span>}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</nav>
|
||||||
|
{showingSearchResults && (
|
||||||
|
<div className="column-subtitle">Search results</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
<div className="header-actions">
|
||||||
|
<div className="view-toggle" role="group" aria-label="Change view">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`view-toggle__button${isGridView ? '' : ' active'}`}
|
||||||
|
onClick={() => handleSetViewMode('list')}
|
||||||
|
aria-pressed={!isGridView}
|
||||||
|
title="List view"
|
||||||
|
>
|
||||||
|
<ViewListIcon className="view-toggle__icon" size={18} />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`view-toggle__button${isGridView ? ' active' : ''}`}
|
||||||
|
onClick={() => handleSetViewMode('grid')}
|
||||||
|
aria-pressed={isGridView}
|
||||||
|
title="Icons view"
|
||||||
|
>
|
||||||
|
<ViewGridIcon className="view-toggle__icon" size={18} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
onClick={onRequestCreateFolder}
|
||||||
|
disabled={creatingFolder}
|
||||||
|
>
|
||||||
|
{creatingFolder ? 'Creating…' : 'New folder'}
|
||||||
|
</button>
|
||||||
|
<button className="secondary" onClick={onRefresh}>
|
||||||
|
Refresh
|
||||||
|
</button>
|
||||||
|
<button className="secondary" type="button" onClick={onShowSkeuoWorkspace}>
|
||||||
|
Desk View
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{showDefaultEmptyState && (
|
||||||
|
<div className="empty-state">
|
||||||
|
Drop files anywhere or onto a folder to upload documents.
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
{showGridSearchEmptyState && (
|
||||||
|
<div className="empty-state empty-state--global">
|
||||||
|
No documents match the current filters.
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div className="column-body">
|
||||||
|
<div
|
||||||
|
ref={scrollRef}
|
||||||
|
className="documents-scroll"
|
||||||
|
onFocus={(event) => {
|
||||||
|
if (event.target === scrollRef.current) {
|
||||||
|
onDocumentListFocus?.();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.target !== scrollRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (onDocumentListKeyDown) {
|
||||||
|
onDocumentListKeyDown(event);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
aria-activedescendant={isGridView ? undefined : activeDescendantId}
|
||||||
|
>
|
||||||
|
{showDefaultEmptyState ? null : isGridView ? (
|
||||||
|
<div
|
||||||
|
className="documents-grid"
|
||||||
|
role="list"
|
||||||
|
onClick={handleGridBackgroundClick}
|
||||||
|
style={{ '--documents-grid-icon-size': `${gridIconSize}px` }}
|
||||||
|
>
|
||||||
|
{!showingSearchResults &&
|
||||||
|
subfolders.map((folder) => {
|
||||||
|
const canDragFolder = folder.id !== 'root';
|
||||||
|
const isDraggingFolder = draggedFolderId === folder.id;
|
||||||
|
const isSelectedFolder = selectedFolderSet.has(folder.id);
|
||||||
|
const classes = ['document-card', 'folder-card'];
|
||||||
|
if (isDraggingFolder) classes.push('is-dragging');
|
||||||
|
if (isSelectedFolder) classes.push('selected');
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={folder.id}
|
||||||
|
className={classes.join(' ')}
|
||||||
|
role="listitem"
|
||||||
|
id={`folder-card-${folder.id}`}
|
||||||
|
draggable={canDragFolder}
|
||||||
|
onClick={(event) => {
|
||||||
|
onFolderRowClick?.(folder.id, event);
|
||||||
|
const shouldNavigate =
|
||||||
|
!event.defaultPrevented &&
|
||||||
|
!event.metaKey &&
|
||||||
|
!event.ctrlKey &&
|
||||||
|
!event.shiftKey;
|
||||||
|
if (shouldNavigate) {
|
||||||
|
onFolderSelect(folder.id);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onDoubleClick={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
onFolderSelect(folder.id);
|
||||||
|
}}
|
||||||
|
onDragOver={(event) => onFolderDragOver(event, folder.id)}
|
||||||
|
onDragLeave={onFolderDragLeave}
|
||||||
|
onDrop={(event) => onFolderDrop(event, folder.id)}
|
||||||
|
onDragStart={(event) => {
|
||||||
|
if (canDragFolder) {
|
||||||
|
onFolderDragStart(event, folder.id);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onDragEnd={(event) => {
|
||||||
|
if (canDragFolder) {
|
||||||
|
onFolderDragEnd(event);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div className="folder-card__icon">
|
||||||
|
<FolderIcon
|
||||||
|
className="folder-card__icon-svg"
|
||||||
|
size={gridIconSize}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
<div className="folder-card__meta">
|
||||||
|
<div className="folder-card__name" title={folder.name}>
|
||||||
|
{folder.name}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{rows.map((doc) => {
|
||||||
|
const isSelected = selectedSet.has(doc.id);
|
||||||
|
const isDraggingDoc = draggingSet.has(doc.id);
|
||||||
|
const tagList = Array.isArray(doc.tags) ? doc.tags : [];
|
||||||
|
const visibleTags = tagList.slice(0, 3);
|
||||||
|
const remainingTagCount = tagList.length > 3 ? tagList.length - 3 : 0;
|
||||||
|
const cardClasses = ['document-card', 'document'];
|
||||||
|
if (isSelected) cardClasses.push('selected');
|
||||||
|
if (isDraggingDoc) cardClasses.push('is-dragging');
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
key={doc.id}
|
||||||
|
className={cardClasses.join(' ')}
|
||||||
|
role="listitem"
|
||||||
|
id={`document-card-${doc.id}`}
|
||||||
|
data-doc-id={doc.id}
|
||||||
|
onClick={(event) => onDocumentRowClick(doc.id, event)}
|
||||||
|
onDoubleClick={() => onDocumentOpen(doc.id)}
|
||||||
|
draggable
|
||||||
|
onDragStart={(event) => onDocumentDragStart(event, doc)}
|
||||||
|
onDragEnd={onDocumentDragEnd}
|
||||||
|
onDragOver={(event) => handleDocumentTagDragOver(event)}
|
||||||
|
onDragOverCapture={(event) => handleDocumentTagDragOver(event)}
|
||||||
|
onDragLeave={handleDocumentTagDragLeave}
|
||||||
|
onDragLeaveCapture={handleDocumentTagDragLeave}
|
||||||
|
onDrop={(event) => handleDocumentTagDrop(event, doc.id)}
|
||||||
|
onDropCapture={(event) => handleDocumentTagDrop(event, doc.id)}
|
||||||
|
>
|
||||||
|
<DocumentThumbnailImage
|
||||||
|
document={doc}
|
||||||
|
ensureAssetUrl={ensureAssetUrl}
|
||||||
|
getDocumentAsset={getDocumentAsset}
|
||||||
|
alt={`Thumbnail for ${doc.title || doc.original_name}`}
|
||||||
|
maxSize={gridIconSize}
|
||||||
|
/>
|
||||||
|
<div className="document-card__meta">
|
||||||
|
<div
|
||||||
|
className="document-card__title"
|
||||||
|
title={doc.title || doc.original_name}
|
||||||
|
>
|
||||||
|
{doc.title || doc.original_name}
|
||||||
|
</div>
|
||||||
|
{visibleTags.length > 0 && (
|
||||||
|
<div className="document-card__tags">
|
||||||
|
{visibleTags.map((tag) => {
|
||||||
|
const colorSource = tag?.color || tagLookupById?.get(tag.id)?.color;
|
||||||
|
const style = getTagColorStyle(colorSource);
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
key={tag.id}
|
||||||
|
className="badge tag-chip"
|
||||||
|
style={style || undefined}
|
||||||
|
title={tag.label}
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (onTagClick) {
|
||||||
|
onTagClick(tag.id);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
role="button"
|
||||||
|
draggable
|
||||||
|
onDragStart={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
try {
|
||||||
|
if (event.dataTransfer) {
|
||||||
|
event.dataTransfer.effectAllowed = 'copyMove';
|
||||||
|
}
|
||||||
|
const payload = JSON.stringify({
|
||||||
|
id: tag.id,
|
||||||
|
label: tag.label,
|
||||||
|
sourceDocId: doc.id,
|
||||||
|
});
|
||||||
|
event.dataTransfer?.setData('application/x-papercrate-tag', payload);
|
||||||
|
event.dataTransfer?.setData('text/papercrate-tag', payload);
|
||||||
|
event.dataTransfer?.setData('text/plain', tag.label || 'Tag');
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {}
|
||||||
|
}}
|
||||||
|
onDragEnd={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
}}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.key === 'Enter' || event.key === ' ') {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
if (onTagClick) {
|
||||||
|
onTagClick(tag.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{tag.label}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{remainingTagCount > 0 && (
|
||||||
|
<span className="badge tag-chip tag-chip--more">+{remainingTagCount}</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
<table aria-multiselectable="true">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th className="thumb-column">Preview</th>
|
||||||
|
<th>Name</th>
|
||||||
|
<th>Type</th>
|
||||||
|
<th>Updated</th>
|
||||||
|
<th className="actions-column">Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{!showingSearchResults &&
|
||||||
|
subfolders.map((folder) => {
|
||||||
|
const canDragFolder = folder.id !== 'root';
|
||||||
|
const isDraggingFolder = draggedFolderId === folder.id;
|
||||||
|
const isSelectedFolder = selectedFolderSet.has(folder.id);
|
||||||
|
return (
|
||||||
|
<tr
|
||||||
|
key={folder.id}
|
||||||
|
className={`folder${isDraggingFolder ? ' is-dragging' : ''}${
|
||||||
|
focusedRowKey === `folder:${folder.id}` ? ' focused' : ''
|
||||||
|
}${isSelectedFolder ? ' selected' : ''}`}
|
||||||
|
id={`folder-row-${folder.id}`}
|
||||||
|
onClick={(event) => {
|
||||||
|
onFolderRowClick?.(folder.id, event);
|
||||||
|
const shouldNavigate =
|
||||||
|
!event.defaultPrevented &&
|
||||||
|
!event.metaKey &&
|
||||||
|
!event.ctrlKey &&
|
||||||
|
!event.shiftKey;
|
||||||
|
if (shouldNavigate) {
|
||||||
|
onFolderSelect(folder.id);
|
||||||
|
}
|
||||||
|
if (scrollRef.current) {
|
||||||
|
scrollRef.current.focus({ preventScroll: true });
|
||||||
|
}
|
||||||
|
onFocusedRowChange?.(`folder:${folder.id}`);
|
||||||
|
}}
|
||||||
|
onDoubleClick={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
onFolderSelect(folder.id);
|
||||||
|
}}
|
||||||
|
onDragOver={(event) => onFolderDragOver(event, folder.id)}
|
||||||
|
onDragLeave={onFolderDragLeave}
|
||||||
|
onDrop={(event) => onFolderDrop(event, folder.id)}
|
||||||
|
draggable={canDragFolder}
|
||||||
|
onDragStart={(event) => {
|
||||||
|
if (canDragFolder) {
|
||||||
|
onFolderDragStart(event, folder.id);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
onDragEnd={(event) => {
|
||||||
|
if (canDragFolder) {
|
||||||
|
onFolderDragEnd(event);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<td className="thumb-cell">
|
||||||
|
<div className="thumb-icon">
|
||||||
|
<FolderIcon
|
||||||
|
className="thumb-icon__image"
|
||||||
|
size={32}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td className="doc-list__name">
|
||||||
|
<div className="doc-list__name-content">
|
||||||
|
<span>{folder.name}</span>
|
||||||
|
{folder.id !== 'root' && (
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="icon-button ghost doc-list__icon-button"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (!onFolderRename) return;
|
||||||
|
const nextName = window.prompt('Rename folder', folder.name || '');
|
||||||
|
if (!nextName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const trimmed = nextName.trim();
|
||||||
|
if (!trimmed || trimmed === folder.name) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onFolderRename(folder.id, trimmed);
|
||||||
|
}}
|
||||||
|
title="Rename folder"
|
||||||
|
aria-label={`Rename folder ${folder.name}`}
|
||||||
|
>
|
||||||
|
<EditIcon className="doc-list__icon" size={16} />
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td>Folder</td>
|
||||||
|
<td>—</td>
|
||||||
|
<td className="actions">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="icon-button danger"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
onFolderDelete(folder.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
{rows.map((doc) => {
|
||||||
|
const isSelected = selectedSet.has(doc.id);
|
||||||
|
const isDraggingDoc = draggingSet.has(doc.id);
|
||||||
|
const rowClasses = ['document'];
|
||||||
|
if (isSelected) rowClasses.push('selected');
|
||||||
|
if (isDraggingDoc) rowClasses.push('is-dragging');
|
||||||
|
const downloadHref = getDownloadHref?.(doc) || null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<tr
|
||||||
|
key={doc.id}
|
||||||
|
className={rowClasses.join(' ')}
|
||||||
|
id={`document-row-${doc.id}`}
|
||||||
|
data-doc-id={doc.id}
|
||||||
|
onClick={(event) => onDocumentRowClick(doc.id, event)}
|
||||||
|
onDoubleClick={() => onDocumentOpen(doc.id)}
|
||||||
|
draggable
|
||||||
|
onDragStart={(event) => onDocumentDragStart(event, doc)}
|
||||||
|
onDragEnd={onDocumentDragEnd}
|
||||||
|
onDragOver={handleDocumentTagDragOver}
|
||||||
|
onDragLeave={handleDocumentTagDragLeave}
|
||||||
|
onDrop={(event) => handleDocumentTagDrop(event, doc.id)}
|
||||||
|
>
|
||||||
|
<td className="thumb-cell">
|
||||||
|
<DocumentThumbnailImage
|
||||||
|
document={doc}
|
||||||
|
ensureAssetUrl={ensureAssetUrl}
|
||||||
|
getDocumentAsset={getDocumentAsset}
|
||||||
|
alt={`Thumbnail for ${doc.title || doc.original_name}`}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
<td className="doc-list__name">
|
||||||
|
<div className="doc-name">
|
||||||
|
<div className="doc-list__name-content">
|
||||||
|
<span className="doc-name__title">{doc.title || doc.original_name}</span>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="icon-button ghost doc-list__icon-button"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (!onDocumentRename) return;
|
||||||
|
const nextName = window.prompt(
|
||||||
|
'Rename document',
|
||||||
|
doc.title || doc.original_name || '',
|
||||||
|
);
|
||||||
|
if (!nextName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const trimmed = nextName.trim();
|
||||||
|
if (!trimmed || trimmed === (doc.title || doc.original_name)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onDocumentRename(doc.id, trimmed);
|
||||||
|
}}
|
||||||
|
title="Rename document"
|
||||||
|
aria-label={`Rename document ${doc.title || doc.original_name}`}
|
||||||
|
>
|
||||||
|
<EditIcon className="doc-list__icon" size={16} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{(doc.tags || []).length > 0 && (
|
||||||
|
<div className="doc-name__tags">
|
||||||
|
{(doc.tags || []).map((tag) => {
|
||||||
|
const colorSource = tag?.color || tagLookupById?.get(tag.id)?.color;
|
||||||
|
const style = getTagColorStyle(colorSource);
|
||||||
|
return (
|
||||||
|
<span
|
||||||
|
key={tag.id}
|
||||||
|
className="badge tag-chip"
|
||||||
|
style={style || undefined}
|
||||||
|
title={tag.label}
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (onTagClick) {
|
||||||
|
onTagClick(tag.id);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
role="button"
|
||||||
|
draggable
|
||||||
|
onDragStart={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
try {
|
||||||
|
if (event.dataTransfer) {
|
||||||
|
event.dataTransfer.effectAllowed = 'copyMove';
|
||||||
|
}
|
||||||
|
const payload = JSON.stringify({
|
||||||
|
id: tag.id,
|
||||||
|
label: tag.label,
|
||||||
|
sourceDocId: doc.id,
|
||||||
|
});
|
||||||
|
event.dataTransfer?.setData('application/x-papercrate-tag', payload);
|
||||||
|
event.dataTransfer?.setData('text/papercrate-tag', payload);
|
||||||
|
event.dataTransfer?.setData('text/plain', tag.label || 'Tag');
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {}
|
||||||
|
}}
|
||||||
|
onDragEnd={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
}}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.key === 'Enter' || event.key === ' ') {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
if (onTagClick) {
|
||||||
|
onTagClick(tag.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{tag.label}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
<td>{doc.content_type || 'Document'}</td>
|
||||||
|
<td>
|
||||||
|
{doc.updated_at
|
||||||
|
? new Date(doc.updated_at).toLocaleString()
|
||||||
|
: '—'}
|
||||||
|
</td>
|
||||||
|
<td className="actions">
|
||||||
|
<div className="action-buttons">
|
||||||
|
{downloadHref ? (
|
||||||
|
<a
|
||||||
|
className="button-link with-icon"
|
||||||
|
href={downloadHref}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
onClick={(event) => event.stopPropagation()}
|
||||||
|
onAuxClick={(event) => event.stopPropagation()}
|
||||||
|
onContextMenu={(event) => event.stopPropagation()}
|
||||||
|
>
|
||||||
|
<DownloadIcon className="icon-inline" />
|
||||||
|
<span>Download</span>
|
||||||
|
</a>
|
||||||
|
) : (
|
||||||
|
<span className="meta">No download</span>
|
||||||
|
)}
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="danger"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
onDocumentDelete?.(doc.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
Delete
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{showListSearchEmptyState && (
|
||||||
|
<div className="empty-state">No documents match the current filters.</div>
|
||||||
|
)}
|
||||||
|
{showSearchHint && (
|
||||||
|
<div className="search-hint">
|
||||||
|
Showing {rows.length} document{rows.length === 1 ? '' : 's'} in {currentFolderName} and subfolders.
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DocumentsTable;
|
||||||
|
export { DocumentThumbnailImage };
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import { useCallback } from 'react';
|
||||||
|
|
||||||
|
const noop = () => {};
|
||||||
|
|
||||||
|
const normalizeMessage = (error) => {
|
||||||
|
if (!error) return 'Something went wrong.';
|
||||||
|
if (typeof error === 'string') {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
const { response, message } = error;
|
||||||
|
if (response?.data?.error) return response.data.error;
|
||||||
|
if (response?.data?.message) return response.data.message;
|
||||||
|
return message || 'Something went wrong.';
|
||||||
|
};
|
||||||
|
|
||||||
|
const useApiError = ({
|
||||||
|
logger = console,
|
||||||
|
onReport = noop,
|
||||||
|
} = {}) => {
|
||||||
|
return useCallback(
|
||||||
|
(error, { message, variant = 'error', retry = null } = {}) => {
|
||||||
|
const normalizedMessage = message || normalizeMessage(error);
|
||||||
|
if (logger && typeof logger.error === 'function') {
|
||||||
|
logger.error('[API]', normalizedMessage, error);
|
||||||
|
}
|
||||||
|
onReport({ message: normalizedMessage, variant, retry, error });
|
||||||
|
return normalizedMessage;
|
||||||
|
},
|
||||||
|
[logger, onReport],
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useApiError;
|
||||||
@@ -0,0 +1,133 @@
|
|||||||
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { createAssetView } from '../asset_manager';
|
||||||
|
|
||||||
|
const clampOrdinalValue = (value, cardinality, defaultOrdinal) => {
|
||||||
|
const raw = Number.isFinite(value) ? value : defaultOrdinal;
|
||||||
|
let next = Math.max(1, Math.floor(raw));
|
||||||
|
if (cardinality && cardinality > 0) {
|
||||||
|
next = Math.min(next, cardinality);
|
||||||
|
}
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useAssetNavigator = ({
|
||||||
|
document,
|
||||||
|
assetType,
|
||||||
|
ensureAssetUrl,
|
||||||
|
getAsset,
|
||||||
|
prefetch = 2,
|
||||||
|
defaultOrdinal = 1,
|
||||||
|
}) => {
|
||||||
|
const documentId = document?.id || null;
|
||||||
|
|
||||||
|
const asset = useMemo(() => {
|
||||||
|
if (!document || typeof getAsset !== 'function') {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return getAsset(document, assetType);
|
||||||
|
}, [document, assetType, getAsset]);
|
||||||
|
|
||||||
|
const view = useMemo(() => createAssetView(asset), [asset]);
|
||||||
|
const cardinality = view.getCardinality();
|
||||||
|
|
||||||
|
const [ordinal, setOrdinalInternal] = useState(defaultOrdinal);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setOrdinalInternal(defaultOrdinal);
|
||||||
|
}, [documentId, assetType, defaultOrdinal]);
|
||||||
|
|
||||||
|
const setOrdinal = useCallback(
|
||||||
|
(next) => {
|
||||||
|
setOrdinalInternal((prev) => {
|
||||||
|
const target = typeof next === 'function' ? next(prev) : next;
|
||||||
|
return clampOrdinalValue(target, cardinality, defaultOrdinal);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[cardinality, defaultOrdinal],
|
||||||
|
);
|
||||||
|
|
||||||
|
const goPrev = useCallback(() => setOrdinal((value) => value - 1), [setOrdinal]);
|
||||||
|
const goNext = useCallback(() => setOrdinal((value) => value + 1), [setOrdinal]);
|
||||||
|
|
||||||
|
const objects = view.getObjects();
|
||||||
|
const currentObject = view.getObject(ordinal);
|
||||||
|
const currentUrl = currentObject?.url || view.getPrimaryUrl();
|
||||||
|
const currentMetadata = currentObject?.metadata || view.getPrimaryMetadata() || null;
|
||||||
|
|
||||||
|
const canGoPrev = ordinal > 1;
|
||||||
|
const canGoNext = cardinality ? ordinal < cardinality : true;
|
||||||
|
|
||||||
|
const ordinalsNeedingLoad = useMemo(() => {
|
||||||
|
const missing = [];
|
||||||
|
if (!asset) {
|
||||||
|
return missing;
|
||||||
|
}
|
||||||
|
const maxOrdinal = cardinality && cardinality > 0
|
||||||
|
? Math.min(cardinality, ordinal + Math.max(1, prefetch) - 1)
|
||||||
|
: ordinal + Math.max(1, prefetch) - 1;
|
||||||
|
|
||||||
|
for (let ord = ordinal; ord <= maxOrdinal; ord += 1) {
|
||||||
|
const object = view.getObject(ord);
|
||||||
|
if (!object?.url) {
|
||||||
|
missing.push(ord);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return missing;
|
||||||
|
}, [asset, view, ordinal, prefetch, cardinality]);
|
||||||
|
|
||||||
|
const fetchStart = ordinalsNeedingLoad.length ? ordinalsNeedingLoad[0] : null;
|
||||||
|
const fetchEnd = ordinalsNeedingLoad.length ? ordinalsNeedingLoad[ordinalsNeedingLoad.length - 1] : null;
|
||||||
|
const fetchLimit = fetchStart && fetchEnd ? fetchEnd - fetchStart + 1 : null;
|
||||||
|
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!documentId || !asset || !ensureAssetUrl) {
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!fetchStart || !fetchLimit) {
|
||||||
|
setLoading(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
setLoading(true);
|
||||||
|
ensureAssetUrl(documentId, asset, {
|
||||||
|
start: fetchStart,
|
||||||
|
limit: fetchLimit,
|
||||||
|
})
|
||||||
|
.catch(() => {})
|
||||||
|
.finally(() => {
|
||||||
|
if (!cancelled) {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [documentId, asset, ensureAssetUrl, fetchStart, fetchLimit]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
document,
|
||||||
|
documentId,
|
||||||
|
asset,
|
||||||
|
assetType,
|
||||||
|
ordinal,
|
||||||
|
setOrdinal,
|
||||||
|
goPrev,
|
||||||
|
goNext,
|
||||||
|
canGoPrev,
|
||||||
|
canGoNext,
|
||||||
|
cardinality,
|
||||||
|
currentObject,
|
||||||
|
currentUrl,
|
||||||
|
currentMetadata,
|
||||||
|
objects,
|
||||||
|
isLoading: loading,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export default useAssetNavigator;
|
||||||
@@ -3,9 +3,10 @@
|
|||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Paperless-NEO</title>
|
<title>Papercrate</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<script src="/config.js"></script>
|
||||||
<main id="app"></main>
|
<main id="app"></main>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
+3468
-1752
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,92 @@
|
|||||||
|
import React from 'react';
|
||||||
|
import { DownloadIcon } from '../ui/icons';
|
||||||
|
import { formatFileSize } from '../utils/format';
|
||||||
|
|
||||||
|
const PreviewWorkspace = ({
|
||||||
|
document,
|
||||||
|
previewEntry,
|
||||||
|
resolveApiPath,
|
||||||
|
onClose,
|
||||||
|
onRegenerateThumbnails,
|
||||||
|
}) => {
|
||||||
|
if (!document) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const title = document.title || document.original_name || 'Document';
|
||||||
|
const mime = previewEntry?.contentType || document.content_type || 'application/pdf';
|
||||||
|
const downloadHref = document.current_version?.download_path
|
||||||
|
? resolveApiPath(document.current_version.download_path)
|
||||||
|
: null;
|
||||||
|
const sizeBytes = Number(document.current_version?.size_bytes) || 0;
|
||||||
|
const sizeLabel = sizeBytes > 0 ? formatFileSize(sizeBytes) : null;
|
||||||
|
const metadata =
|
||||||
|
document.metadata && Object.keys(document.metadata).length > 0 ? document.metadata : null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<section className="preview-workspace">
|
||||||
|
<header className="preview-workspace__header">
|
||||||
|
<div className="preview-workspace__meta">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
onClick={() => onClose(document.folder_id ?? 'root')}
|
||||||
|
>
|
||||||
|
← Back
|
||||||
|
</button>
|
||||||
|
<div>
|
||||||
|
<h2>{title}</h2>
|
||||||
|
<span className="meta">
|
||||||
|
{document.content_type || mime}
|
||||||
|
{sizeLabel ? ` · ${sizeLabel}` : ''}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="preview-workspace__actions">
|
||||||
|
<a
|
||||||
|
className="button-link with-icon"
|
||||||
|
href={downloadHref || '#'}
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener noreferrer"
|
||||||
|
aria-disabled={!downloadHref}
|
||||||
|
onClick={(event) => {
|
||||||
|
if (!downloadHref) {
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<DownloadIcon className="icon-inline" />
|
||||||
|
<span>Download</span>
|
||||||
|
</a>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="secondary"
|
||||||
|
onClick={() => onRegenerateThumbnails(document.id)}
|
||||||
|
>
|
||||||
|
Re-run analysis
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</header>
|
||||||
|
<div className="preview-workspace__body">
|
||||||
|
{!previewEntry?.url ? (
|
||||||
|
<div className="preview-workspace__message">Loading preview…</div>
|
||||||
|
) : (
|
||||||
|
<iframe
|
||||||
|
src={previewEntry.url}
|
||||||
|
title={`Preview of ${title}`}
|
||||||
|
className="preview-workspace__object"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{metadata && (
|
||||||
|
<section className="preview-workspace__metadata">
|
||||||
|
<h3>Metadata</h3>
|
||||||
|
<pre>{JSON.stringify(metadata, null, 2)}</pre>
|
||||||
|
</section>
|
||||||
|
)}
|
||||||
|
</section>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default PreviewWorkspace;
|
||||||
|
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
import React, { useEffect } from 'react';
|
||||||
|
import { useNavigate, useParams } from 'react-router-dom';
|
||||||
|
|
||||||
|
import { useAppShell } from '../appShellContext';
|
||||||
|
import PreviewWorkspace from '../preview/PreviewWorkspace';
|
||||||
|
|
||||||
|
const DocumentViewerRoute = () => {
|
||||||
|
const {
|
||||||
|
previewWorkspaceDocument,
|
||||||
|
previewWorkspaceEntry,
|
||||||
|
closeDocumentPreview,
|
||||||
|
handleThumbnailRegeneration,
|
||||||
|
ensurePreviewData,
|
||||||
|
notifyApiError,
|
||||||
|
resolveApiPath,
|
||||||
|
} = useAppShell();
|
||||||
|
const { documentId } = useParams();
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!documentId) {
|
||||||
|
navigate('/documents', { replace: true });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let cancelled = false;
|
||||||
|
|
||||||
|
const hydrate = async () => {
|
||||||
|
try {
|
||||||
|
await ensurePreviewData(documentId);
|
||||||
|
} catch (error) {
|
||||||
|
if (cancelled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
notifyApiError(error, 'Failed to open document preview.');
|
||||||
|
navigate('/documents', { replace: true });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
hydrate();
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
cancelled = true;
|
||||||
|
};
|
||||||
|
}, [documentId, ensurePreviewData, notifyApiError, navigate]);
|
||||||
|
|
||||||
|
const isReady =
|
||||||
|
documentId && previewWorkspaceDocument && previewWorkspaceDocument.id === documentId;
|
||||||
|
|
||||||
|
if (!isReady) {
|
||||||
|
return (
|
||||||
|
<main className="preview-main">
|
||||||
|
<div className="preview-workspace__message">Loading preview…</div>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<main className="preview-main">
|
||||||
|
<PreviewWorkspace
|
||||||
|
document={previewWorkspaceDocument}
|
||||||
|
previewEntry={previewWorkspaceEntry}
|
||||||
|
resolveApiPath={resolveApiPath}
|
||||||
|
onClose={closeDocumentPreview}
|
||||||
|
onRegenerateThumbnails={handleThumbnailRegeneration}
|
||||||
|
/>
|
||||||
|
</main>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default DocumentViewerRoute;
|
||||||
|
|
||||||
@@ -0,0 +1,301 @@
|
|||||||
|
import React, { useCallback, useMemo } from 'react';
|
||||||
|
import { ChevronIcon, TrashIcon, EditIcon, FolderIcon } from '../ui/icons';
|
||||||
|
|
||||||
|
import { getTagColorStyle } from '../utils/colors';
|
||||||
|
|
||||||
|
const FolderNode = ({
|
||||||
|
node,
|
||||||
|
depth,
|
||||||
|
isSelected,
|
||||||
|
onToggle,
|
||||||
|
onSelect,
|
||||||
|
onDrop,
|
||||||
|
onDragOver,
|
||||||
|
onDragLeave,
|
||||||
|
onDelete,
|
||||||
|
onRename,
|
||||||
|
renderChildren,
|
||||||
|
onFolderDragStart,
|
||||||
|
onFolderDragEnd,
|
||||||
|
draggingFolderId,
|
||||||
|
}) => {
|
||||||
|
const isRoot = node.id === 'root';
|
||||||
|
const hasChildren = node.children.length > 0;
|
||||||
|
const canToggle = !isRoot && (hasChildren || !node.loaded);
|
||||||
|
const showChevron = !isRoot && hasChildren;
|
||||||
|
const icon = showChevron ? <ChevronIcon className="toggle-icon" /> : null;
|
||||||
|
const canDrag = !isRoot;
|
||||||
|
const isDragging = draggingFolderId === node.id;
|
||||||
|
const isExpanded = isRoot ? true : Boolean(node.expanded);
|
||||||
|
const rowClasses = ['folder-row'];
|
||||||
|
if (isSelected) {
|
||||||
|
rowClasses.push('active');
|
||||||
|
}
|
||||||
|
|
||||||
|
const handleToggleClick = (event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (canToggle) {
|
||||||
|
onToggle(node.id);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li className={`folder-node${isDragging ? ' is-dragging' : ''}`}>
|
||||||
|
<div
|
||||||
|
className={rowClasses.join(' ')}
|
||||||
|
draggable={canDrag}
|
||||||
|
onClick={() => onSelect(node.id)}
|
||||||
|
onDragOver={(event) => onDragOver(event, node.id)}
|
||||||
|
onDragLeave={onDragLeave}
|
||||||
|
onDrop={(event) => onDrop(event, node.id)}
|
||||||
|
onDragStart={(event) => {
|
||||||
|
if (!canDrag || !onFolderDragStart) return;
|
||||||
|
onFolderDragStart(event, node.id);
|
||||||
|
}}
|
||||||
|
onDragEnd={(event) => {
|
||||||
|
if (onFolderDragEnd) {
|
||||||
|
onFolderDragEnd(event);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{!isRoot && (
|
||||||
|
<span
|
||||||
|
className={`toggle${showChevron ? '' : ' invisible'}${isExpanded ? ' expanded' : ''}`}
|
||||||
|
onClick={handleToggleClick}
|
||||||
|
>
|
||||||
|
{icon}
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
<span className="name">
|
||||||
|
<FolderIcon className="folder-icon-image" size={16} />
|
||||||
|
{node.name}
|
||||||
|
</span>
|
||||||
|
{node.id !== 'root' && (
|
||||||
|
<div className="folder-row__actions">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="icon-button ghost"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
if (!onRename) return;
|
||||||
|
const nextName = window.prompt('Rename folder', node.name || '');
|
||||||
|
if (!nextName) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const trimmed = nextName.trim();
|
||||||
|
if (!trimmed || trimmed === node.name) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onRename(node.id, trimmed);
|
||||||
|
}}
|
||||||
|
title="Rename folder"
|
||||||
|
aria-label={`Rename folder ${node.name}`}
|
||||||
|
>
|
||||||
|
<EditIcon className="icon-edit" size={18} />
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="icon-button ghost"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
onDelete(node.id);
|
||||||
|
}}
|
||||||
|
title="Delete folder"
|
||||||
|
aria-label={`Delete folder ${node.name}`}
|
||||||
|
>
|
||||||
|
<TrashIcon className="icon-trash" size={18} />
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
{isExpanded && node.children.length > 0 && (
|
||||||
|
<ul className={`folder-children${depth === 0 ? ' folder-children--level1' : ''}`}>
|
||||||
|
{renderChildren(node.children, depth + 1)}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const Sidebar = ({
|
||||||
|
folderNodes,
|
||||||
|
onToggle,
|
||||||
|
onSelect,
|
||||||
|
onDrop,
|
||||||
|
onDragOver,
|
||||||
|
onDragLeave,
|
||||||
|
onDeleteFolder,
|
||||||
|
onRenameFolder,
|
||||||
|
selectedFolder,
|
||||||
|
onFolderDragStart,
|
||||||
|
onFolderDragEnd,
|
||||||
|
draggedFolderId,
|
||||||
|
tags = [],
|
||||||
|
activeTagIds = [],
|
||||||
|
onToggleTagFilter,
|
||||||
|
correspondents = [],
|
||||||
|
activeCorrespondentIds = [],
|
||||||
|
onToggleCorrespondentFilter,
|
||||||
|
}) => {
|
||||||
|
const sortedCorrespondents = useMemo(
|
||||||
|
() =>
|
||||||
|
[...correspondents].sort((a, b) =>
|
||||||
|
(a?.name || '').localeCompare(b?.name || '', undefined, { sensitivity: 'base' }),
|
||||||
|
),
|
||||||
|
[correspondents],
|
||||||
|
);
|
||||||
|
const activeCorrespondentSet = useMemo(
|
||||||
|
() => new Set(activeCorrespondentIds || []),
|
||||||
|
[activeCorrespondentIds],
|
||||||
|
);
|
||||||
|
const handleToggleTag = onToggleTagFilter || (() => {});
|
||||||
|
const activeTagSet = new Set(activeTagIds);
|
||||||
|
|
||||||
|
const renderNodes = useCallback(
|
||||||
|
(ids, depth) =>
|
||||||
|
ids.map((id) => {
|
||||||
|
const node = folderNodes.get(id);
|
||||||
|
if (!node) return null;
|
||||||
|
return (
|
||||||
|
<FolderNode
|
||||||
|
key={id}
|
||||||
|
node={node}
|
||||||
|
depth={depth}
|
||||||
|
isSelected={selectedFolder === id}
|
||||||
|
onToggle={() => onToggle(id)}
|
||||||
|
onSelect={onSelect}
|
||||||
|
onDrop={onDrop}
|
||||||
|
onDragOver={onDragOver}
|
||||||
|
onDragLeave={onDragLeave}
|
||||||
|
onDelete={onDeleteFolder}
|
||||||
|
onRename={onRenameFolder}
|
||||||
|
renderChildren={renderNodes}
|
||||||
|
onFolderDragStart={onFolderDragStart}
|
||||||
|
onFolderDragEnd={onFolderDragEnd}
|
||||||
|
draggingFolderId={draggedFolderId}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
[
|
||||||
|
folderNodes,
|
||||||
|
selectedFolder,
|
||||||
|
onToggle,
|
||||||
|
onSelect,
|
||||||
|
onDrop,
|
||||||
|
onDragOver,
|
||||||
|
onDragLeave,
|
||||||
|
onDeleteFolder,
|
||||||
|
onRenameFolder,
|
||||||
|
onFolderDragStart,
|
||||||
|
onFolderDragEnd,
|
||||||
|
draggedFolderId,
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
const rootNode = folderNodes.get('root');
|
||||||
|
|
||||||
|
return (
|
||||||
|
<aside className="sidebar column">
|
||||||
|
<div className="sidebar-section sidebar-section--folders">
|
||||||
|
<div className="sidebar-section__header">
|
||||||
|
<h3>Folders</h3>
|
||||||
|
</div>
|
||||||
|
<ul className="folder-tree">
|
||||||
|
{rootNode && renderNodes([rootNode.id], 0)}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
<div className="sidebar-section">
|
||||||
|
<div className="sidebar-section__header">
|
||||||
|
<h3>Tags</h3>
|
||||||
|
<span className="meta">{tags.length}</span>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className={`sidebar-tag-cloud${
|
||||||
|
activeTagSet.size ? ' sidebar-tag-cloud--has-active' : ''
|
||||||
|
}`}
|
||||||
|
role="list"
|
||||||
|
>
|
||||||
|
{tags.length ? (
|
||||||
|
tags.map((tag) => {
|
||||||
|
const isActive = activeTagSet.has(tag.id);
|
||||||
|
const style = getTagColorStyle(tag.color);
|
||||||
|
const className = `sidebar-tag-pill${isActive ? ' active' : ''}`;
|
||||||
|
return (
|
||||||
|
<button
|
||||||
|
key={tag.id}
|
||||||
|
type="button"
|
||||||
|
role="listitem"
|
||||||
|
className={className}
|
||||||
|
style={style || undefined}
|
||||||
|
onClick={() => handleToggleTag(tag.id)}
|
||||||
|
aria-pressed={isActive}
|
||||||
|
draggable
|
||||||
|
onDragStart={(event) => {
|
||||||
|
try {
|
||||||
|
const payload = JSON.stringify({
|
||||||
|
id: tag.id,
|
||||||
|
label: tag.label,
|
||||||
|
color: tag.color || null,
|
||||||
|
});
|
||||||
|
event.dataTransfer.effectAllowed = 'copy';
|
||||||
|
event.dataTransfer.setData('application/x-papercrate-tag', payload);
|
||||||
|
event.dataTransfer.setData('text/papercrate-tag', payload);
|
||||||
|
} catch (
|
||||||
|
// eslint-disable-next-line no-empty
|
||||||
|
error
|
||||||
|
) {}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{tag.label}
|
||||||
|
</button>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
) : (
|
||||||
|
<span className="meta">No tags yet</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="sidebar-section">
|
||||||
|
<div className="sidebar-section__header">
|
||||||
|
<h3>Correspondents</h3>
|
||||||
|
<span className="meta">{correspondents.length}</span>
|
||||||
|
</div>
|
||||||
|
<ul className="sidebar-correspondent-list">
|
||||||
|
{sortedCorrespondents.length ? (
|
||||||
|
sortedCorrespondents.map((correspondent) => {
|
||||||
|
const isActive = activeCorrespondentSet.has(correspondent.id);
|
||||||
|
const className = `sidebar-correspondent-item${isActive ? ' active' : ''}`;
|
||||||
|
const label = correspondent.name || 'Unnamed';
|
||||||
|
const handleSelect = () => {
|
||||||
|
const nextId = isActive ? null : correspondent.id;
|
||||||
|
onToggleCorrespondentFilter?.(nextId);
|
||||||
|
};
|
||||||
|
return (
|
||||||
|
<li key={correspondent.id}>
|
||||||
|
<span
|
||||||
|
className={className}
|
||||||
|
role="button"
|
||||||
|
onClick={handleSelect}
|
||||||
|
onKeyDown={(event) => {
|
||||||
|
if (event.key === 'Enter' || event.key === ' ') {
|
||||||
|
event.preventDefault();
|
||||||
|
handleSelect();
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</span>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
})
|
||||||
|
) : (
|
||||||
|
<li className="meta">No correspondents yet</li>
|
||||||
|
)}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</aside>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default Sidebar;
|
||||||
|
export { FolderNode };
|
||||||
@@ -0,0 +1,310 @@
|
|||||||
|
/* Skeuomorphic workspace styles */
|
||||||
|
.skeuo-main {
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-shell {
|
||||||
|
flex: 1;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
grid-column: 2 / -1;
|
||||||
|
min-height: 0;
|
||||||
|
position: relative;
|
||||||
|
background-color: var(--surface-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-header {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
padding: 0.75rem 1rem 0.5rem;
|
||||||
|
background-color: var(--surface-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-header__meta {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.35rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-header__meta h2 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 1.25rem;
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-breadcrumbs {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.35rem;
|
||||||
|
font-size: 0.85rem;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-crumb {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.35rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-crumb.is-current {
|
||||||
|
color: var(--fg);
|
||||||
|
font-weight: 600;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-crumb-separator {
|
||||||
|
opacity: 0.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-header__actions {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-canvas {
|
||||||
|
flex: 1;
|
||||||
|
position: relative;
|
||||||
|
overflow: hidden;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-empty {
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
padding: 3rem;
|
||||||
|
text-align: center;
|
||||||
|
color: var(--muted);
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item {
|
||||||
|
position: absolute;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
width: auto;
|
||||||
|
cursor: grab;
|
||||||
|
touch-action: none;
|
||||||
|
transform-origin: center center;
|
||||||
|
transition: transform 0.28s ease, box-shadow 0.16s ease;
|
||||||
|
outline: none;
|
||||||
|
will-change: transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__body {
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
transition: width 0.28s ease, height 0.28s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item:focus-visible {
|
||||||
|
outline: 2px solid var(--accent);
|
||||||
|
outline-offset: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item.is-dragging {
|
||||||
|
cursor: grabbing;
|
||||||
|
transition: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item.is-zoomed {
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item.is-tag-target .skeuo-item__card {
|
||||||
|
outline: 1em dashed var(--accent);
|
||||||
|
outline-offset: 1.41em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item.is-tag-pending .skeuo-item__card {
|
||||||
|
opacity: 0.6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item.is-filtered-out {
|
||||||
|
opacity: 0.12;
|
||||||
|
pointer-events: none;
|
||||||
|
filter: blur(15px) grayscale(100%);
|
||||||
|
transition: opacity 0.6s ease, filter 0.28s ease;
|
||||||
|
z-index: 0 !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__tags {
|
||||||
|
--tag-scale: 1;
|
||||||
|
position: absolute;
|
||||||
|
top: 0;
|
||||||
|
right: 0;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.35rem;
|
||||||
|
align-items: flex-end;
|
||||||
|
transform-origin: top right;
|
||||||
|
transform: scale(var(--tag-scale)) translate(-0.5em, 0.5em);
|
||||||
|
transition: transform 0.28s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-tag {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: flex-start;
|
||||||
|
padding: 0.28rem 0.7rem;
|
||||||
|
border-radius: 1rem;
|
||||||
|
background: linear-gradient(180deg, rgba(0, 0, 0, 0.04), rgba(0, 0, 0, 0) 70%), var(--surface);
|
||||||
|
color: var(--fg);
|
||||||
|
font-size: 1rem;
|
||||||
|
font-weight: 600;
|
||||||
|
box-shadow: 2px 2px 3px rgba(0, 0, 0, 0.18);
|
||||||
|
text-align: left;
|
||||||
|
white-space: nowrap;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__tags .skeuo-tag {
|
||||||
|
font-size: 0.75rem;
|
||||||
|
padding: 0.18rem 0.55rem;
|
||||||
|
pointer-events: auto;
|
||||||
|
cursor: grab;
|
||||||
|
transition: transform 0.16s ease, opacity 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-tag.is-drag-hidden {
|
||||||
|
opacity: 0.4;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-tag span {
|
||||||
|
display: block;
|
||||||
|
pointer-events: none;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.skeuo-card__nav {
|
||||||
|
position: absolute;
|
||||||
|
bottom: calc(3em * 0.707 * var(--nav-scale));
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%) scale(calc(0.707 * var(--nav-scale, 1)));
|
||||||
|
transform-origin: center;
|
||||||
|
display: flex;
|
||||||
|
gap: 2rem;
|
||||||
|
opacity: 0;
|
||||||
|
pointer-events: none;
|
||||||
|
transition: opacity 0.2s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__card:hover .skeuo-card__nav {
|
||||||
|
opacity: 1;
|
||||||
|
pointer-events: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-card__nav-button {
|
||||||
|
display: inline-flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 3.3em;
|
||||||
|
height: 3.3em;
|
||||||
|
padding: 0.36em;
|
||||||
|
border-radius: 3.3em;
|
||||||
|
border: none;
|
||||||
|
background: rgba(0, 0, 0, 0.55);
|
||||||
|
color: #fff;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: background 0.15s ease, opacity 0.15s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-card__nav-button:hover:not([disabled]) {
|
||||||
|
background: rgba(0, 0, 0, 0.75);
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-card__nav-button:disabled {
|
||||||
|
opacity: 0.4;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-card__nav-button:focus-visible {
|
||||||
|
outline: 2px solid var(--accent, #2684ff);
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-card__nav-button svg {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__tags .skeuo-tag.is-tear-pending {
|
||||||
|
opacity: 0.4;
|
||||||
|
}
|
||||||
|
|
||||||
|
body.skeuo-cursor-remove,
|
||||||
|
body.skeuo-cursor-remove * {
|
||||||
|
cursor: not-allowed !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__shadow {
|
||||||
|
display: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__card {
|
||||||
|
position: relative;
|
||||||
|
border-radius: 0;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
box-shadow: 0 24px 72px rgba(0, 0, 0, 0.24);
|
||||||
|
overflow: hidden;
|
||||||
|
--nav-scale: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__card--empty {
|
||||||
|
background:
|
||||||
|
radial-gradient(circle at 28% 24%, rgba(255, 255, 255, 0.32), transparent 60%),
|
||||||
|
radial-gradient(circle at 72% 78%, rgba(0, 0, 0, 0.08), transparent 65%),
|
||||||
|
linear-gradient(135deg, #e6e1d6 0%, #d2cdc2 100%);
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__placeholder {
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-weight: 500;
|
||||||
|
letter-spacing: normal;
|
||||||
|
color: var(--muted);
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__empty {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
gap: 0.5rem;
|
||||||
|
padding: 1rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.skeuo-item__title {
|
||||||
|
font-size: 0.95rem;
|
||||||
|
font-weight: 500;
|
||||||
|
color: #3b3b3b;
|
||||||
|
max-width: 90%;
|
||||||
|
overflow: hidden;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user