This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d87fd33
commit 4f7fd9a
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <switch.h> | ||
#include <string> | ||
#include <vector> | ||
#include <dirent.h> | ||
#include <stdio.h> | ||
#include "model.hpp" | ||
namespace fs{ | ||
u64 getFreeSpace(); | ||
std::string getHumanReadableBytes(u64); | ||
std::vector<std::string> listDirectory(std::string); | ||
bool writeFile(std::string filename, std::string data); | ||
bool deleteFile(std::string filename); | ||
bool fileExists(std::string filename); | ||
bool mkpath(std::string path); | ||
std::string getSuffix(int fType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#include <string> | ||
#include <chrono> | ||
namespace utl { | ||
std::string getRelativeTime(uint64_t timestamp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "fs.hpp" | ||
|
||
namespace fs{ | ||
u64 getFreeSpace() { | ||
u64 space = 0; | ||
fsFsGetFreeSpace(fsdevGetDefaultFileSystem(), "/", &space); | ||
return space; | ||
} | ||
std::vector<std::string> listDirectory(std::string path) { | ||
std::vector<std::string> itemList = {}; | ||
DIR* dir; | ||
struct dirent* ent; | ||
dir = opendir(path.c_str()); | ||
while((ent = readdir(dir))){ | ||
itemList.push_back(ent->d_name); | ||
} | ||
closedir(dir); | ||
return itemList; | ||
} | ||
// https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L34 | ||
bool writeFile(std::string filename, std::string data) { | ||
deleteFile(filename); | ||
|
||
FILE * file = fopen(filename.c_str(), "wb"); | ||
if (!file) { | ||
return false; | ||
} | ||
|
||
size_t result = fwrite(data.c_str(), sizeof(char), data.size(), file); | ||
|
||
fflush(file); | ||
fclose(file); | ||
|
||
return (result == data.size()); | ||
} | ||
// https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L50 | ||
bool deleteFile(std::string filename) { | ||
if (fileExists(filename)) { | ||
return remove(filename.c_str()) == 0; | ||
} | ||
|
||
return false; | ||
} | ||
// https://github.com/AtlasNX/Kosmos-Updater/blob/master/source/FileManager.cpp#L58 | ||
bool fileExists(std::string filename) { | ||
FILE * file = fopen(filename.c_str(), "r"); | ||
|
||
if (file) { | ||
fflush(file); | ||
fclose(file); | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
// http://stackoverflow.com/a/11366985 | ||
bool mkpath(std::string path) { | ||
bool bSuccess = false; | ||
int nRC = ::mkdir( path.c_str(), 0775 ); | ||
if( nRC == -1 ) | ||
{ | ||
switch(errno) | ||
{ | ||
case ENOENT: | ||
//parent didn't exist, try to create it | ||
if( mkpath( path.substr(0, path.find_last_of('/')) ) ) | ||
//Now, try to create again. | ||
bSuccess = 0 == ::mkdir( path.c_str(), 0775 ); | ||
else | ||
bSuccess = false; | ||
break; | ||
case EEXIST: | ||
//Done! | ||
bSuccess = true; | ||
break; | ||
default: | ||
bSuccess = false; | ||
break; | ||
} | ||
} | ||
else | ||
bSuccess = true; | ||
return bSuccess; | ||
} | ||
|
||
std::string getSuffix(int fType){ | ||
switch(fType) { | ||
case model::FType::JPG: | ||
return ".jpg"; | ||
case model::FType::PNG: | ||
return ".png"; | ||
case model::FType::GIF: | ||
return ".gif"; | ||
default: | ||
return ".unsupported"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "utl.hpp" | ||
|
||
using namespace std::chrono; | ||
namespace utl { | ||
std::string getRelativeTime(uint64_t timestamp) { | ||
long currentTimeInSecs = duration_cast<seconds>(system_clock::now().time_since_epoch()).count(); | ||
long minute = 60L; | ||
long hour = 3600L; | ||
long day = 86400L; | ||
long month = 2592000L; | ||
long year = 31536000L; | ||
|
||
std::string relativeString; | ||
int amount; | ||
long relative = currentTimeInSecs-timestamp; | ||
if(relative>year) { | ||
amount = (int)(relative/year); | ||
relativeString = "year"; | ||
} else if(relative>month) { | ||
amount = (int)(relative/month); | ||
relativeString = "month"; | ||
} else if(relative>day) { | ||
amount = (int)(relative/day); | ||
relativeString = "day"; | ||
} else if(relative>hour) { | ||
amount = (int)(relative/hour); | ||
relativeString = "hour"; | ||
} else if(relative>minute) { | ||
amount = (int)(relative/minute); | ||
relativeString = "minute"; | ||
} else { | ||
amount = (int)relative; | ||
relativeString = "second"; | ||
} | ||
if(amount>1){ | ||
relativeString+="s"; | ||
} | ||
return "Uploaded " + std::to_string(amount) + " " + relativeString + " ago"; | ||
} | ||
} |