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 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| func (u *cuser) get(db *sql.DB, command string, t trans.Transmitable) { var err error var isdir, private bool var uid, valid int = 0, 0 var recordCount, ownerid, cfileid, parentLength, downloaded int var pass, filename, originFilename, path, subpath string var queryRow *sql.Row var queryRows *sql.Rows var fileReader *bufio.Reader args := generateArgs(command, 3) if args == nil { valid = 1 goto GET_VERIFY } uid, err = strconv.Atoi(args[1]) if err != nil || strings.ToUpper(args[0]) != "GET" { valid = 1 goto GET_VERIFY } queryRow = db.QueryRow(fmt.Sprintf(`select isdir, private, ownerid, linkpass, cfileid, filename, path, downloaded from ufile where uid=%d`, uid)) if queryRow == nil { valid = 2 goto GET_VERIFY } queryRow.Scan(&isdir, &private, &ownerid, &pass, &cfileid, &filename, &path, &downloaded) if int64(ownerid) != u.id && pass != args[2] || int64(ownerid) != u.id && private { valid = 3 goto GET_VERIFY } GET_VERIFY: if valid != 0 { t.SendBytes([]byte("NOTPERMITTED")) return } else { t.SendBytes([]byte("VALID")) }
db.Exec(fmt.Sprintf(`update ufile set downloaded=%d where uid=%d`, downloaded+1, uid)) var totalFileLength int = 0 if !isdir { if !t.SendBytes(auth.Int64ToBytes(int64(1))) { return } if !t.SendBytes([]byte(filename)) { return } if !t.SendBytes([]byte(auth.Int64ToBytes(int64(0)))) { return } if cfileid < 0 { t.SendFromReader(nil, int64(0)) } else { queryRow = db.QueryRow(fmt.Sprintf(`select size from cfile where uid=%d`, cfileid)) if queryRow == nil { return } queryRow.Scan(&totalFileLength) file, err := os.Open(fmt.Sprintf("%s%d", conf.STORE_PATH, cfileid)) if err != nil { return } defer file.Close() fileReader = bufio.NewReader(file) t.SendFromReader(fileReader, int64(totalFileLength)) } } else { queryRow = db.QueryRow(fmt.Sprintf(`select count (*) from ufile where path like '%s%%' and ownerid=%d`, path+filename+"/", u.id)) originFilename = filename if queryRow == nil { return } err = queryRow.Scan(&recordCount) if err != nil { return } recordCount += 1 if !t.SendBytes(auth.Int64ToBytes(int64(recordCount))) { return } if !t.SendBytes([]byte(filename)) { return } if !t.SendBytes(auth.Int64ToBytes(int64(1))) { return } parentLength = len(path)
queryRows, err = db.Query(fmt.Sprintf(`select filename, path from ufile where path like '%s%%' and isdir=1 and ownerid=%d order by length(path)`, path+filename+"/", ownerid)) if err != nil { return } for queryRows.Next() { err = queryRows.Scan(&filename, &subpath) if err != nil { continue } filename = subpath[parentLength:] + filename if conf.CLIENT_VERSION == "Windows" { filename = strings.Replace(filename, "/", "\\", -1) } if !t.SendBytes([]byte(filename)) { return } if !t.SendBytes(auth.Int64ToBytes(int64(1))) { return } } queryRow = db.QueryRow(fmt.Sprintf(`select count (*) from ufile where path like '%s%%' and isdir=0 and ownerid=%d`, path+originFilename+"/", ownerid)) if queryRow == nil { return } err = queryRow.Scan(&recordCount) if err != nil { return } file_list := make([]downloadItem, 0, recordCount) var fileItem downloadItem queryRows, err = db.Query(fmt.Sprintf(`select filename, path, cfileid from ufile where path like '%s%%' and isdir=0 and ownerid=%d order by length(path)`, path+originFilename+"/", ownerid)) if err != nil { return } for queryRows.Next() { err = queryRows.Scan(&filename, &subpath, &cfileid) if err != nil { continue } if cfileid < 0 { totalFileLength = 0 } else { queryRow = db.QueryRow(fmt.Sprintf(`select size from cfile where uid=%d`, cfileid)) if queryRow == nil { totalFileLength = 0 continue } err = queryRow.Scan(&totalFileLength) if err != nil { totalFileLength = 0 continue } } fileItem.size = totalFileLength filename = subpath[parentLength:] + filename if conf.CLIENT_VERSION == "Windows" { filename = strings.Replace(filename, "/", "\\", -1) } fileItem.filename = filename fileItem.cfileid = cfileid file_list = append(file_list, fileItem) } for _, fileItem = range file_list { if !t.SendBytes([]byte(fileItem.filename)) { break } fmt.Println("发送文件名: ", fileItem.filename) if !t.SendBytes(auth.Int64ToBytes(int64(0))) { break } if fileItem.size > 0 && fileItem.cfileid >= 0 { file, err := os.Open(fmt.Sprintf("%s%d", conf.STORE_PATH, fileItem.cfileid)) if err != nil { fileItem.size = 0 fileReader = nil } else { defer file.Close() fileReader = bufio.NewReader(file) } } else { fileReader = nil } t.SendFromReader(fileReader, int64(fileItem.size)) } } }
|