Ma Suhyeon

Edit extraction upload

package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)
func (app *App) PostExtractions(w http.ResponseWriter, r *http.Request) {
......@@ -22,11 +20,7 @@ func (app *App) PostExtractions(w http.ResponseWriter, r *http.Request) {
defer form.Close()
dir := fmt.Sprintf("data/%d", userNo)
os.MkdirAll(dir, 0644)
name := strings.Replace(uuid.New().String(), "-", "", -1)
file, err := os.Create(fmt.Sprintf("%s/%s", dir, name))
file, err := ioutil.TempFile("", "db")
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
......@@ -39,5 +33,59 @@ func (app *App) PostExtractions(w http.ResponseWriter, r *http.Request) {
return
}
db, err := sqlx.Connect("sqlite3", file.Name())
if err != nil {
WriteError(w, http.StatusInternalServerError, "Could not open db file")
return
}
defer db.Close()
tx, err := app.db.Beginx()
if err != nil {
WriteError(w, http.StatusInternalServerError, "Unknown error")
return
}
res, _ := tx.Exec("INSERT INTO extractions (`owner`) VALUES (?)", userNo)
extNo, _ := res.LastInsertId()
rows, err := db.Queryx("SELECT * FROM calllog")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO calls VALUES (?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
sql := `SELECT
a.packagename, a.name, a.version, a.wifiusage, a.cellularusage,
u.lasttimeused, u.totaltimeforeground
FROM AppInfo a JOIN AppUsageYear u ON a.packagename=u.packagename`
rows, err = db.Queryx(sql)
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO apps VALUES (?, ?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
rows, err = db.Queryx("SELECT mid, type, address, body, date FROM sms")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO messages VALUES (?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
rows, err = db.Queryx("SELECT PID, UID, PPID, STIME, TIME, CMD FROM process")
if err == nil {
for rows.Next() {
vals, _ := rows.SliceScan()
tx.Exec("INSERT INTO processes VALUES (?, ?, ?, ?, ?, ?, ?)", append([]interface{}{extNo}, vals...)...)
}
}
tx.Commit()
w.Write([]byte("success"))
}
......