This function helps us find all the files in a given directory and its subdirectories.
(dirPth string)
| 135 | |
| 136 | // This function helps us find all the files in a given directory and its subdirectories. |
| 137 | func getAllFiles(dirPth string) (files []string, err error) { |
| 138 | var dirs []string |
| 139 | dir, err := os.ReadDir(dirPth) |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | pathSeparator := string(os.PathSeparator) |
| 144 | |
| 145 | // Now, we go through each item in the directory. |
| 146 | for _, fi := range dir { |
| 147 | // If the item is a directory, we add it to our list of directories to check later. |
| 148 | if fi.IsDir() { |
| 149 | dirs = append(dirs, dirPth+pathSeparator+fi.Name()) |
| 150 | // We also call this function again to check inside this subdirectory. |
| 151 | temp, _ := getAllFiles(dirPth + pathSeparator + fi.Name()) |
| 152 | files = append(files, temp...) |
| 153 | } else { |
| 154 | // If the item is a file, we add its path to our list of files. |
| 155 | files = append(files, dirPth+pathSeparator+fi.Name()) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Now, we go through each subdirectory we found and get all the files inside them. |
| 160 | for _, table := range dirs { |
| 161 | temp, _ := getAllFiles(table) |
| 162 | files = append(files, temp...) |
| 163 | } |
| 164 | return files, nil |
| 165 | } |
| 166 | |
| 167 | // This function creates a new HTTP request for file upload. |
| 168 | // It takes in the URI of the server, the authentication key, parameters, the name of the parameter, and the path of the file. |