github.com/vulppine/fotoDen@v0.3.0/tool/init.go (about) 1 package tool 2 3 import ( 4 // "crypto/md5" 5 "fmt" 6 // "io/ioutil" 7 // "log" 8 "os" 9 "path" 10 "path/filepath" 11 12 "github.com/vulppine/fotoDen/generator" 13 ) 14 15 /* Disabled in favor of zipped and embed themes 16 func CopyThemeToConfig(srcpath string) error { 17 wd, _ := os.Getwd() 18 srcpath, err := filepath.Abs(srcpath) 19 t, err := ReadThemeConfig(path.Join(srcpath, "theme.json")) 20 tpath, err := filepath.Abs(path.Join(generator.RootConfigDir, "theme", t.ThemeName)) 21 err = os.MkdirAll(tpath, 0755) 22 if checkError(err) { 23 return err 24 } 25 26 err = os.Mkdir(filepath.Join(tpath, "html"), 0755) 27 if checkError(err) { 28 return err 29 } 30 31 err = generator.CopyFile(path.Join(srcpath, "theme.json"), "theme.json", tpath) 32 err = generator.CopyFile( 33 path.Join(srcpath, "html", "photo-template.html"), 34 "photo-template.html", 35 path.Join(tpath, "html")) 36 err = generator.CopyFile( 37 path.Join(srcpath, "html", "album-template.html"), 38 "album-template.html", 39 path.Join(tpath, "html")) 40 err = generator.CopyFile( 41 path.Join(srcpath, "html", "folder-template.html"), 42 "folder-template.html", 43 path.Join(tpath, "html")) 44 if checkError(err) { 45 return err 46 } 47 48 if len(t.Stylesheets) != 0 { 49 err = os.Mkdir( 50 path.Join(tpath, "css"), 0755) 51 if checkError(err) { 52 return err 53 } 54 55 os.Chdir(path.Join(srcpath, "css")) 56 err = generator.BatchCopyFile( 57 t.Stylesheets, 58 path.Join(tpath, "css")) 59 checkError(err) 60 os.Chdir(wd) 61 } 62 63 if len(t.Scripts) != 0 { 64 err = os.Mkdir( 65 path.Join(tpath, "js"), 0755) 66 if checkError(err) { 67 return err 68 } 69 70 os.Chdir(path.Join(srcpath, "js")) 71 err = generator.BatchCopyFile( 72 t.Scripts, path.Join(tpath, "js")) 73 checkError(err) 74 os.Chdir(wd) 75 } 76 77 if len(t.Other) != 0 { 78 err = os.Mkdir( 79 path.Join(tpath, "other"), 0755) 80 if checkError(err) { 81 return err 82 } 83 84 os.Chdir(path.Join(srcpath, "etc")) 85 err = generator.BatchCopyFile( 86 t.Other, path.Join(tpath, "etc")) 87 checkError(err) 88 os.Chdir(wd) 89 } 90 91 if !fileCheck(path.Join(generator.RootConfigDir, "defaulttheme")) { 92 f, err := os.Create(path.Join(generator.RootConfigDir, "defaulttheme")) 93 if checkError(err) { 94 return err 95 } 96 f.WriteString(t.ThemeName) 97 f.Close() 98 } 99 100 return nil 101 } 102 103 // InitializeWebTheme sets up web templates according to a given URL, and path containing templates. 104 // All templates should be labelled with [photo, album, folder]-template.html. 105 func InitializeWebTheme(u string, srcpath string, dest string) error { 106 wd, _ := os.Getwd() 107 108 t, err := ReadThemeConfig(path.Join(srcpath, "theme.json")) 109 webvars, err := generator.NewWebVars(u) 110 checkError(err) 111 112 tpath, err := filepath.Abs(path.Join(dest, "theme", t.ThemeName)) 113 err = os.MkdirAll(tpath, 0755) 114 if checkError(err) { 115 return err 116 } 117 118 err = generator.CopyFile(path.Join(srcpath, "theme.json"), "theme.json", tpath) 119 120 err = os.Mkdir( 121 path.Join(tpath, "html"), 0755) 122 if checkError(err) { 123 return err 124 } 125 126 err = generator.ConfigureWebFile( 127 path.Join(srcpath, "html", "photo-template.html"), 128 path.Join( 129 tpath, 130 "html", 131 "photo-template.html"), 132 webvars) 133 checkError(err) 134 135 err = generator.ConfigureWebFile( 136 path.Join(srcpath, "html", "album-template.html"), 137 path.Join( 138 tpath, 139 "html", 140 "album-template.html"), 141 webvars) 142 checkError(err) 143 144 err = generator.ConfigureWebFile( 145 path.Join(srcpath, "html", "folder-template.html"), 146 path.Join( 147 tpath, 148 "html", 149 "folder-template.html"), 150 webvars) 151 checkError(err) 152 153 if len(t.Stylesheets) != 0 { 154 err = os.Mkdir( 155 path.Join(tpath, "css"), 0755) 156 if checkError(err) { 157 return err 158 } 159 160 os.Chdir(path.Join(srcpath, "css")) 161 err = generator.BatchCopyFile( 162 t.Stylesheets, 163 path.Join(tpath, "css")) 164 checkError(err) 165 os.Chdir(wd) 166 } 167 168 if len(t.Scripts) != 0 { 169 err = os.Mkdir( 170 path.Join(tpath, "js"), 0755) 171 if checkError(err) { 172 return err 173 } 174 175 os.Chdir(path.Join(srcpath, "js")) 176 err = generator.BatchCopyFile( 177 t.Scripts, path.Join(tpath, "js")) 178 checkError(err) 179 os.Chdir(wd) 180 } 181 182 return nil 183 } 184 */ 185 186 /* Disabled, see embedjs.go for its replacement. 187 var ( 188 // The MD5 checksum of fotoDen.js. Must be defined during build. 189 JSSum string 190 // The MD5 checksum of fotoDen.min.js. Must be defined during build. 191 JSMinSum string 192 ) 193 194 // InitializefotoDenjs copies over a valid fotoDen.js into the configuration's web directory. 195 // The JS must be validated during build, otherwise it will warn the user that the script cannot be validated. 196 func InitializefotoDenjs(fpath string) error { 197 if JSSum != "" || JSMinSum != "" { 198 f, err := ioutil.ReadFile(fpath) 199 if checkError(err) { 200 return err 201 } 202 s := fmt.Sprintf("%x", md5.Sum(f)) 203 if s != JSSum && s != JSMinSum { 204 verbose("checksum: " + s) 205 verbose("valid checksums:") 206 verbose("fotoDen.js: " + JSSum) 207 verbose("fotoDen.min.js: " + JSMinSum) 208 if !ReadInputAsBool("Warning: the selected JS file may have been modified, or is not fotoDen.js. Continue?", "y") { 209 return fmt.Errorf("js not valid") 210 } 211 } 212 } else { 213 log.Println("Warning: fotoDen tool was not compiled with valid JS checksums. Ensure that your scripts are safe, or are valid for fotoDen websites.") 214 } 215 216 err := generator.CopyFile(path.Join(fpath), "fotoDen.js", generator.RootConfigDir) 217 if checkError(err) { 218 return err 219 } 220 221 return nil 222 } 223 */ 224 225 // isEmbed says if fotoDen.js is embed or not, 226 // and is automatically set if embed.go is 227 // included from the build flag +embed 228 var isEmbed bool 229 230 // WebsiteConfig represents a struct that contains 231 // everything needed for the fotoDen generator to work. 232 type WebsiteConfig struct { 233 Name string 234 RootLocation string 235 Theme string 236 URL string 237 GeneratorConfig generator.Config 238 } 239 240 // InitializefotoDenRoot sets up the root directory for fotoDen, including a folderInfo.json file. 241 // Creates the folder structure, 242 // copies over the folder page as well as the theme.css file, 243 // and copies over fotoDen.js, 244 // and then generates a folderInfo.json file according to the name given in the command line, 245 // otherwise generates with a blank name. 246 // 247 // By default, the root of a generated fotoDen website should be specifically a folder. 248 // A fotoDen page can be anything, as long as the required tags are there, 249 // but if it is being generated via this tool, 250 // it is, by default, a folder. 251 // 252 // TODO: Break this apart into smaller chunks, holy fuck 253 // - Done, technically. Disabling several pieces and replacing them 254 // with more efficient, (hopefully?) user friendly methods of 255 // managing website resources should help with readability. 256 func InitializefotoDenRoot(rootpath string, webconfig WebsiteConfig) error { 257 rootpath, _ = filepath.Abs(rootpath) 258 259 err := GenerateWebRoot(rootpath) 260 if checkError(err) { 261 panic(err) 262 } 263 264 var w *generator.WebConfig 265 266 var t *theme 267 if fileCheck(webconfig.Theme) { 268 verbose("attempting to open given theme as a zip file") 269 z, zs, err := zipFileReader(webconfig.Theme) 270 if checkError(err) { 271 return err 272 } 273 274 t, err = openTheme(z, zs) 275 if checkError(err) { 276 return err 277 } 278 } else if isEmbed { 279 verbose("no theme given, attempting to use internal default") 280 t, err = openTheme(defaultThemeZipReader(), defaultThemeZipLen) 281 if checkError(err) { 282 return err 283 } 284 } else { 285 return fmt.Errorf("could not find theme, aborting") 286 } 287 288 if !fileCheck(path.Join(generator.RootConfigDir, "defaulttheme")) { 289 f, err := os.Create(path.Join(generator.RootConfigDir, "defaulttheme")) 290 if checkError(err) { 291 return err 292 } 293 f.WriteString(t.s.ThemeName) 294 f.Close() 295 } 296 297 /* 298 if webconfig.Theme == "" { 299 if fileCheck(path.Join(generator.RootConfigDir, "defaulttheme")) { 300 f, err := os.Open(path.Join(generator.RootConfigDir, "defaulttheme")) 301 d, err := ioutil.ReadAll(f) 302 if checkError(err) { 303 return err 304 } 305 306 webconfig.Theme = string(d) 307 } else { 308 return fmt.Errorf("theme does not exist, cannot continue generation") 309 } 310 } 311 */ 312 313 if WizardFlag == true { 314 webconfig, w = setupWebsite(rootpath, t.s.ThemeName) 315 generator.CurrentConfig = webconfig.GeneratorConfig 316 } else { 317 webconfig.RootLocation = rootpath 318 webconfig.GeneratorConfig = generator.DefaultConfig 319 webconfig.GeneratorConfig.WebBaseURL = webconfig.URL 320 if webconfig.Name == "" { 321 verbose("Name not specified, using base of given path.") 322 webconfig.Name = filepath.Base(rootpath) 323 verbose(webconfig.Name) 324 } 325 webconfig.GeneratorConfig.WebSourceLocation, err = filepath.Abs( 326 path.Join( 327 generator.RootConfigDir, 328 "sites", 329 webconfig.Name, 330 "theme", 331 t.s.ThemeName, 332 )) 333 generator.CurrentConfig = webconfig.GeneratorConfig 334 w = generator.GenerateWebConfig(URLFlag) 335 w.Theme = true // we're generating this from fotoDen tool, so we're using a theme obviously 336 w.WebsiteTitle = webconfig.Name 337 w.ImageRootDir = webconfig.GeneratorConfig.ImageRootDirectory 338 w.ThumbnailFrom = w.ImageSizes[0].SizeName 339 w.DisplayImageFrom = w.ImageSizes[len(w.ImageSizes)-1].SizeName 340 for _, v := range w.ImageSizes { 341 w.DownloadSizes = append(w.DownloadSizes, v.SizeName) 342 } 343 344 if URLFlag == "" { 345 fmt.Printf("You will have to configure your photo storage provider in %v.\n", path.Join(rootpath, "config.json")) 346 } 347 } 348 349 spath := path.Join(generator.RootConfigDir, "sites", webconfig.Name) 350 verbose("Creating site config directory at: " + spath) 351 err = os.MkdirAll(spath, 0755) 352 if checkError(err) { 353 return err 354 } 355 356 err = generator.WriteJSON(path.Join(spath, "config.json"), "multi", webconfig) 357 if checkError(err) { 358 return err 359 } 360 361 generator.CurrentConfig = webconfig.GeneratorConfig 362 363 /* 364 err = InitializeWebTheme( 365 webconfig.URL, 366 path.Join(generator.RootConfigDir, "theme", webconfig.Theme), 367 path.Join(spath), 368 ) 369 */ 370 371 err = w.WriteWebConfig(path.Join(rootpath, "config.json")) 372 if checkError(err) { 373 return err 374 } 375 376 if isEmbed { 377 verbose("copying fotoDen.js from internal embed file") 378 err = writefotoDenJS(path.Join(rootpath, "js", "fotoDen.js")) 379 checkError(err) 380 } else { 381 verbose("copying fotoDen.js from config dir") 382 err = generator.CopyFile(path.Join(generator.RootConfigDir, "fotoDen.js"), path.Join(rootpath, "js", "fotoDen.js")) 383 checkError(err) 384 } 385 386 t.initTheme( 387 webconfig.URL, 388 path.Join(spath, "theme", t.s.ThemeName), // change this 389 path.Join(rootpath, "theme"), 390 ) 391 392 /* 393 tpath := path.Join(spath, "theme", webconfig.Theme) 394 t, err := ReadThemeConfig(path.Join(tpath, "theme.json")) 395 if checkError(err) { 396 return err 397 } 398 */ 399 400 // wd, _ := os.Getwd() 401 402 /* 403 os.Chdir(tpath) 404 if len(t.Stylesheets) != 0 { 405 os.Chdir("css") 406 err = generator.BatchCopyFile(t.Stylesheets, path.Join(rootpath, "theme", "css")) 407 if checkError(err) { 408 return err 409 } 410 os.Chdir(tpath) 411 } 412 413 if len(t.Scripts) != 0 { 414 os.Chdir("js") 415 err = generator.BatchCopyFile(t.Scripts, path.Join(rootpath, "theme", "js")) 416 if checkError(err) { 417 return err 418 } 419 os.Chdir(tpath) 420 } 421 422 if len(t.Other) != 0 { 423 os.Chdir("etc") 424 err = generator.BatchCopyFile(t.Other, path.Join(rootpath, "theme", "etc")) 425 if checkError(err) { 426 return err 427 } 428 } 429 */ 430 431 // os.Chdir(wd) 432 433 folder, err := generator.GenerateFolderInfo(rootpath, w.WebsiteTitle) // do it in rootpath since we're not trying to scan for images in the current folder 434 folder.Type = "folder" 435 checkError(err) 436 err = folder.WriteFolderInfo(path.Join(rootpath, "folderInfo.json")) 437 checkError(err) 438 439 // err = GenerateWeb("folder", rootpath, folder, Genoptions) 440 err = t.generateWeb("folder", rootpath, nil) 441 checkError(err) 442 443 if !fileCheck(path.Join(generator.RootConfigDir, "defaultsite")) { 444 f, err := os.Create(path.Join(generator.RootConfigDir, "defaultsite")) 445 if checkError(err) { 446 return err 447 } 448 f.WriteString(webconfig.Name) 449 f.Close() 450 } 451 452 return nil 453 } 454 455 /* Deprecated since fotoDen configs are now included at a per-site basis 456 // InitializefotoDenConfig initializes a fotoDen config folder. 457 // 458 // This should only be done once. 459 // 460 // Takes a single string to set WebBaseURL as. 461 func InitializefotoDenConfig(u string, dest string) error { 462 fmt.Println("Initializing fotoDen config with base URL: ", u) 463 464 var config generator.Config 465 466 if WizardFlag == true { 467 config = setupConfig() 468 } else { 469 config = generator.DefaultConfig 470 config.WebBaseURL = u 471 } 472 473 if dest == "" { 474 dest = generator.RootConfigDir 475 } else { 476 dest, err := filepath.Abs(dest) 477 if checkError(err) { 478 return err 479 } 480 config.WebSourceLocation = path.Join(dest, "web") 481 } 482 483 err := os.MkdirAll(config.WebSourceLocation, 0755) 484 if checkError(err) { 485 return err 486 } 487 488 err = generator.WriteConfig(config, path.Join(dest, "config.json")) 489 if checkError(err) { 490 return err 491 } 492 493 return nil 494 } 495 */