Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
b6380f6f
Commit
b6380f6f
authored
Mar 02, 2023
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fetch media type via node.js
parent
370b1c00
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
77 additions
and
5 deletions
+77
-5
media-type.ts
pages/api/media-type.ts
+24
-0
nextjs-routes.d.ts
types/nextjs-routes.d.ts
+1
-0
NftMedia.tsx
ui/shared/nft/NftMedia.tsx
+24
-5
utils.ts
ui/shared/nft/utils.ts
+28
-0
No files found.
pages/api/media-type.ts
0 → 100644
View file @
b6380f6f
import
type
{
NextApiRequest
,
NextApiResponse
}
from
'
next
'
;
import
nodeFetch
from
'
node-fetch
'
;
import
{
httpLogger
}
from
'
lib/api/logger
'
;
import
getQueryParamString
from
'
lib/router/getQueryParamString
'
;
export
default
async
function
mediaTypeHandler
(
req
:
NextApiRequest
,
res
:
NextApiResponse
)
{
httpLogger
(
req
,
res
);
try
{
const
url
=
getQueryParamString
(
req
.
query
.
url
);
const
response
=
await
nodeFetch
(
url
,
{
method
:
'
HEAD
'
});
if
(
response
.
status
!==
200
)
{
throw
new
Error
();
}
const
contentType
=
response
.
headers
.
get
(
'
content-type
'
);
const
mediaType
=
contentType
?.
startsWith
(
'
video
'
)
?
'
video
'
:
'
image
'
;
res
.
status
(
200
).
json
({
type
:
mediaType
});
}
catch
(
error
)
{
res
.
status
(
200
).
json
({
type
:
undefined
});
}
}
types/nextjs-routes.d.ts
View file @
b6380f6f
...
@@ -15,6 +15,7 @@ declare module "nextjs-routes" {
...
@@ -15,6 +15,7 @@ declare module "nextjs-routes" {
|
DynamicRoute
<
"
/address/[hash]/contract_verification
"
,
{
"
hash
"
:
string
}
>
|
DynamicRoute
<
"
/address/[hash]/contract_verification
"
,
{
"
hash
"
:
string
}
>
|
DynamicRoute
<
"
/address/[hash]
"
,
{
"
hash
"
:
string
}
>
|
DynamicRoute
<
"
/address/[hash]
"
,
{
"
hash
"
:
string
}
>
|
StaticRoute
<
"
/api/csrf
"
>
|
StaticRoute
<
"
/api/csrf
"
>
|
StaticRoute
<
"
/api/media-type
"
>
|
StaticRoute
<
"
/api/proxy
"
>
|
StaticRoute
<
"
/api/proxy
"
>
|
StaticRoute
<
"
/api-docs
"
>
|
StaticRoute
<
"
/api-docs
"
>
|
DynamicRoute
<
"
/apps/[id]
"
,
{
"
id
"
:
string
}
>
|
DynamicRoute
<
"
/apps/[id]
"
,
{
"
id
"
:
string
}
>
...
...
ui/shared/nft/NftMedia.tsx
View file @
b6380f6f
import
{
AspectRatio
,
chakra
,
Skeleton
}
from
'
@chakra-ui/react
'
;
import
{
AspectRatio
,
chakra
,
Skeleton
}
from
'
@chakra-ui/react
'
;
import
{
route
}
from
'
nextjs-routes
'
;
import
React
from
'
react
'
;
import
React
from
'
react
'
;
import
NftImage
from
'
./NftImage
'
;
import
NftImage
from
'
./NftImage
'
;
import
NftVideo
from
'
./NftVideo
'
;
import
NftVideo
from
'
./NftVideo
'
;
import
type
{
MediaType
}
from
'
./utils
'
;
import
{
getPreliminaryMediaType
}
from
'
./utils
'
;
interface
Props
{
interface
Props
{
imageUrl
:
string
|
null
;
imageUrl
:
string
|
null
;
...
@@ -11,7 +14,7 @@ interface Props {
...
@@ -11,7 +14,7 @@ interface Props {
}
}
const
NftMedia
=
({
imageUrl
,
animationUrl
,
className
}:
Props
)
=>
{
const
NftMedia
=
({
imageUrl
,
animationUrl
,
className
}:
Props
)
=>
{
const
[
type
,
setType
]
=
React
.
useState
<
'
image
'
|
'
video
'
|
undefined
>
(
!
animationUrl
?
'
image
'
:
undefined
);
const
[
type
,
setType
]
=
React
.
useState
<
MediaType
|
undefined
>
(
!
animationUrl
?
'
image
'
:
undefined
);
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
if
(
!
animationUrl
)
{
if
(
!
animationUrl
)
{
...
@@ -20,10 +23,26 @@ const NftMedia = ({ imageUrl, animationUrl, className }: Props) => {
...
@@ -20,10 +23,26 @@ const NftMedia = ({ imageUrl, animationUrl, className }: Props) => {
// media could be either gif or video
// media could be either gif or video
// so we pre-fetch the resources in order to get its content type
// so we pre-fetch the resources in order to get its content type
fetch
(
animationUrl
,
{
method
:
'
HEAD
'
})
// have to do it via Node.js due to strict CSP for connect-src
.
then
((
response
)
=>
{
// but in order not to abuse our server firstly we check file url extension
const
contentType
=
response
.
headers
.
get
(
'
content-type
'
);
// and if it is valid we will trust it and display corresponding media component
setType
(
contentType
?.
startsWith
(
'
video
'
)
?
'
video
'
:
'
image
'
);
const
preliminaryType
=
getPreliminaryMediaType
(
animationUrl
);
if
(
preliminaryType
)
{
setType
(
preliminaryType
);
return
;
}
const
url
=
route
({
pathname
:
'
/api/media-type
'
,
query
:
{
url
:
animationUrl
}
});
fetch
(
url
)
.
then
((
response
)
=>
response
.
json
())
.
then
((
_response
)
=>
{
const
response
=
_response
as
{
type
:
MediaType
|
undefined
};
setType
(
response
.
type
||
'
image
'
);
})
.
catch
(()
=>
{
setType
(
'
image
'
);
});
});
},
[
animationUrl
]);
},
[
animationUrl
]);
...
...
ui/shared/nft/utils.ts
0 → 100644
View file @
b6380f6f
export
type
MediaType
=
'
image
'
|
'
video
'
;
const
IMAGE_EXTENSIONS
=
[
'
.jpg
'
,
'
jpeg
'
,
'
.png
'
,
'
.gif
'
,
'
.svg
'
,
];
const
VIDEO_EXTENSIONS
=
[
'
.mp4
'
,
'
.webm
'
,
'
.ogg
'
,
];
export
function
getPreliminaryMediaType
(
url
:
string
):
MediaType
|
undefined
{
if
(
IMAGE_EXTENSIONS
.
some
((
ext
)
=>
url
.
endsWith
(
ext
)))
{
return
'
image
'
;
}
if
(
url
.
startsWith
(
'
data:image
'
))
{
return
'
image
'
;
}
if
(
VIDEO_EXTENSIONS
.
some
((
ext
)
=>
url
.
endsWith
(
ext
)))
{
return
'
video
'
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment