func readSourceFiles(s session.Store, fileNames []string, fileProjects []string, fileType string) Data { // if user is not logged in return if s.Get("UserName") == nil { return Data{} } name := s.Get("UserName").(string) dir := beego.AppConfig.String("userdata::location") + name + "/" codeFiles := make(map[string]SourceFile) for i := 0; i < len(fileNames); i++ { var ( file *os.File err error ) ///////////////////////////////////////// // Fetch project project := fileProjects[i] status := STATUS_OK if project != "" { _, err := fetchProject(s, project) if err != nil { switch err.Error() { case "uncommitted changes": beego.Warning("Uncommitted changes detected with project", "for user", name) status = STATUS_UNCOMMITTED } } } ///////////////////////////////////////// // Read file var fileName string if project != "" { fileName = dir + beego.AppConfig.String("userdata::projects") + "/" + project + "/" + fileType + "/" + fileNames[i] } else { fileName = dir + fileType + "/" + fileNames[i] } if file, err = os.Open(fileName); err != nil { beego.Error("Cannot open file", fileName) return Data{} } defer file.Close() fileInfo, _ := file.Stat() fileSize := fileInfo.Size() codeFile := make([]byte, fileSize) _, err = file.Read(codeFile) if err != nil { beego.Error("Read error occured.") return Data{} } rights := []string{} users := []string{} if project != "" { rights = models.GetProjectRightsFromDatabase(name, project) users = models.GetProjectUsersFromDatabase(project) } codeFiles[fileNames[i]] = SourceFile{ TimeStamp: fileInfo.ModTime().UnixNano() / int64(time.Millisecond), Code: string(codeFile), Project: project, Rights: rights, Users: users, Status: status, } } return Data{ "CodeFiles": codeFiles, } }
func writeSourceFile(s session.Store, fileName string, project string, fileType string, codeFile string, timeStamp int64, Image string, alternateFile string, alternateType string, overwrite bool) Data { // if user is not logged in return if s.Get("UserName") == nil { return Data{} } T := models.T userName := s.Get("UserName").(string) if project == "/" { project = "" } //////////////////////////////////////// // Retrieve rights and users if it is a project rights := []string{} users := []string{} if project != "" { rights = models.GetProjectRightsFromDatabase(userName, project) users = models.GetProjectUsersFromDatabase(project) beego.Warning("writeSourceFile:", userName, project, rights, users) } if project != "" && !models.CheckRight(userName, project, "Write") { return Data{ "Error": "Insufficient rights for project " + project, } } var dir string if project != "" { dir = beego.AppConfig.String("userdata::location") + userName + "/" + beego.AppConfig.String("userdata::projects") + "/" + project + "/" + fileType + "/" } else { dir = beego.AppConfig.String("userdata::location") + userName + "/" + fileType + "/" } var ( file *os.File err error savedTimeStamp int64 ) filePath := dir + fileName ///////////////////////////////////////// // Check if directory is there and create if not // fileStat, err := os.Stat(filePath) if os.IsNotExist(err) { if err = os.MkdirAll(dir, os.ModePerm); err != nil { beego.Error("Cannot create directory", dir) return Data{} } } else if err != nil { beego.Error("Error while checking for directory", dir) return Data{} } ///////////////////////////////////////// // Don't overwrite file unintendedly fileStat, err = os.Stat(filePath) if !overwrite { if !os.IsNotExist(err) { return Data{ "Error": T["websockets_file_exists"], } } else if err == nil { time := fileStat.ModTime().UnixNano() / int64(time.Millisecond) // Look if the file changed on disk since last writing if time > timeStamp { return Data{ "OutdatedTimeStamp": time, } } } } ///////////////////////////////////////// // Also check if alternate file type (png, mp3) would be overwritten altSubDir := "" switch alternateType { case "png": altSubDir = beego.AppConfig.String("userdata::imagefiles") case "mp3": altSubDir = beego.AppConfig.String("userdata::soundfiles") } altDir := dir[:len(dir)-len(fileType)-1] + altSubDir + "/" altFileName := altDir + fileName[:len(fileName)-len(fileType)] + alternateType if alternateType != "" { fileStat, err = os.Stat(altFileName) if !overwrite { if !os.IsNotExist(err) { return Data{ "Error": T["websockets_file_exists"], } } } } beego.Warning("altDir:", altDir, "altFileName:", altFileName) ///////////////////////////////////////// // Create/overwrite file if file, err = os.Create(filePath); err != nil { beego.Error("Cannot create or overwrite file", filePath) return Data{} } defer file.Close() _, err = file.Write([]byte(codeFile)) //////////////////////////////////////// // Record timestamps for web app if err == nil { fileStat, _ = file.Stat() savedTimeStamp = fileStat.ModTime().UnixNano() / int64(time.Millisecond) } else { beego.Error("Cannot write to file", filePath) return Data{} } //////////////////////////////////////// // Create image file createImageFile(Image, filePath) if alternateType == "png" { createImageFile(alternateFile, altFileName) } else if alternateType == "mp3" { // create sound file } return Data{ "SavedTimeStamp": savedTimeStamp, "Rights": rights, "Users": users, } }