Esempio n. 1
0
func (this DocsController) CreateNewFrontPageDoc(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	var err error

	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", "Unknown Error"}
	defer rest_utils.WriteResultData(rw, &ResultData)

	DocName := params.ByName("name")

	// create an empty directory for the new page.
	PageFilePath := getDocTextFilePath(this.DocRoot, DocName)
	err = createNewPage(PageFilePath)
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "CannotCreateNewPage"
		ResultData.ErrorMsg = err.Error()
		return
	}

	// open the page data base.
	PageDir := getDocDirectory(this.DocRoot, DocName)
	PageDB, err := openPageDatabase(DocName, PageDir)
	defer closePageDatabase(PageDB)
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "CannotOpenPageDatabase"
		ResultData.ErrorMsg = err.Error()
		return
	}

	// set front page attribute.
	err = writePageAttribute(PageDB, "IsOnFrontPage", "true")
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "DatabaseError"
		ResultData.ErrorMsg = err.Error()
		return
	}

	// TODO:HIGH should update the page summary here too.

	//== return the correct AJAX result. ==
	ResultData.IsSuccess = true
	ResultData.ErrorType = ""
	ResultData.ErrorMsg = ""
}
Esempio n. 2
0
func (this CommandsController) OpenFile(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", "Unknown Error"}
	defer rest_utils.WriteResultData(rw, &ResultData)

	DocName := params.ByName("DocName")
	if DocName == "" {
		panic("DocName can not be empty.")
	}
	TargetFilePath := params.ByName("filepath")
	if TargetFilePath == "" {
		panic("TargetFilePath can not be empty.")
	}

	rootDir, err := malkovich.GetWikiPageDirectory(DocName)
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "CannotGetWikiDirectory"
		ResultData.ErrorMsg = err.Error()
		return
	}
	fp := filepath.Join(rootDir, `files`, TargetFilePath)

	log.Infof("OpenFile %s", fp)

	if fs_utils.Exists(fp) != true {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "FileDoesNotExist"
		ResultData.ErrorMsg = err.Error()
		return
	}

	cmd := exec.Command("rundll32.exe", "url.dll,FileProtocolHandler", fp)
	err = cmd.Run()
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "CannotOpenFile"
		ResultData.ErrorMsg = err.Error()
		return
	}

	//== return the correct AJAX result. ==
	ResultData.IsSuccess = true
	ResultData.ErrorType = ""
	ResultData.ErrorMsg = ""
}
Esempio n. 3
0
func (this DocsController) UpdateDoc(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	var err error

	RequestData := struct {
		DocText string
	}{}

	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", "Unknown Error"}
	defer rest_utils.WriteResultData(rw, &ResultData)

	//=== Update the page content ====
	// TODO:MED replace with rest_utils readrequestdata.
	decoder := json.NewDecoder(req.Body)
	err = decoder.Decode(&RequestData)
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "CannotReadRequestBody"
		ResultData.ErrorMsg = err.Error()
		return
	}

	DocName := params.ByName("name")
	PageFilePath := getDocTextFilePath(this.DocRoot, DocName)
	err = updatePageContent(DocName, PageFilePath, RequestData.DocText)
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "CannotUpdatePage"
		ResultData.ErrorMsg = err.Error()
		return
	}

	this.UpdateDocPreview(DocName)

	//== return the correct AJAX result. ==
	ResultData.IsSuccess = true
	ResultData.ErrorType = ""
	ResultData.ErrorMsg = ""
}
Esempio n. 4
0
func (this DocsController) DeleteDoc(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", "Unknown Error"}
	defer rest_utils.WriteResultData(rw, &ResultData)

	DocName := params.ByName("name")
	DocDir := getDocDirectory(this.DocRoot, DocName)
	log.Info("delete: " + DocDir)
	err := os.RemoveAll(DocDir)
	//TODO:HIGH ensure the file is contained under the DocRoot.
	if err != nil {
		ResultData.IsSuccess = false
		ResultData.ErrorType = "DeleteFail"
		ResultData.ErrorMsg = err.Error()
	} else {
		ResultData.IsSuccess = true
		ResultData.ErrorType = ""
		ResultData.ErrorMsg = ""
	}
}
Esempio n. 5
0
func (this *FilesController) DeleteFile(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", ""}
	defer func() {
		if ResultData.IsSuccess == false {
			log.Error("File Delete Error: " + ResultData.ErrorType)
			log.Error(ResultData.ErrorMsg)
		}
		rest_utils.WriteResultData(rw, &ResultData)
	}()

	DocName := params.ByName("DocName")
	if DocName == "" {
		ResultData.ErrorType = "DocNotFound"
		ResultData.ErrorMsg = "DocName value is empty. Cannot not save file without reference to owner document."
		return
	}

	DocFileRoot := this.GetDocFilesRoot(DocName)
	FullFilePath := path.Join(DocFileRoot, params.ByName("filepath"))
	FullFilePath = filepath.ToSlash(FullFilePath)
	log.Info("FilePath " + FullFilePath)

	err := os.Remove(FullFilePath)
	if err != nil {
		ResultData.ErrorType = "DeleteError"
		ResultData.ErrorMsg = err.Error()
		return
	}

	// If we make it this far...
	ResultData.IsSuccess = true
}
Esempio n. 6
0
func (this *FilesController) ShowFile(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", ""}
	defer func() {
		if ResultData.IsSuccess == false {
			log.Error("File Delete Error: " + ResultData.ErrorType)
			log.Error(ResultData.ErrorMsg)
		}
		rest_utils.WriteResultData(rw, &ResultData)
	}()

	DocName := params.ByName("DocName")
	if DocName == "" {
		panic("DocName can not be empty.")
	}

	DocFileRoot := this.GetDocFilesRoot(DocName)
	fp := path.Join(DocFileRoot, params.ByName("filepath"))
	fp = filepath.ToSlash(fp)
	fp = path.Clean(fp)
	fp = filepath.FromSlash(fp)
	log.Info(fp)

	// TODO:HIGH should check if the file exists on disc and
	// return an error if the file doesn't exist.

	if runtime.GOOS == "windows" {
		// NOTE: HACK: The command commented out below is to
		// show the referenced file using Windows Explorer.
		// For some reason, the code when run returns a 'status 1`
		// error and causes the application to exit. I'm not sure
		// why and can't figure it out. This answer below advises
		// on how to debug exec.Command() calls but the error message
		// is empty. It's likely I'm doing something wrong but can't
		// see what.
		//
		// How to debug exit code 1.
		// http://stackoverflow.com/q/18159704/395461
		//
		// Instead of fixing the problem, I've added a hacky work around.
		// the `cmd /k` forces the command to be run in a new console window.
		// It seems to work so will do for now.
		//
		//cmd := exec.Command(`explorer`, `/select,`, fp)
		cmd := exec.Command(`cmd`, `/k`, `explorer`, `/select,`, fp)
		var out bytes.Buffer
		var stderr bytes.Buffer
		cmd.Stdout = &out
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			log.Fatal(`Error showing file. ` + err.Error() + ` ` + stderr.String())
			ResultData.IsSuccess = false
			ResultData.ErrorType = "CannotShowFile"
			ResultData.ErrorMsg = err.Error() + ` ` + stderr.String()
			return
		}
	}

	// If we make it this far the file has been uploaded successfully.
	ResultData.IsSuccess = true
	ResultData.ErrorType = ""
	ResultData.ErrorMsg = ""
}
Esempio n. 7
0
func (this *FilesController) UploadFile(rw http.ResponseWriter, req *http.Request, params mux.Params) {
	// Upload file handler in Golang.
	// https://www.socketloop.com/tutorials/golang-upload-file

	time.Sleep(1 * time.Millisecond)

	ResultData := struct {
		IsSuccess bool
		ErrorType string
		ErrorMsg  string
	}{false, "Unknown", ""}
	defer func() {
		if ResultData.IsSuccess == false {
			log.Error("Upload Error: " + ResultData.ErrorType)
			log.Error(ResultData.ErrorMsg)
		}
		rest_utils.WriteResultData(rw, &ResultData)
	}()

	// the FormFile function takes in the POST input id file
	file, header, err := req.FormFile("myFile")
	if err != nil {
		ResultData.ErrorType = "UploadError"
		ResultData.ErrorMsg = err.Error()
		return
	}
	defer file.Close()

	DocName := req.FormValue("DocName")
	if DocName == "" {
		ResultData.ErrorType = "UploadError"
		ResultData.ErrorMsg = "DocName value is empty. Cannot not save file without reference to owner document."
		return
	}

	DocFileRoot := this.GetDocFilesRoot(DocName)
	os.MkdirAll(DocFileRoot, 0666) //TODO:HIGH what should the permission value be here.

	fn := filepath.Join(DocFileRoot, header.Filename)
	log.Info("Out FN: " + fn)

	out, err := os.Create(fn)
	if err != nil {
		ResultData.ErrorType = "UploadError"
		ResultData.ErrorMsg = err.Error()
		return
	}
	defer out.Close()

	// write the content from POST to the file
	_, err = io.Copy(out, file)
	if err != nil {
		ResultData.ErrorType = "UploadError"
		ResultData.ErrorMsg = err.Error()
		return
	}

	// If we make it this far the file has been uploaded successfully.
	ResultData.IsSuccess = true
	ResultData.ErrorType = ""
	ResultData.ErrorMsg = ""
}