-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_files.go
185 lines (137 loc) · 5.01 KB
/
read_files.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
///////////////////////////////////////////////////////////////////////////////
//
// rmweb/read_files.go
// John Simpson <[email protected]> 2023-12-16
//
// List files on a reMarkable tablet
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strings"
)
///////////////////////////////////////////////////////////////////////////////
//
// Replace problematic characters in documents' visible names
var name_cleaner = strings.NewReplacer( "/" , "_" , "\\" , "_" , ":" , "_" )
///////////////////////////////////////////////////////////////////////////////
//
// read_files
//
// Send a series of "POST http://10.11.99.1/documents/" requests to retrieve
// the documents and containers in the tablet.
//
// Returns a map of IDs pointing to DocInfo structures.
func read_files() ( map[string]DocInfo ) {
////////////////////////////////////////
// Start map to store file/dir info
rv := make( map [string]DocInfo )
////////////////////////////////////////////////////////////
// Process directories until there are no more
l_dirs := []string{ "" }
for len( l_dirs ) > 0 {
////////////////////////////////////////
// Get the first directory name from the array
this_dir := l_dirs[0]
l_dirs = l_dirs[1:]
////////////////////////////////////////
// Request info about this directory
url := "http://" + tablet_addr + "/documents" + this_dir + "/"
content_type := "application/json"
buf := bytes.NewBufferString( "" )
if flag_debug {
fmt.Println( "/========================================" )
fmt.Println( "POST " + url )
}
resp, err := http.Post( url , content_type , buf )
if err != nil {
log.Fatal( err )
}
defer resp.Body.Close()
////////////////////////////////////////
// Read the response into memory
resp_bytes,err := io.ReadAll( resp.Body )
if ( err != nil ) {
log.Fatal( err )
}
if flag_debug {
fmt.Print( string( resp_bytes[:] ) )
fmt.Println( "\\========================================" )
}
////////////////////////////////////////
// Parse the response
var data []map[string]interface{}
err = json.Unmarshal( resp_bytes , &data )
if err != nil {
log.Fatal(err)
}
////////////////////////////////////////
// process items within response
for _,v := range data {
////////////////////////////////////////
// Get info about this item
var size int64
var pages int
id := v["ID"].(string)
parent := v["Parent"].(string)
folder := bool( v["Type"].(string) == "CollectionType" )
vis_name := v["VissibleName"].(string)
////////////////////////////////////////
// Convert all '/', '\', and ':' with underscores
name := name_cleaner.Replace( vis_name )
////////////////////////////////////////
// Get size and page count from data
if ! folder {
if _,ok := v["sizeInBytes"] ; ok {
fmt.Sscan( v["sizeInBytes"].(string) , &size )
list_size = true
}
if _,ok := v["pageCount"] ; ok {
pages = int( v["pageCount"].(float64) )
list_pages = true
}
}
if flag_debug {
fmt.Printf( "%s %-5t %s\n" , id , folder , name )
}
////////////////////////////////////////
// Build user-facing name for this item
parent_name := ""
if parent != "" {
parent_name = rv[parent].full_name
}
full_name := name
if ! flag_collapse {
full_name = parent_name + "/" + name
}
if full_name[0] == '/' {
full_name = full_name[1:]
}
////////////////////////////////////////
// Remember this item
var f DocInfo
f.id = id
f.parent = parent
f.folder = folder
f.name = name
f.full_name = full_name
f.size = int64( size )
f.pages = int64( pages )
f.find_by = strings.ToLower( name )
rv[f.id] = f
////////////////////////////////////////
// If this item is a folder, add it to the list
// so it also gets scanned
if folder {
l_dirs = append( l_dirs , string( this_dir + "/" + id ) )
}
} // for range data
} // for len( l_dirs ) > 0
////////////////////////////////////////
// Return the files and directories
return rv
}