github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/third/upyun/bulk_upyun.go (about) 1 package upyun 2 3 import ( 4 "os" 5 "path/filepath" 6 "strconv" 7 "sync" 8 "time" 9 10 "github.com/bronze1man/kmg/kmgLog" 11 "github.com/bronze1man/kmg/kmgTask" 12 ) 13 14 //批量upyun操作 15 type BulkUpyun struct { 16 UpYun *UpYun 17 Tm *kmgTask.LimitThreadTaskManager 18 } 19 20 //批量上传接口 21 //upload a file 22 func (obj *BulkUpyun) UploadFile(upyun_path, local_path string) { 23 obj.Tm.AddTask(kmgTask.TaskFunc(func() { 24 kmgLog.Log("upyun", "upload file: "+upyun_path, nil) 25 file, err := os.Open(local_path) 26 if err != nil { 27 kmgLog.Log("upyunError", err.Error(), err) 28 return 29 } 30 defer file.Close() 31 err = obj.UpYun.WriteFile(upyun_path, file, true) 32 if err != nil { 33 kmgLog.Log("upyunError", err.Error(), err) 34 return 35 } 36 return 37 })) 38 } 39 40 //upload a dir 41 func (obj *BulkUpyun) UploadDir(upyun_path, local_path string) { 42 obj.Tm.AddTask(kmgTask.TaskFunc(func() { 43 kmgLog.Log("upyun", "upload dir: "+upyun_path, nil) 44 45 dir, err := os.Open(local_path) 46 if err != nil { 47 kmgLog.Log("upyunError", err.Error(), err) 48 return 49 } 50 file_list, err := dir.Readdir(0) 51 if err != nil { 52 kmgLog.Log("upyunError", err.Error(), err) 53 return 54 } 55 err = obj.UpYun.MkDir(upyun_path, true) 56 if err != nil { 57 kmgLog.Log("upyunError", err.Error(), err) 58 return 59 } 60 for _, file_info := range file_list { 61 file_name := file_info.Name() 62 this_local_path := local_path + "/" + file_name 63 this_upyun_path := upyun_path + "/" + file_name 64 if file_info.IsDir() { 65 obj.UploadDir(this_upyun_path, this_local_path) 66 } else { 67 obj.UploadFile(this_upyun_path, this_local_path) 68 } 69 } 70 return 71 })) 72 } 73 74 //download a file 75 func (obj *BulkUpyun) DownloadFile(upyun_path, local_path string) { 76 obj.Tm.AddTask(kmgTask.TaskFunc(func() { 77 kmgLog.Log("upyun", "download file: "+upyun_path, nil) 78 err := os.MkdirAll(filepath.Dir(local_path), os.FileMode(0777)) 79 if err != nil { 80 kmgLog.Log("upyunError", err.Error(), err) 81 return 82 } 83 84 file, err := os.Create(local_path) 85 if err != nil { 86 kmgLog.Log("upyunError", err.Error(), err) 87 return 88 } 89 defer file.Close() 90 err = obj.UpYun.ReadFile(upyun_path, file) 91 if err != nil { 92 kmgLog.Log("upyunError", err.Error(), err) 93 return 94 } 95 return 96 })) 97 } 98 99 //resursive download a dir 100 func (obj *BulkUpyun) DownloadDir(upyun_path string, file_path string) { 101 obj.Tm.AddTask(kmgTask.TaskFunc(func() { 102 kmgLog.Log("upyun", "download dir: "+upyun_path, nil) 103 file_list, err := obj.UpYun.ReadDir(upyun_path) 104 file_mode := os.FileMode(0777) 105 if err != nil { 106 kmgLog.Log("upyunError", err.Error(), err) 107 return 108 } 109 for _, file_info := range file_list { 110 file_type := file_info.Type 111 file_name := file_info.Name 112 this_local_path := file_path + "/" + file_name 113 this_upyun_path := upyun_path + "/" + file_name 114 if file_type == "folder" { 115 err := os.MkdirAll(this_local_path, file_mode) 116 if err != nil { 117 kmgLog.Log("upyunError", "os.MkdirAll fail!"+err.Error(), err) 118 return 119 } 120 obj.DownloadDir(this_upyun_path, this_local_path) 121 } else if file_type == "file" { 122 obj.DownloadFile(this_upyun_path, this_local_path) 123 } else { 124 kmgLog.Log("upyunError", "unknow file type2:"+file_type, err) 125 return 126 } 127 } 128 return 129 })) 130 } 131 132 //delete a file 133 func (obj *BulkUpyun) DeleteFile(upyun_path string) { 134 wg := &sync.WaitGroup{} 135 wg.Add(1) 136 obj.deleteFile(upyun_path, wg) 137 obj.Tm.AddTaskNewThread(kmgTask.TaskFunc(func() { 138 wg.Wait() 139 })) 140 } 141 142 //resursive delete a dir 143 func (obj *BulkUpyun) DeleteDir(upyun_path string) { 144 wg := &sync.WaitGroup{} 145 wg.Add(1) 146 obj.deleteDir(upyun_path, wg) 147 obj.Tm.AddTaskNewThread(kmgTask.TaskFunc(func() { 148 wg.Wait() 149 })) 150 } 151 152 //delete a file 153 func (obj *BulkUpyun) deleteFile(upyun_path string, finish_wg *sync.WaitGroup) { 154 obj.Tm.AddTask(kmgTask.TaskFunc(func() { 155 defer finish_wg.Done() 156 kmgLog.Log("upyun", "delete file: "+upyun_path, nil) 157 err := obj.UpYun.DeleteFile(upyun_path) 158 if err != nil { 159 kmgLog.Log("upyunError", "delete file failed!:"+upyun_path+":"+err.Error(), nil) 160 return 161 } 162 return 163 })) 164 } 165 166 //we need to know when is finish delete all file in it ,so we can delete the dir 167 func (obj *BulkUpyun) deleteDir(upyun_path string, finish_wg *sync.WaitGroup) { 168 obj.Tm.AddTask(kmgTask.TaskFunc(func() { 169 wg := &sync.WaitGroup{} 170 defer obj.Tm.AddTaskNewThread(kmgTask.TaskFunc(func() { 171 wg.Wait() 172 wg.Add(1) 173 obj.deleteFile(upyun_path, wg) 174 wg.Wait() 175 finish_wg.Done() 176 })) 177 kmgLog.Log("upyun", "delete dir: "+upyun_path, nil) 178 file_list, err := obj.UpYun.ReadDir(upyun_path) 179 if err != nil { 180 kmgLog.Log("upyunError", err.Error(), err) 181 return 182 } 183 for _, file_info := range file_list { 184 file_type := file_info.Type 185 file_name := file_info.Name 186 this_upyun_path := upyun_path + "/" + file_name 187 if file_type == "folder" { 188 wg.Add(1) 189 obj.deleteDir(this_upyun_path, wg) 190 } else if file_type == "file" { 191 wg.Add(1) 192 obj.deleteFile(this_upyun_path, wg) 193 } else { 194 kmgLog.Log("upyunError", "unknow file type2:"+file_type, nil) 195 return 196 } 197 } 198 return 199 })) 200 } 201 202 func (obj *BulkUpyun) GetFileType(upyun_path string) (file_type string, err error) { 203 info, err := obj.UpYun.GetFileInfo(upyun_path) 204 if err != nil { 205 return 206 } 207 file_type = info["type"] 208 return 209 } 210 211 func (obj *BulkUpyun) GetFileSize(upyun_path string) (size uint64, err error) { 212 info, err := obj.UpYun.GetFileInfo(upyun_path) 213 if err != nil { 214 return 215 } 216 size, err = strconv.ParseUint(info["size"], 10, 64) 217 if err != nil { 218 return 219 } 220 return 221 } 222 223 func (obj *BulkUpyun) GetFileDate(upyun_path string) (date time.Time, err error) { 224 info, err := obj.UpYun.GetFileInfo(upyun_path) 225 if err != nil { 226 return 227 } 228 date, err = time.Parse(time.RFC1123, info["date"]) 229 if err != nil { 230 return 231 } 232 return 233 }