Skip to content

Commit 0b6fbe0

Browse files
authored
Merge pull request #123 from syscoin/feat/nft-video-fallback
feat: nft video fallback
2 parents 465796e + 9c4f42c commit 0b6fbe0

File tree

2 files changed

+75
-15
lines changed

2 files changed

+75
-15
lines changed

app/app/components/UI/NFTImage/index.js

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { addAudioUrl, addImageUrl, addOutOfMemoryUrl, addVideoUrl } from '../../
1212
import Engine from '../../../core/Engine';
1313
import { colors } from '../../../styles/common';
1414
import { isImageFile, isMp3File, isSvgFile, isVideoFile } from '../../../util/general';
15+
import Device from '../../../util/Device';
1516
import SvgImage from '../SvgImage';
1617

1718
export const convertImageUrl = imageUrl => {
@@ -68,7 +69,8 @@ class NFTImage extends PureComponent {
6869
resizeMode: PropTypes.string,
6970
isBlurBg: PropTypes.bool,
7071
svgUseWebView: PropTypes.bool,
71-
isThumbnail: PropTypes.bool
72+
isThumbnail: PropTypes.bool,
73+
videoThumbnail: PropTypes.any
7274
};
7375

7476
state = {
@@ -78,7 +80,8 @@ class NFTImage extends PureComponent {
7880
isSvgUrl: false,
7981
imageLoadingError: false,
8082
defaultLoadingError: false,
81-
parseError: false
83+
parseError: false,
84+
videoError: false
8285
};
8386

8487
refImage = React.createRef();
@@ -97,6 +100,7 @@ class NFTImage extends PureComponent {
97100
addAudioUrl,
98101
addOutOfMemoryUrl
99102
} = this.props;
103+
100104
if (isImageFile(urlValue) || imageUrls.indexOf(urlValue) !== -1) {
101105
this.setState({ isImageUrl: true });
102106
// eslint-disable-next-line no-empty
@@ -108,10 +112,13 @@ class NFTImage extends PureComponent {
108112
} else if (isSvgFile(urlValue)) {
109113
} else {
110114
const task = RNFetchBlob.fetch('GET', urlValue);
111-
task.then(res => {
115+
116+
task.then(async res => {
112117
const status = res.info()?.status;
118+
113119
if (status === 200) {
114120
const contentType = res.info()?.headers['Content-Type']?.toLowerCase();
121+
115122
if (contentType) {
116123
if (contentType.startsWith('video/')) {
117124
this.setState({ isVideoUrl: true });
@@ -131,6 +138,33 @@ class NFTImage extends PureComponent {
131138
addOutOfMemoryUrl(urlValue);
132139
}
133140
}
141+
142+
//Fallback call only on android
143+
if (Device.isAndroid()) {
144+
if (!status || status !== 200) {
145+
const response = await fetch(urlValue, { method: 'HEAD' });
146+
const contentType = response.headers.get('Content-Type');
147+
if (contentType) {
148+
if (contentType.startsWith('video/')) {
149+
this.setState({ isVideoUrl: true });
150+
addVideoUrl(urlValue);
151+
} else if (contentType.startsWith('image/')) {
152+
this.setState({ isImageUrl: true });
153+
addImageUrl(urlValue);
154+
} else if (contentType.startsWith('audio/')) {
155+
this.setState({ isAudioUrl: true });
156+
addAudioUrl(urlValue);
157+
}
158+
}
159+
160+
if (
161+
response.headers.get('Content-Length') &&
162+
response.headers.get('Content-Length') > 1024 * 1024 * 3
163+
) {
164+
addOutOfMemoryUrl(urlValue);
165+
}
166+
}
167+
}
134168
}).catch(err => {
135169
console.error(err);
136170
});
@@ -143,6 +177,10 @@ class NFTImage extends PureComponent {
143177
}
144178
};
145179

180+
handleVideoError = () => {
181+
this.setState({ videoError: true });
182+
};
183+
146184
render() {
147185
const {
148186
style,
@@ -218,18 +256,34 @@ class NFTImage extends PureComponent {
218256
} else if (isVideoUrl || isVideoFile(urlValue)) {
219257
return (
220258
<View style={[style, showBorder && styles.borderStyle, isBlurBg && styles.bgBlack]}>
221-
<Video
222-
muted
223-
source={{ uri: convertToProxyURL(urlValue) }}
224-
style={[{ width, height }, styles.videoLayout]} //组件样式
225-
mixWithOthers={'mix'}
226-
useTextureView
227-
playWhenInactive
228-
playInBackground
229-
ignoreSilentSwitch="ignore"
230-
disableFocus
231-
repeat
232-
/>
259+
{this.state.videoError && this.props.videoThumbnail ? (
260+
<Video
261+
muted
262+
source={{ uri: convertToProxyURL(convertImageUrl(this.props.videoThumbnail)) }}
263+
style={[{ width, height }, styles.videoLayout]}
264+
mixWithOthers={'mix'}
265+
useTextureView
266+
playWhenInactive
267+
playInBackground
268+
ignoreSilentSwitch="ignore"
269+
disableFocus
270+
repeat
271+
/>
272+
) : (
273+
<Video
274+
muted
275+
source={{ uri: convertToProxyURL(urlValue) }}
276+
style={[{ width, height }, styles.videoLayout]}
277+
mixWithOthers={'mix'}
278+
useTextureView
279+
playWhenInactive
280+
playInBackground
281+
ignoreSilentSwitch="ignore"
282+
disableFocus
283+
repeat
284+
onError={this.handleVideoError}
285+
/>
286+
)}
233287
</View>
234288
);
235289
} else if (isSvgUrl || isSvgFile(urlValue)) {

app/app/components/Views/NftView/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,12 @@ class NftView extends PureComponent {
755755
height: this.state.nftImageHeight
756756
}
757757
]}
758+
videoThumbnail={
759+
isSupportLuxy(nftToken.chainId) &&
760+
nftToken.image_thumbnail_url !== 'error' &&
761+
nftToken.image_thumbnail_url &&
762+
nftToken.image_thumbnail_url
763+
}
758764
imageUrl={nftToken.image_url}
759765
resizeMode={'stretch'}
760766
onLoad={e => {

0 commit comments

Comments
 (0)