-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_image_path.go
146 lines (123 loc) · 3.07 KB
/
win_image_path.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
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this db except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
type WinMedia struct {
blob OsBlob
path string
//optional(blob)
table string
column string
row int
}
func InitWinMedia_blob(blob OsBlob) WinMedia {
var ip WinMedia
ip.blob = blob
return ip
}
func InitWinMedia_url(url string) WinMedia {
var ip WinMedia
//get type + cut
var isFile bool
url, isFile = strings.CutPrefix(url, "file:")
if !isFile {
var isDbsPath bool
url, isDbsPath = strings.CutPrefix(url, "db:")
if !isDbsPath {
fmt.Println("must start with 'file:' or 'db:'")
return WinMedia{}
}
}
d := strings.Index(url, ":")
if d >= 0 {
ip.path = url[:d]
//optional(table/column/row)
opt := url[d+1:]
//table
d := strings.Index(opt, "/")
if d <= 0 {
fmt.Println("table '/' is missing")
return WinMedia{}
}
ip.table = opt[:d]
opt = opt[d+1:]
//column
d = strings.Index(opt, "/")
if d <= 0 {
fmt.Println("column '/' is missing")
return WinMedia{}
}
ip.column = opt[:d]
opt = opt[d+1:]
//row
var err error
ip.row, err = strconv.Atoi(opt)
if err != nil {
fmt.Printf("Atoi(%s) failed: %v", opt, err)
return WinMedia{}
}
} else {
ip.path = url
}
return ip
}
func (ip *WinMedia) IsBlob() bool {
return ip.path == ""
}
func (ip *WinMedia) IsDb() bool {
return ip.table != ""
}
func (ip *WinMedia) IsFile() bool {
return !ip.IsDb()
}
func (ip *WinMedia) GetString() string {
return fmt.Sprintf("%s - %s/%s/%d", ip.path, ip.table, ip.column, ip.row)
}
func (a *WinMedia) Cmp(b *WinMedia) bool {
if a.IsBlob() && b.IsBlob() {
return a.blob.CmpHash(&b.blob)
}
return a.path == b.path && a.table == b.table && a.column == b.column && a.row == b.row
}
func (ip *WinMedia) GetBlob(disk *Disk) ([]byte, error) {
var data []byte
var err error
if ip.IsBlob() {
return ip.blob.data, nil
} else if ip.IsFile() {
//file
data, err = os.ReadFile(ip.path)
if err != nil {
return nil, fmt.Errorf("ReadFile(%s) failed: %w", ip.path, err)
}
} else if disk != nil {
//db
db, _, err := disk.OpenDb(ip.path)
if err != nil {
return nil, fmt.Errorf("OpenDb(%s) failed: %w", ip.path, err)
}
db.Lock()
defer db.Unlock()
row := db.ReadRow_unsafe(fmt.Sprintf("SELECT %s FROM %s WHERE rowid==%d", ip.column, ip.table, ip.row))
err = row.Scan(&data)
if err != nil {
return nil, fmt.Errorf("QueryRow(%s) failed: %w", ip.path, err)
}
}
return data, nil
}