-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
download logs as txt, dropbox or gdrive
- Loading branch information
1 parent
34270cc
commit 0a630c1
Showing
14 changed files
with
536 additions
and
57 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
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
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,127 @@ | ||
import { useEffect, useState } from "react"; | ||
import { Dropbox } from "dropbox"; | ||
import { AsciiSpinner } from "./ascii-spinner"; | ||
import { useGetSession } from "../session/session-handler"; | ||
import Alert from '@material-ui/lab/Alert'; | ||
|
||
const CLIENT_ID = 'g2a1ufuwx67ivex'; | ||
|
||
const getAuthenticationUrl = async () => { | ||
var dbx = new Dropbox({ clientId: CLIENT_ID }); | ||
return dbx.auth.getAuthenticationUrl( window.location.origin+window.location.pathname, "dropbox" ) ; | ||
} | ||
|
||
const readAccessToken = ()=> { | ||
|
||
const regex = /access_token=([^&]+).*state=dropbox/; | ||
let m; | ||
if ((m = regex.exec( window.location.hash )) !== null) { | ||
return m[1]; | ||
} | ||
} | ||
|
||
const sendToDropbox = async (accessToken, filename, contents)=>{ | ||
|
||
var dbx = new Dropbox({ accessToken }); | ||
let op = await dbx.filesUpload({path: '/'+filename, contents, mode:"overwrite" }); | ||
|
||
if( op.status==200 ) | ||
{ | ||
return op.result.path_display; | ||
} | ||
else | ||
{ | ||
throw new Error( op.result.error_summary ); | ||
} | ||
} | ||
|
||
export const DownloadToDropbox = ({selfFocus, txt, isFocused, close})=>{ | ||
|
||
const {session} = useGetSession(); | ||
const [error, setError] = useState(null); | ||
const [loading, setLoading] = useState(false); | ||
const [data, setData] = useState(); | ||
const [sent, setSent] = useState(); | ||
let token = readAccessToken(); | ||
|
||
useEffect(()=>{ | ||
|
||
if( token && !isFocused ) | ||
{ | ||
selfFocus(false); | ||
} | ||
|
||
},[]); | ||
|
||
const onClose = ()=>{ | ||
setSent(null); | ||
close(); | ||
} | ||
|
||
|
||
if( loading ) | ||
{ | ||
if( data===true && txt ) | ||
{ | ||
setLoading(false); | ||
} | ||
|
||
return <AsciiSpinner label={data===true?"Packing your logs...":loading}/> | ||
} | ||
|
||
if( sent === false ) | ||
{ | ||
return <Alert severity="error" onClose={onClose}>{error}</Alert> ; | ||
} | ||
|
||
if( sent ) | ||
{ | ||
return <Alert onClose={onClose}>Logs uploaded to your dropbox folder named "weightxreps.net{sent}"</Alert> ; | ||
} | ||
|
||
if( isFocused ) | ||
{ | ||
if( token ) | ||
{ | ||
if( txt ) | ||
{ | ||
setData( | ||
txt().then( | ||
logsAsText=> sendToDropbox(token, session.user.uname+"--logs.txt", logsAsText) | ||
) | ||
|
||
.then( path=>{ | ||
setSent(path); | ||
}, e=>{ | ||
setSent(false); | ||
setError(e.message); | ||
}) | ||
|
||
.finally(()=>{ | ||
setLoading(false); | ||
setData(null) | ||
}) | ||
); | ||
|
||
setLoading("Sending to dropbox..."); | ||
} | ||
else | ||
{ | ||
setData(true); //<--- flag to indicate we are waiting for the logs | ||
setLoading("Downloading your logs..."); | ||
selfFocus(true); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
// pedir token | ||
setLoading("Connecting to your Dropbox..."); | ||
|
||
getAuthenticationUrl() | ||
.then( url=>window.open(url,"_self") ); | ||
} | ||
} | ||
|
||
return null; | ||
}; |
Oops, something went wrong.