mirror of
https://github.com/balzack/databag.git
synced 2025-02-14 20:49:16 +00:00
adding mobile support for gif type
This commit is contained in:
parent
af8eccd688
commit
f795a32eae
@ -5,6 +5,9 @@ import ImageResizer from '@bam.tech/react-native-image-resizer';
|
|||||||
import RNFS from 'react-native-fs';
|
import RNFS from 'react-native-fs';
|
||||||
|
|
||||||
const ENCRYPTED_BLOCK_SIZE = (1024 * 1024);
|
const ENCRYPTED_BLOCK_SIZE = (1024 * 1024);
|
||||||
|
const SCALE_SIZE = (128 * 1024 * 1024);
|
||||||
|
const GIF_TYPE = 'image/gif';
|
||||||
|
const WEBP_TYPE = 'image/webp';
|
||||||
|
|
||||||
export function useUploadContext() {
|
export function useUploadContext() {
|
||||||
|
|
||||||
@ -119,11 +122,17 @@ export function useUploadContext() {
|
|||||||
return { state, actions }
|
return { state, actions }
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getThumb(file, type, position) {
|
async function getThumb(file, type, mime, size, position) {
|
||||||
if (type === 'image') {
|
if (type === 'image') {
|
||||||
const thumb = await ImageResizer.createResizedImage(file, 192, 192, "JPEG", 50, 0, null);
|
if ((mime === GIF_TYPE || mime === WEBP_TYPE) && size < SCALE_SIZE) {
|
||||||
const base = await RNFS.readFile(thumb.path, 'base64')
|
const base = await RNFS.readFile(file, 'base64')
|
||||||
return `data:image/jpeg;base64,${base}`;
|
return `data:image/jpeg;base64,${base}`;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const thumb = await ImageResizer.createResizedImage(file, 192, 192, "JPEG", 50, 0, null);
|
||||||
|
const base = await RNFS.readFile(thumb.path, 'base64')
|
||||||
|
return `data:image/jpeg;base64,${base}`;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (type === 'video') {
|
else if (type === 'video') {
|
||||||
const shot = await createThumbnail({ url: file, timeStamp: position * 1000 })
|
const shot = await createThumbnail({ url: file, timeStamp: position * 1000 })
|
||||||
@ -154,8 +163,8 @@ async function upload(entry, update, complete) {
|
|||||||
entry.active = {};
|
entry.active = {};
|
||||||
try {
|
try {
|
||||||
if (file.encrypted) {
|
if (file.encrypted) {
|
||||||
const { data, type, size, getEncryptedBlock, position } = file;
|
const { data, type, mime, size, getEncryptedBlock, position } = file;
|
||||||
const thumb = await getThumb(data, type, position);
|
const thumb = await getThumb(data, type, mime, size, position);
|
||||||
const parts = [];
|
const parts = [];
|
||||||
for (let pos = 0; pos < size; pos += ENCRYPTED_BLOCK_SIZE) {
|
for (let pos = 0; pos < size; pos += ENCRYPTED_BLOCK_SIZE) {
|
||||||
const len = pos + ENCRYPTED_BLOCK_SIZE > size ? size - pos : ENCRYPTED_BLOCK_SIZE;
|
const len = pos + ENCRYPTED_BLOCK_SIZE > size ? size - pos : ENCRYPTED_BLOCK_SIZE;
|
||||||
|
@ -46,7 +46,7 @@ export function AddTopic({ contentKey, shareIntent, setShareIntent }) {
|
|||||||
const addImage = async () => {
|
const addImage = async () => {
|
||||||
try {
|
try {
|
||||||
const full = await ImagePicker.openPicker({ mediaType: 'photo' });
|
const full = await ImagePicker.openPicker({ mediaType: 'photo' });
|
||||||
actions.addImage(full.path);
|
actions.addImage(full.path, full.mime);
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
@ -29,6 +29,7 @@ export function useAddTopic(contentKey) {
|
|||||||
conflict: false,
|
conflict: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const SCALE_SIZE = (128 * 1024 * 1024);
|
||||||
const assetId = useRef(0);
|
const assetId = useRef(0);
|
||||||
const conversation = useContext(ConversationContext);
|
const conversation = useContext(ConversationContext);
|
||||||
const account = useContext(AccountContext);
|
const account = useContext(AccountContext);
|
||||||
@ -109,7 +110,8 @@ export function useAddTopic(contentKey) {
|
|||||||
const url = file.startsWith('file:') ? file : `file://${file}`;
|
const url = file.startsWith('file:') ? file : `file://${file}`;
|
||||||
|
|
||||||
if (contentKey) {
|
if (contentKey) {
|
||||||
const scaled = scale ? await scale(url) : url;
|
const orig = await RNFS.stat(url);
|
||||||
|
const scaled = (scale && orig.size > SCALE_SIZE) ? await scale(url) : url;
|
||||||
const stat = await RNFS.stat(scaled);
|
const stat = await RNFS.stat(scaled);
|
||||||
const getEncryptedBlock = async (pos, len) => {
|
const getEncryptedBlock = async (pos, len) => {
|
||||||
if (pos + len > stat.size) {
|
if (pos + len > stat.size) {
|
||||||
@ -129,7 +131,7 @@ export function useAddTopic(contentKey) {
|
|||||||
setMessage: (message) => {
|
setMessage: (message) => {
|
||||||
updateState({ message });
|
updateState({ message });
|
||||||
},
|
},
|
||||||
addImage: async (data) => {
|
addImage: async (data, mime) => {
|
||||||
assetId.current++;
|
assetId.current++;
|
||||||
const asset = await setAsset(data, async (file) => {
|
const asset = await setAsset(data, async (file) => {
|
||||||
const scaled = await ImageResizer.createResizedImage(file, 512, 512, "JPEG", 90, 0, null);
|
const scaled = await ImageResizer.createResizedImage(file, 512, 512, "JPEG", 90, 0, null);
|
||||||
@ -137,6 +139,7 @@ export function useAddTopic(contentKey) {
|
|||||||
});
|
});
|
||||||
asset.key = assetId.current;
|
asset.key = assetId.current;
|
||||||
asset.type = 'image';
|
asset.type = 'image';
|
||||||
|
asset.mime = mime;
|
||||||
asset.ratio = 1;
|
asset.ratio = 1;
|
||||||
updateState({ assets: [ ...state.assets, asset ] });
|
updateState({ assets: [ ...state.assets, asset ] });
|
||||||
},
|
},
|
||||||
|
@ -3,14 +3,15 @@ import { TouchableOpacity } from 'react-native-gesture-handler';
|
|||||||
import { useImageThumb } from './useImageThumb.hook';
|
import { useImageThumb } from './useImageThumb.hook';
|
||||||
import { styles } from './ImageThumb.styled';
|
import { styles } from './ImageThumb.styled';
|
||||||
import Colors from 'constants/Colors';
|
import Colors from 'constants/Colors';
|
||||||
|
import FastImage from 'react-native-fast-image'
|
||||||
|
|
||||||
export function ImageThumb({ url, onAssetView }) {
|
export function ImageThumb({ url, onAssetView }) {
|
||||||
const { state, actions } = useImageThumb();
|
const { state, actions } = useImageThumb();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TouchableOpacity activeOpacity={1} onPress={onAssetView}>
|
<TouchableOpacity activeOpacity={1} onPress={onAssetView}>
|
||||||
<Image source={{ uri: url }} style={{ opacity: state.loaded ? 1 : 0, borderRadius: 4, width: 92 * state.ratio, height: 92, marginRight: 16 }}
|
<FastImage source={{ uri: url }} style={{ opacity: state.loaded ? 1 : 0, borderRadius: 4, width: 92 * state.ratio, height: 92, marginRight: 16 }}
|
||||||
onLoad={actions.loaded} resizeMode={'cover'} />
|
onLoad={actions.loaded} resizeMode={FastImage.resizeMode.contain} />
|
||||||
</TouchableOpacity>
|
</TouchableOpacity>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@ export function useImageThumb() {
|
|||||||
|
|
||||||
const actions = {
|
const actions = {
|
||||||
loaded: (e) => {
|
loaded: (e) => {
|
||||||
const { width, height } = e.nativeEvent.source;
|
const { width, height } = e.nativeEvent;
|
||||||
updateState({ loaded: true, ratio: width / height });
|
updateState({ loaded: true, ratio: width / height });
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user