next-tinacms-dos 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +176 -0
- package/README.md +150 -0
- package/dist/dos-media-store.d.ts +23 -0
- package/dist/dos-tina-cloud-media-store.d.ts +18 -0
- package/dist/errors.d.ts +19 -0
- package/dist/handlers.d.ts +28 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.es.js +131 -0
- package/dist/index.js +172 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# _next-tinacms-dos_
|
|
2
|
+
|
|
3
|
+
Manage **Digital Ocean Space media assets** in TinaCMS.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### With Yarn
|
|
8
|
+
```bash
|
|
9
|
+
yarn add next-tinacms-dos
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### With NPM
|
|
13
|
+
```bash
|
|
14
|
+
npm install next-tinacms-dos
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Connect with Digital Ocean Space
|
|
18
|
+
|
|
19
|
+
You need some credentials provided by Digital Ocean Space to set this up properly. If you do not already have an account, you can (register here)[https://cloud.digitalocean.com/registrations/new].
|
|
20
|
+
|
|
21
|
+
**next-tinacms-dos** uses environment variables within the context of a Next.js site to properly access your Digital Ocean Space account.
|
|
22
|
+
|
|
23
|
+
Add the following variables to an `.env` file.
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
NEXT_PUBLIC_SPACE_ENDPOINT=<Your Digital Ocean Space Endpoint>
|
|
27
|
+
NEXT_PUBLIC_SPACE_NAME=<Your Digital Ocean Space Name>
|
|
28
|
+
NEXT_PUBLIC_SPACE_KEY=<Your Digital Ocean Space access key>
|
|
29
|
+
SPACE_SECRET_KEY=<Your Digital Ocean Space access secret>
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Register the Media Store
|
|
33
|
+
|
|
34
|
+
Now, you can register the Digital Ocean Space Media store with the instance of Tina in your app by passing the `TinaCloudDOSMediaStore` to the `TinaCMS` instance via its `mediaStore` prop.
|
|
35
|
+
|
|
36
|
+
This is also where we can update our `mediaOptions` on the cms object.
|
|
37
|
+
|
|
38
|
+
```tsx
|
|
39
|
+
// Typically in the _app.js file of a Next.js project
|
|
40
|
+
|
|
41
|
+
import dynamic from "next/dynamic";
|
|
42
|
+
import { TinaEditProvider } from "tinacms/dist/edit-state";
|
|
43
|
+
import { Layout } from "../components/layout";
|
|
44
|
+
import { TinaCloudDOSMediaStore } from "next-tinacms-dos";
|
|
45
|
+
const TinaCMS = dynamic(() => import("tinacms"), { ssr: false });
|
|
46
|
+
|
|
47
|
+
const App = ({ Component, pageProps }) => {
|
|
48
|
+
return (
|
|
49
|
+
<>
|
|
50
|
+
<TinaEditProvider
|
|
51
|
+
editMode={
|
|
52
|
+
<TinaCMS
|
|
53
|
+
branch="main"
|
|
54
|
+
clientId={NEXT_PUBLIC_TINA_CLIENT_ID}
|
|
55
|
+
isLocalClient={Boolean(Number(NEXT_PUBLIC_USE_LOCAL_CLIENT))}
|
|
56
|
+
mediaStore={async () => {
|
|
57
|
+
const pack = await import("next-tinacms-dos");
|
|
58
|
+
return pack.TinaCloudDOSMediaStore;
|
|
59
|
+
}}
|
|
60
|
+
{...pageProps}
|
|
61
|
+
>
|
|
62
|
+
{(livePageProps) => (
|
|
63
|
+
<Layout
|
|
64
|
+
rawData={livePageProps}
|
|
65
|
+
data={livePageProps.data?.getGlobalDocument?.data}
|
|
66
|
+
>
|
|
67
|
+
<Component {...livePageProps} />
|
|
68
|
+
</Layout>
|
|
69
|
+
)}
|
|
70
|
+
</TinaCMS>
|
|
71
|
+
}
|
|
72
|
+
>
|
|
73
|
+
<Layout
|
|
74
|
+
rawData={pageProps}
|
|
75
|
+
data={pageProps.data?.getGlobalDocument?.data}
|
|
76
|
+
>
|
|
77
|
+
<Component {...pageProps} />
|
|
78
|
+
</Layout>
|
|
79
|
+
</TinaEditProvider>
|
|
80
|
+
</>
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
...
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Set up API routes
|
|
88
|
+
|
|
89
|
+
Set up a new API route in the `pages` directory of your Next.js app, e.g. `pages/api/media`.
|
|
90
|
+
Then add a new catch all API route for media.
|
|
91
|
+
|
|
92
|
+
Call `createMediaHandler` to set up routes and connect your instance of the Media Store to your Digital Ocean Space.
|
|
93
|
+
|
|
94
|
+
Import `isAuthorized` from [`@tinacms/auth`](https://github.com/tinacms/tinacms/tree/main/packages/%40tinacms/auth).
|
|
95
|
+
|
|
96
|
+
The `authorized` key will make it so only authorized users within Tina Cloud can upload and make media edits.
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
```
|
|
100
|
+
//[...media].tsx
|
|
101
|
+
|
|
102
|
+
import {
|
|
103
|
+
mediaHandlerConfig,
|
|
104
|
+
createMediaHandler,
|
|
105
|
+
} from "next-tinacms-dos/dist/handlers";
|
|
106
|
+
import { isAuthorized } from "@tinacms/auth";
|
|
107
|
+
|
|
108
|
+
export const config = mediaHandlerConfig;
|
|
109
|
+
|
|
110
|
+
export default createMediaHandler({
|
|
111
|
+
config: {
|
|
112
|
+
endpoint: process.env.NEXT_PUBLIC_SPACE_ENDPOINT,
|
|
113
|
+
credentials: {
|
|
114
|
+
accessKeyId: process.env.NEXT_PUBLIC_SPACE_KEY || '',
|
|
115
|
+
secretAccessKey: process.env.SPACE_SECRET_KEY || '',
|
|
116
|
+
},
|
|
117
|
+
region: 'us-east-1',
|
|
118
|
+
},
|
|
119
|
+
bucket: process.env.NEXT_PUBLIC_SPACE_NAME || '',
|
|
120
|
+
authorized: async (req, _res) => {
|
|
121
|
+
if (process.env.NEXT_PUBLIC_USE_LOCAL_CLIENT === "1") {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const user = await isAuthorized(req);
|
|
126
|
+
return user && user.verified;
|
|
127
|
+
} catch (e) {
|
|
128
|
+
console.error(e);
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Update Schema
|
|
137
|
+
|
|
138
|
+
Now that the media store is registered and the API route for media set up, let's add an image to your schema.
|
|
139
|
+
|
|
140
|
+
In your `.tina/schema.ts` add a new field for the image, e.g:
|
|
141
|
+
|
|
142
|
+
```
|
|
143
|
+
{
|
|
144
|
+
name: 'hero',
|
|
145
|
+
type: 'image',
|
|
146
|
+
label: 'Hero Image',
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
Now, when editing your site, the image field will allow you to connect to your Digital Ocean Space via the Media Store to manage your media assets.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import { Media, MediaList, MediaListOptions, MediaStore, MediaUploadOptions } from '@tinacms/toolkit';
|
|
14
|
+
export declare class DOSMediaStore implements MediaStore {
|
|
15
|
+
fetchFunction: (input: RequestInfo, init?: RequestInit) => Promise<Response>;
|
|
16
|
+
accept: string;
|
|
17
|
+
persist(media: MediaUploadOptions[]): Promise<Media[]>;
|
|
18
|
+
delete(media: Media): Promise<void>;
|
|
19
|
+
list(options: MediaListOptions): Promise<MediaList>;
|
|
20
|
+
previewSrc: (publicId: string | Media) => string;
|
|
21
|
+
parse: (img: any) => any;
|
|
22
|
+
private buildQuery;
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import { DOSMediaStore } from './dos-media-store';
|
|
14
|
+
import { Client } from 'tinacms';
|
|
15
|
+
export declare class TinaCloudDOSMediaStore extends DOSMediaStore {
|
|
16
|
+
client: Client;
|
|
17
|
+
constructor(client: Client);
|
|
18
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import { MediaListError } from '@tinacms/toolkit';
|
|
14
|
+
export declare const E_DEFAULT: MediaListError;
|
|
15
|
+
export declare const E_UNAUTHORIZED: MediaListError;
|
|
16
|
+
export declare const E_CONFIG: MediaListError;
|
|
17
|
+
export declare const E_KEY_FAIL: MediaListError;
|
|
18
|
+
export declare const E_BAD_ROUTE: MediaListError;
|
|
19
|
+
export declare const interpretErrorMessage: (message: string) => MediaListError;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
import { S3ClientConfig } from '@aws-sdk/client-s3';
|
|
14
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
15
|
+
export interface DOSConfig {
|
|
16
|
+
config: S3ClientConfig;
|
|
17
|
+
bucket: string;
|
|
18
|
+
authorized: (_req: NextApiRequest, _res: NextApiResponse) => Promise<boolean>;
|
|
19
|
+
}
|
|
20
|
+
export interface DOSOptions {
|
|
21
|
+
cdnUrl?: string;
|
|
22
|
+
}
|
|
23
|
+
export declare const mediaHandlerConfig: {
|
|
24
|
+
api: {
|
|
25
|
+
bodyParser: boolean;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export declare const createMediaHandler: (config: DOSConfig, options?: DOSOptions) => (req: NextApiRequest, res: NextApiResponse) => Promise<void>;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
Copyright 2021 Forestry.io Holdings, Inc.
|
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
you may not use this file except in compliance with the License.
|
|
5
|
+
You may obtain a copy of the License at
|
|
6
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
Unless required by applicable law or agreed to in writing, software
|
|
8
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
9
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
10
|
+
See the License for the specific language governing permissions and
|
|
11
|
+
limitations under the License.
|
|
12
|
+
*/
|
|
13
|
+
export * from './dos-media-store';
|
|
14
|
+
export * from './dos-tina-cloud-media-store';
|
package/dist/index.es.js
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { MediaListError } from "@tinacms/toolkit";
|
|
2
|
+
const E_DEFAULT = new MediaListError({
|
|
3
|
+
title: "An Error Occurred",
|
|
4
|
+
message: "Something went wrong fetching your media from Digital Ocean Space.",
|
|
5
|
+
docsLink: "https://tina.io/packages/next-tinacms-dos"
|
|
6
|
+
});
|
|
7
|
+
const E_UNAUTHORIZED = new MediaListError({
|
|
8
|
+
title: "Unauthorized",
|
|
9
|
+
message: "You don't have access to this resource.",
|
|
10
|
+
docsLink: "https://tina.io/packages/next-tinacms-dos"
|
|
11
|
+
});
|
|
12
|
+
const E_CONFIG = new MediaListError({
|
|
13
|
+
title: "Missing Credentials",
|
|
14
|
+
message: "Unable to connect to Digital Ocean Space because one or more environment variables are missing.",
|
|
15
|
+
docsLink: "https://tina.io/docs/media-dos/"
|
|
16
|
+
});
|
|
17
|
+
const E_KEY_FAIL = new MediaListError({
|
|
18
|
+
title: "Bad Credentials",
|
|
19
|
+
message: "Unable to connect to Digital Ocean Space because one or more environment variables are misconfigured.",
|
|
20
|
+
docsLink: "https://tina.io/docs/media-dos/"
|
|
21
|
+
});
|
|
22
|
+
const E_BAD_ROUTE = new MediaListError({
|
|
23
|
+
title: "Bad Route",
|
|
24
|
+
message: "The Digital Ocean Space API route is missing or misconfigured.",
|
|
25
|
+
docsLink: "https://tina.io/packages/next-tinacms-dos/#set-up-api-routes"
|
|
26
|
+
});
|
|
27
|
+
const interpretErrorMessage = (message) => {
|
|
28
|
+
switch (message) {
|
|
29
|
+
case "Must supply cloud_name":
|
|
30
|
+
case "Must supply api_key":
|
|
31
|
+
case "Must supply api_secret":
|
|
32
|
+
return E_CONFIG;
|
|
33
|
+
case "unknown api_key":
|
|
34
|
+
return E_KEY_FAIL;
|
|
35
|
+
default:
|
|
36
|
+
return E_DEFAULT;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
class DOSMediaStore {
|
|
40
|
+
constructor() {
|
|
41
|
+
this.fetchFunction = (input, init) => {
|
|
42
|
+
return fetch(input, init);
|
|
43
|
+
};
|
|
44
|
+
this.accept = "text/*, application/*, image/*";
|
|
45
|
+
this.previewSrc = (publicId) => {
|
|
46
|
+
if (typeof publicId === "string")
|
|
47
|
+
return publicId;
|
|
48
|
+
return publicId.previewSrc;
|
|
49
|
+
};
|
|
50
|
+
this.parse = (img) => {
|
|
51
|
+
return img.src;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async persist(media) {
|
|
55
|
+
let newFiles = [];
|
|
56
|
+
for (const item of media) {
|
|
57
|
+
const { file, directory } = item;
|
|
58
|
+
const formData = new FormData();
|
|
59
|
+
formData.append("file", file);
|
|
60
|
+
formData.append("directory", directory);
|
|
61
|
+
formData.append("filename", file.name);
|
|
62
|
+
const res = await this.fetchFunction(`/api/dos/media`, {
|
|
63
|
+
method: "POST",
|
|
64
|
+
body: formData
|
|
65
|
+
});
|
|
66
|
+
if (res.status != 200) {
|
|
67
|
+
const responseData = await res.json();
|
|
68
|
+
throw new Error(responseData.message);
|
|
69
|
+
}
|
|
70
|
+
const fileRes = await res.json();
|
|
71
|
+
await new Promise((resolve) => {
|
|
72
|
+
setTimeout(resolve, 2e3);
|
|
73
|
+
});
|
|
74
|
+
const parsedRes = {
|
|
75
|
+
type: "file",
|
|
76
|
+
id: fileRes.public_id,
|
|
77
|
+
filename: fileRes.original_filename,
|
|
78
|
+
directory: "/",
|
|
79
|
+
previewSrc: fileRes.url
|
|
80
|
+
};
|
|
81
|
+
newFiles.push(parsedRes);
|
|
82
|
+
}
|
|
83
|
+
return newFiles;
|
|
84
|
+
}
|
|
85
|
+
async delete(media) {
|
|
86
|
+
await this.fetchFunction(`/api/dos/media/${encodeURIComponent(media.id)}`, {
|
|
87
|
+
method: "DELETE"
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
async list(options) {
|
|
91
|
+
const query = this.buildQuery(options);
|
|
92
|
+
const response = await this.fetchFunction("/api/dos/media" + query);
|
|
93
|
+
if (response.status == 401) {
|
|
94
|
+
throw E_UNAUTHORIZED;
|
|
95
|
+
}
|
|
96
|
+
if (response.status == 404) {
|
|
97
|
+
throw E_BAD_ROUTE;
|
|
98
|
+
}
|
|
99
|
+
if (response.status >= 500) {
|
|
100
|
+
const { e } = await response.json();
|
|
101
|
+
const error = interpretErrorMessage(e);
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
const { items, offset } = await response.json();
|
|
105
|
+
return {
|
|
106
|
+
items: items.map((item) => item),
|
|
107
|
+
nextOffset: offset
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
buildQuery(options) {
|
|
111
|
+
const params = Object.keys(options).filter((key) => options[key] !== "" && options[key] !== void 0).map((key) => `${key}=${options[key]}`).join("&");
|
|
112
|
+
return `?${params}`;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
class TinaCloudDOSMediaStore extends DOSMediaStore {
|
|
116
|
+
constructor(client) {
|
|
117
|
+
super();
|
|
118
|
+
this.client = client;
|
|
119
|
+
this.fetchFunction = async (input, init) => {
|
|
120
|
+
try {
|
|
121
|
+
const url = input.toString();
|
|
122
|
+
const query = `${url.includes("?") ? "&" : "?"}clientID=${client.clientId}`;
|
|
123
|
+
const res = client.fetchWithToken(url + query, init);
|
|
124
|
+
return res;
|
|
125
|
+
} catch (error) {
|
|
126
|
+
console.error(error);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export { DOSMediaStore, TinaCloudDOSMediaStore };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
var __create = Object.create;
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
__markAsModule(target);
|
|
10
|
+
for (var name in all)
|
|
11
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, module2, desc) => {
|
|
14
|
+
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
|
|
15
|
+
for (let key of __getOwnPropNames(module2))
|
|
16
|
+
if (!__hasOwnProp.call(target, key) && key !== "default")
|
|
17
|
+
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
|
|
18
|
+
}
|
|
19
|
+
return target;
|
|
20
|
+
};
|
|
21
|
+
var __toModule = (module2) => {
|
|
22
|
+
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// src/handlers.ts
|
|
26
|
+
__export(exports, {
|
|
27
|
+
createMediaHandler: () => createMediaHandler,
|
|
28
|
+
mediaHandlerConfig: () => mediaHandlerConfig
|
|
29
|
+
});
|
|
30
|
+
var import_client_s3 = __toModule(require("@aws-sdk/client-s3"));
|
|
31
|
+
var import_path = __toModule(require("path"));
|
|
32
|
+
var import_fs = __toModule(require("fs"));
|
|
33
|
+
var import_multer = __toModule(require("multer"));
|
|
34
|
+
var import_util = __toModule(require("util"));
|
|
35
|
+
var mediaHandlerConfig = {
|
|
36
|
+
api: {
|
|
37
|
+
bodyParser: false
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var createMediaHandler = (config, options) => {
|
|
41
|
+
const client = new import_client_s3.S3Client(config.config);
|
|
42
|
+
const bucket = config.bucket;
|
|
43
|
+
const cdnUrl = (options == null ? void 0 : options.cdnUrl) || config.config.endpoint.toString().replace(/http(s|):\/\//i, `https://${bucket}.`);
|
|
44
|
+
return async (req, res) => {
|
|
45
|
+
const isAuthorized = await config.authorized(req, res);
|
|
46
|
+
if (!isAuthorized) {
|
|
47
|
+
res.status(401).json({ message: "sorry this user is unauthorized" });
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
switch (req.method) {
|
|
51
|
+
case "GET":
|
|
52
|
+
return listMedia(req, res, client, bucket, cdnUrl);
|
|
53
|
+
case "POST":
|
|
54
|
+
return uploadMedia(req, res, client, bucket);
|
|
55
|
+
case "DELETE":
|
|
56
|
+
return deleteAsset(req, res, client, bucket);
|
|
57
|
+
default:
|
|
58
|
+
res.end(404);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
async function uploadMedia(req, res, client, bucket) {
|
|
63
|
+
const upload = (0, import_util.promisify)((0, import_multer.default)({
|
|
64
|
+
storage: import_multer.default.diskStorage({
|
|
65
|
+
directory: (req2, file, cb) => {
|
|
66
|
+
cb(null, "/tmp");
|
|
67
|
+
},
|
|
68
|
+
filename: (req2, file, cb) => {
|
|
69
|
+
cb(null, file.originalname);
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
}).single("file"));
|
|
73
|
+
await upload(req, res);
|
|
74
|
+
const { directory } = req.body;
|
|
75
|
+
let prefix = directory.replace(/^\//, "").replace(/\/$/, "");
|
|
76
|
+
if (prefix)
|
|
77
|
+
prefix = prefix + "/";
|
|
78
|
+
const filePath = req.file.path;
|
|
79
|
+
const blob = import_fs.default.readFileSync(filePath);
|
|
80
|
+
const params = {
|
|
81
|
+
Bucket: bucket,
|
|
82
|
+
Key: prefix + import_path.default.basename(filePath),
|
|
83
|
+
Body: blob,
|
|
84
|
+
ACL: "public-read"
|
|
85
|
+
};
|
|
86
|
+
const command = new import_client_s3.PutObjectCommand(params);
|
|
87
|
+
const result = await client.send(command);
|
|
88
|
+
res.json(result);
|
|
89
|
+
}
|
|
90
|
+
async function listMedia(req, res, client, bucket, cdnUrl) {
|
|
91
|
+
var _a;
|
|
92
|
+
try {
|
|
93
|
+
const {
|
|
94
|
+
directory = "",
|
|
95
|
+
limit = 500,
|
|
96
|
+
offset
|
|
97
|
+
} = req.query;
|
|
98
|
+
let prefix = directory.replace(/^\//, "").replace(/\/$/, "");
|
|
99
|
+
if (prefix)
|
|
100
|
+
prefix = prefix + "/";
|
|
101
|
+
const params = {
|
|
102
|
+
Bucket: bucket,
|
|
103
|
+
Delimiter: "/",
|
|
104
|
+
Prefix: prefix,
|
|
105
|
+
Marker: offset == null ? void 0 : offset.toString(),
|
|
106
|
+
MaxKeys: directory && !offset ? +limit + 1 : +limit
|
|
107
|
+
};
|
|
108
|
+
const command = new import_client_s3.ListObjectsCommand(params);
|
|
109
|
+
const response = await client.send(command);
|
|
110
|
+
const items = [];
|
|
111
|
+
(_a = response.CommonPrefixes) == null ? void 0 : _a.forEach(({ Prefix }) => items.push({
|
|
112
|
+
id: Prefix,
|
|
113
|
+
type: "dir",
|
|
114
|
+
filename: import_path.default.basename(Prefix),
|
|
115
|
+
directory: import_path.default.dirname(Prefix)
|
|
116
|
+
}));
|
|
117
|
+
items.push(...(response.Contents || []).filter((file) => file.Key !== prefix).map(getDOSToTinaFunc(cdnUrl)));
|
|
118
|
+
res.json({
|
|
119
|
+
items,
|
|
120
|
+
offset: response.NextMarker
|
|
121
|
+
});
|
|
122
|
+
} catch (e) {
|
|
123
|
+
res.status(500);
|
|
124
|
+
const message = findErrorMessage(e);
|
|
125
|
+
res.json({ e: message });
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
var findErrorMessage = (e) => {
|
|
129
|
+
if (typeof e == "string")
|
|
130
|
+
return e;
|
|
131
|
+
if (e.message)
|
|
132
|
+
return e.message;
|
|
133
|
+
if (e.error && e.error.message)
|
|
134
|
+
return e.error.message;
|
|
135
|
+
return "an error occurred";
|
|
136
|
+
};
|
|
137
|
+
async function deleteAsset(req, res, client, bucket) {
|
|
138
|
+
const { media } = req.query;
|
|
139
|
+
const [, objectKey] = media;
|
|
140
|
+
const params = {
|
|
141
|
+
Bucket: bucket,
|
|
142
|
+
Key: objectKey
|
|
143
|
+
};
|
|
144
|
+
const command = new import_client_s3.DeleteObjectCommand(params);
|
|
145
|
+
try {
|
|
146
|
+
const data = await client.send(command);
|
|
147
|
+
res.json(data);
|
|
148
|
+
} catch (err) {
|
|
149
|
+
res.status(500).json({
|
|
150
|
+
message: err.message || "Something went wrong"
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
function getDOSToTinaFunc(cdnUrl) {
|
|
155
|
+
return function dosToTina(file) {
|
|
156
|
+
const filename = import_path.default.basename(file.Key);
|
|
157
|
+
const directory = import_path.default.dirname(file.Key) + "/";
|
|
158
|
+
return {
|
|
159
|
+
id: file.Key,
|
|
160
|
+
filename,
|
|
161
|
+
directory,
|
|
162
|
+
src: cdnUrl + "/" + file.Key,
|
|
163
|
+
previewSrc: cdnUrl + "/" + file.Key,
|
|
164
|
+
type: "file"
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
169
|
+
0 && (module.exports = {
|
|
170
|
+
createMediaHandler,
|
|
171
|
+
mediaHandlerConfig
|
|
172
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "next-tinacms-dos",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist"
|
|
7
|
+
],
|
|
8
|
+
"typings": "dist/index.d.ts",
|
|
9
|
+
"license": "Apache-2.0",
|
|
10
|
+
"buildConfig": {
|
|
11
|
+
"entryPoints": [
|
|
12
|
+
"src/index.ts",
|
|
13
|
+
{
|
|
14
|
+
"name": "src/handlers.ts",
|
|
15
|
+
"target": "node"
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@aws-sdk/client-s3": "3.150.0",
|
|
21
|
+
"multer": "1.4.2"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@tinacms/toolkit": "0.57.0",
|
|
25
|
+
"@tinacms/scripts": "0.51.0",
|
|
26
|
+
"@types/crypto-js": "^3.1.47",
|
|
27
|
+
"@types/js-cookie": "^2.2.6",
|
|
28
|
+
"@types/node": "^13.13.1",
|
|
29
|
+
"@types/react": "^16.9.43",
|
|
30
|
+
"@types/styled-components": "^5.1.3",
|
|
31
|
+
"next": "10.0.5",
|
|
32
|
+
"react": "17.0.2",
|
|
33
|
+
"react-dom": "17.0.2",
|
|
34
|
+
"styled-components": "^5.2.0",
|
|
35
|
+
"tinacms": "0.0.0-20220818181559",
|
|
36
|
+
"typescript": "4.3.5"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@tinacms/toolkit": "*",
|
|
40
|
+
"react": ">=16.14",
|
|
41
|
+
"react-dom": ">=16.14",
|
|
42
|
+
"react-is": "^16.13.1 || <18.0.0",
|
|
43
|
+
"styled-components": "*",
|
|
44
|
+
"tinacms": ">=0.0.0-20220818181559"
|
|
45
|
+
},
|
|
46
|
+
"publishConfig": {
|
|
47
|
+
"registry": "https://registry.npmjs.org"
|
|
48
|
+
},
|
|
49
|
+
"repository": {
|
|
50
|
+
"url": "https://github.com/tinacms/tinacms.git",
|
|
51
|
+
"directory": "packages/next-tinacms-dos"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"types": "pnpm tsc",
|
|
55
|
+
"build": "tinacms-scripts build"
|
|
56
|
+
}
|
|
57
|
+
}
|