Skip to content

Commit

Permalink
Android homologation code completed, Storage initialization fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Simbad Marino authored and Simbad Marino committed Oct 14, 2022
1 parent f5cae87 commit 85aef43
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 96 deletions.
23 changes: 17 additions & 6 deletions android/app/src/main/cpp/nativelib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@ extern "C" {
Java_com_justshare_BTFSmodule_mainC(JNIEnv *env, jobject, jstring btfsCMD) {
const char *cstr = env->GetStringUTFChars(btfsCMD, 0);
//printf("Transformed Commands into %*C", cstr);
char *cout = mainC(const_cast<char *>(cstr));
jstring out = env->NewStringUTF(cout);
//std::string hello = "Hello from C++ mainC function";
env->ReleaseStringUTFChars(btfsCMD, cstr);
free(cout);
return out;
try
{
char *cout = mainC(const_cast<char *>(cstr));
jstring out = env->NewStringUTF(cout);
//std::string hello = "Hello from C++ mainC function";
env->ReleaseStringUTFChars(btfsCMD, cstr);
free(cout);
return out;
}
catch(...)
{
jstring out = env->NewStringUTF("General Error");
return out;
}



}
}
15 changes: 4 additions & 11 deletions components/Browser/Files/FileItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,18 @@ export default function FileItem({
: decodeURI(item.name)
}
visible={itemActionsOpen}
actionItems={['Rename', 'Move', 'Copy', 'Upload to BTFS', 'Copy BTFS CID', 'Share', 'Delete', 'Cancel']}
actionItems={['Rename', 'Move', 'Copy', 'Share', 'Delete', 'Cancel']}
itemIcons={[
'edit',
'drive-file-move',
'file-copy',
'cloud-upload',
'cloud',
'share',
'delete',
'close',
]}
onClose={setItemActionsOpen}
onItemPressed={(buttonIndex) => {
if (buttonIndex === 6) {
if (buttonIndex === 4) {
setTimeout(() => {
Alert.alert(
'Confirm Delete',
Expand All @@ -178,18 +176,13 @@ export default function FileItem({
]
);
}, 300);
} else if (buttonIndex === 5) {
} else if (buttonIndex === 3) {
Sharing.isAvailableAsync().then((canShare) => {
if (canShare) {
Sharing.shareAsync(docDir + '/' + item.name);
}
});
} else if (buttonIndex === 4) {
Alert.alert("Not implemented yet :)")
}
else if (buttonIndex === 3) {
Alert.alert("Not implemented yet :)")
}
else if (buttonIndex === 2) {
setMoveOrCopy('Copy');
if (!multiSelect) toggleSelect(item);
Expand All @@ -204,7 +197,7 @@ export default function FileItem({
setNewFileName(item.name);
}
}}
cancelButtonIndex={6}
cancelButtonIndex={5}
modalStyle={{ backgroundColor: colors.background2 }}
itemTextStyle={{ color: colors.text }}
titleStyle={{ color: colors.secondary }}
Expand Down
182 changes: 105 additions & 77 deletions screens/Browser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ const {BTFSmodule} = NativeModules;
var currentFileQMhash = '';
var OSpath = '';
var OSpathHomeSize = 0;
var actionItemsVar = [];
var itemIconsVar = [];

import Clipboard from "@react-native-clipboard/clipboard";

Expand Down Expand Up @@ -103,6 +105,7 @@ const Browser = ({ route }: IBrowserProps) => {
const [result, setResult] = useState<Array<DocumentPickerResponse> | DirectoryPickerResponse | undefined | null>();
const [qmhash, setqmhash] = useState('');
const [default_storage, set_default_storage] = useState('');
//const [storage_saver, set_storage_saver] = useState('');
//const [fileResponse, setFileResponse] = useState([]);
const [btfsRepo, setbtfsRepo] = useState(false);
const [enableDaemon, setenableDaemon] = useState(false);
Expand All @@ -127,11 +130,33 @@ const Browser = ({ route }: IBrowserProps) => {
{
OSpath = "files";
OSpathHomeSize = 5;
actionItemsVar = [
'Import File from Storage',
'Import CID',
'Cancel',
];
itemIconsVar = [
'storage',
'cloud-download',
'close',
];
}
else if (Platform.OS == "ios")
{
OSpath = "Documents";
OSpathHomeSize = 9;
actionItemsVar = [
'Camera Roll',
'Import File from Storage',
'Import CID',
'Cancel',
];
itemIconsVar = [
'camera-roll',
'storage',
'cloud-download',
'close',
];
}

},[]);
Expand All @@ -151,6 +176,8 @@ const Browser = ({ route }: IBrowserProps) => {
getFiles();
console.log("Getting default storage...");
getStorageDuration();
//console.log("Getting storage saver data...");
//getStorageSaverFlag();

});

Expand Down Expand Up @@ -217,11 +244,25 @@ const Browser = ({ route }: IBrowserProps) => {
async function getStorageDuration(){

const storedStorageDuration = await AsyncStorage.getItem('storage_duration');
if (storedStorageDuration == null)
{
await AsyncStorage.setItem('storage_duration', '32');
}
console.log(storedStorageDuration);
//let numericstoredStorageDuration = parseInt(storedStorageDuration);
set_default_storage(storedStorageDuration);


}

async function getStorageSaverFlag(){

const storedStorageSaver = await AsyncStorage.getItem('storage_saver');
console.log(storedStorageSaver);
//let numericstoredStorageDuration = parseInt(storedStorageDuration);
set_storage_saver(storedStorageSaver);


}

const enableBTFSDaemon = async (en) => {
Expand Down Expand Up @@ -444,6 +485,7 @@ function addFileToBTFS(file)

const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsMultipleSelection: false,
aspect: [4, 3],
quality: 1,
});
Expand Down Expand Up @@ -476,9 +518,9 @@ function addFileToBTFS(file)
{
const formData = new FormData();

console.log("FILE: " + file);
var urresponse = decodeURIComponent(file.uri);
console.log("urresponse: " + urresponse);
console.log(file);
// var urresponse = decodeURIComponent(file.uri);
//console.log("urresponse: " + urresponse);



Expand Down Expand Up @@ -550,7 +592,7 @@ function addFileToBTFS(file)
});


const { exists: fileExists } = FileSystem.getInfoAsync(urresponse);
//const { exists: fileExists } = FileSystem.getInfoAsync(urresponse);
//let filenameStr = file.name.replace(' ','_').replace('-', '_');
//filenameStr = filenameStr.replace(/-/,'_');
//console.log("str filename: " + filenameStr);
Expand All @@ -566,23 +608,12 @@ function addFileToBTFS(file)

try {
var response = [];
if(Platform.OS == "android")
{
response = await DocumentPicker.pick({
presentationStyle: 'fullScreen',
allowMultiSelection: true,
copyTo: "cachesDirectory", //TODO: Android Only?
});
}

if(Platform.OS == "ios")
{
response = await DocumentPicker.pick({
presentationStyle: 'fullScreen',
allowMultiSelection: true,
//copyTo: "cachesDirectory", //TODO: Android Only?
});
}


console.log(response);
//setResult(response);
Expand All @@ -604,38 +635,31 @@ function addFileToBTFS(file)
//const obj = JSON.parse(response[0]);
console.log(file);
console.log("Index: " + index);
var urresponse = decodeURIComponent(file.fileCopyUri); //TODO:Check if this is OK for Android
console.log("urresponse: " + urresponse);
//var urresponse = decodeURIComponent(file.fileCopyUri); //TODO:Check if this is OK for Android
//console.log("urresponse: " + urresponse);
//console.log(currentDir);

FileSystem.copyAsync({
from: file.fileCopyUri, //TODO:Check if this is OK for Android
to: currentDir + "/" + file.name.replace(/ /g, "_").replace(/-/g, "_").toLowerCase(),
})
console.log("Step 1");
//addFileToBTFS(currentDir + '/' + file.name);
getFiles();

if(Platform.OS == "android")
{
formData.append(file.name, {
uri: file.fileCopyUri,
name: file.name ,
type: file.type
});
}

if(Platform.OS == "ios")
{

FileSystem.copyAsync({
from: file.uri, //TODO:Check if this is OK for Android
to: currentDir + "/" + file.name.replace(/ /g, "_").replace(/-/g, "_").toLowerCase(),
})
console.log("Step 1");
//addFileToBTFS(currentDir + '/' + file.name);
getFiles();
formData.append(file.name, {
uri: file.uri,
name: file.name ,
name: file.name,
type: file.type
});
}


var addFileData = axios.post("http://localhost:5001/api/v1/add?w=true&n=true", formData, {



var addFileData = axios.post("http://localhost:5001/api/v1/add?w=true", formData, { //TODO: Future implementation: Enable storage saver by not copying files to btfs repo with http://localhost:5001/api/v1/add?w=true&n=true
headers: {
'Content-Type': 'multipart/form-data'
}
Expand All @@ -654,21 +678,26 @@ function addFileToBTFS(file)
console.log("QMhash obtained :), proceeding to upload file...");
let homePointer = currentDir.search(OSpath) + OSpathHomeSize; // Ripping off the whole path + Documents home folder, 9 is the "Documents" string magic number
let currentdir_stripped = currentDir.slice(homePointer);
console.log(currentdir_stripped);
var fileCopyToMFS = axios.post("http://localhost:5001/api/v1/files/cp?arg=/btfs/" + currentFileQMhash + "&arg=" + currentdir_stripped + "/" + file.name.replace(/ /g, "_").replace(/-/g, "_").toLowerCase())
.then(function (fileCopyToMFS){
console.log("http://localhost:5001/api/v1/files/cp?arg=/btfs/" + currentFileQMhash + "&arg=" + currentdir_stripped + "/" + file.name.replace(/ /g, "_").replace(/-/g, "_").toLowerCase());
console.log(fileCopyToMFS);
console.log("Step 3");
// getStorageDuration();
})

.catch(function(error) {
console.log('There has been a problem with the BTFS copy command: ' + error.message);
//Alert.alert("Error", "Password already set, use cli to change it if needed");
//console.log("Call response: " + JSON.stringify(depositBTT_resp));
Alert.alert("BTFS Copy MFS Error", "Format error u.u ");
});
console.log("currentdir_Stripped: " + currentdir_stripped);
//Now proceeding to copy to Mutable File System

var fileCopyToMFS = axios.post("http://localhost:5001/api/v1/files/cp?arg=/btfs/" + currentFileQMhash + "&arg=" + currentdir_stripped + "/" + file.name.replace(/ /g, "_").replace(/-/g, "_").toLowerCase())
.then(function (fileCopyToMFS){
console.log("http://localhost:5001/api/v1/files/cp?arg=/btfs/" + currentFileQMhash + "&arg=" + currentdir_stripped + "/" + file.name.replace(/ /g, "_").replace(/-/g, "_").toLowerCase());
console.log(fileCopyToMFS);
console.log("Step 3");
// getStorageDuration();
})

.catch(function(error) {
console.log('There has been a problem with the BTFS copy command: ' + error.message);
//Alert.alert("Error", "Password already set, use cli to change it if needed");
//console.log("Call response: " + JSON.stringify(depositBTT_resp));
Alert.alert("BTFS Copy MFS Error", "Format error u.u ");
});



console.log("http://localhost:5001/api/v1/storage/upload?arg=" + currentFileQMhash + "&len=" + default_storage)
console.log("Step 4");
var fileUploadID = axios.post("http://localhost:5001/api/v1/storage/upload?arg=" + currentFileQMhash + "&len=" + default_storage)
Expand Down Expand Up @@ -697,7 +726,7 @@ function addFileToBTFS(file)
});


const { exists: fileExists } = FileSystem.getInfoAsync(urresponse);
//const { exists: fileExists } = FileSystem.getInfoAsync(urresponse);
//let filenameStr = file.name.replace(' ','_').replace('-', '_');
//filenameStr = filenameStr.replace(/-/,'_');
//console.log("str filename: " + filenameStr);
Expand Down Expand Up @@ -831,33 +860,32 @@ function addFileToBTFS(file)
<ActionSheet
title={'Add a new file'}
visible={newFileActionSheet}
actionItems={[
'Camera Roll',
'Multi Image Picker',
'Import File from Storage',
'Import CID',
'Cancel',
]}
itemIcons={[
'camera-roll',
'image',
'storage',
'cloud-download',
'close',
]}
actionItems={actionItemsVar}
itemIcons={itemIconsVar}
onClose={setNewFileActionSheet}
onItemPressed={(buttonIndex) => {
if (buttonIndex === 0) {
pickImage();
} else if (buttonIndex === 1) {
setMultiImageVisible(true);
} else if (buttonIndex === 2) {
pickDocument();
} else if (buttonIndex === 3) {
setDownloadDialogVisible(true);
if(Platform.OS=='ios')
{
if (buttonIndex === 0) {
pickImage();
} else if (buttonIndex === 1) {
pickDocument();
} else if (buttonIndex === 2) {
setDownloadDialogVisible(true);
}
}
if(Platform.OS=='android')
{
if (buttonIndex === 0) {
pickDocument();
} else if (buttonIndex === 1) {
setDownloadDialogVisible(true);
}
}


}}
cancelButtonIndex={4}
cancelButtonIndex={Platform.OS == 'ios'?3:2}
modalStyle={{ backgroundColor: colors.background2 }}
itemTextStyle={{ color: colors.text }}
titleStyle={{ color: colors.secondary }}
Expand Down
Loading

0 comments on commit 85aef43

Please sign in to comment.