parent
8ecc9bd066
commit
bd23448f42
@ -0,0 +1,43 @@ |
||||
package com.apside.assistDbBackend.controller; |
||||
|
||||
import com.apside.assistDbBackend.model.Script; |
||||
import com.apside.assistDbBackend.service.ScriptsService; |
||||
import org.eclipse.jgit.api.errors.GitAPIException; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.web.bind.annotation.*; |
||||
|
||||
import java.io.IOException; |
||||
import java.net.URISyntaxException; |
||||
import java.util.List; |
||||
|
||||
@RestController |
||||
@RequestMapping("/api") |
||||
public class ScriptController { |
||||
@Autowired |
||||
ScriptsService scriptsService; |
||||
|
||||
@GetMapping("/scripts") |
||||
public List<Script> getAllScripts() throws IOException, GitAPIException { |
||||
return scriptsService.retrieveScripts(); |
||||
} |
||||
|
||||
@DeleteMapping("/script/delete/{name}") |
||||
public void deleteScript(@PathVariable("name") final String name){ |
||||
scriptsService.deleteOneScript(name); |
||||
} |
||||
|
||||
|
||||
@GetMapping("/script/add") |
||||
public void addScript(@RequestParam("content") String content, @RequestParam("name") String name) throws IOException, GitAPIException, URISyntaxException { |
||||
scriptsService.addOneScript(content, name); |
||||
} |
||||
|
||||
@GetMapping("/script/edit") |
||||
public void editScript(@RequestParam("content") String content, @RequestParam("defaultname") String defaultName, @RequestParam("newname") String newName) throws IOException, GitAPIException, URISyntaxException { |
||||
scriptsService.simpleDeleteScript(defaultName); |
||||
scriptsService.addOneScript(content, newName); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
@ -0,0 +1,20 @@ |
||||
package com.apside.assistDbBackend.model; |
||||
|
||||
import jakarta.persistence.Entity; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
public class Script { |
||||
private String extension; |
||||
private String name; |
||||
|
||||
private String data; |
||||
|
||||
public Script(){} |
||||
|
||||
public Script(String extension, String name, String data){ |
||||
this.extension = extension; |
||||
this.name = name; |
||||
this.data = data; |
||||
} |
||||
} |
@ -0,0 +1,118 @@ |
||||
package com.apside.assistDbBackend.service; |
||||
|
||||
import com.apside.assistDbBackend.model.Script; |
||||
import lombok.Data; |
||||
import org.eclipse.jgit.api.Git; |
||||
import org.eclipse.jgit.api.RemoteAddCommand; |
||||
import org.eclipse.jgit.api.errors.GitAPIException; |
||||
import org.eclipse.jgit.api.errors.TransportException; |
||||
import org.eclipse.jgit.transport.PushResult; |
||||
import org.eclipse.jgit.transport.URIish; |
||||
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties; |
||||
import org.springframework.stereotype.Repository; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.io.File; |
||||
import java.io.FileWriter; |
||||
import java.io.IOException; |
||||
import java.net.URISyntaxException; |
||||
import java.nio.file.Files; |
||||
import java.nio.file.Path; |
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Iterator; |
||||
import java.util.List; |
||||
|
||||
@Data |
||||
@Service |
||||
public class ScriptsService { |
||||
|
||||
public List<Script> retrieveScripts() throws IOException, GitAPIException { |
||||
pullFromGit(); |
||||
List<Script> listOfScripts = new ArrayList<>(); |
||||
//Creating a File object for directory
|
||||
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts"; |
||||
//List of all files and directories
|
||||
File scriptDirectory = new File(tempDirectoryPath); |
||||
String contents[] = scriptDirectory.list(); |
||||
for(int i=0; i<contents.length; i++) { |
||||
Path filePath = Path.of(tempDirectoryPath + "/" + contents[i]); |
||||
String allData = Files.readString(filePath); |
||||
String extension = contents[i].substring(contents[i].lastIndexOf(".") + 1); |
||||
Script tempScript = new Script(extension, contents[i], allData); |
||||
listOfScripts.add(tempScript); |
||||
//System.out.println(contents[i] + " Extension : " + extension);
|
||||
} |
||||
return listOfScripts; |
||||
} |
||||
|
||||
public void deleteOneScript(final String name){ |
||||
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts"; |
||||
try { |
||||
File scriptDirectory = new File(tempDirectoryPath + "/" + name); |
||||
scriptDirectory.delete(); |
||||
pushToGit(); |
||||
pullFromGit(); |
||||
} catch (Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
public void simpleDeleteScript(final String name){ |
||||
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts"; |
||||
try { |
||||
File scriptDirectory = new File(tempDirectoryPath + "/" + name); |
||||
scriptDirectory.delete(); |
||||
} catch (Exception e){ |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
|
||||
public void addOneScript(final String content, final String name) throws IOException, GitAPIException, URISyntaxException { |
||||
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "/AssistDB_AdditionalFiles/Scripts"; |
||||
File newFile = new File(tempDirectoryPath + "/" + name); |
||||
newFile.createNewFile(); |
||||
FileWriter writerDataFile = new FileWriter(tempDirectoryPath + "/" + name); |
||||
writerDataFile.write(content); |
||||
writerDataFile.close(); |
||||
|
||||
pushToGit(); |
||||
} |
||||
|
||||
|
||||
|
||||
@Value("${USERNAME_GIT}") |
||||
private String userGit; |
||||
|
||||
@Value("${ACCESS_TOKEN_GIT}") |
||||
private String accesToken; |
||||
|
||||
public void pushToGit() throws IOException, GitAPIException, URISyntaxException { |
||||
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles"; |
||||
|
||||
Git git = Git.open(new File(tempDirectoryPath)); |
||||
RemoteAddCommand remoteAddCommand = git.remoteAdd(); |
||||
remoteAddCommand.setName("origin"); |
||||
remoteAddCommand.setUri(new URIish("https://gitea.ci.apside-top.fr/Prudence_Creole/AssistDB_AdditionalFiles.git")); |
||||
// you can add more settings here if needed
|
||||
remoteAddCommand.call(); |
||||
|
||||
git.add().setUpdate(true).addFilepattern(".").call(); |
||||
git.add().addFilepattern(".").call(); |
||||
git.commit().setMessage("commited").call(); |
||||
|
||||
git.push().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).call(); |
||||
|
||||
|
||||
} |
||||
|
||||
public void pullFromGit() throws IOException, GitAPIException { |
||||
String tempDirectoryPath = new File(System.getProperty("user.dir")).getParent() + "\\AssistDB_AdditionalFiles"; |
||||
|
||||
Git git = Git.open(new File(tempDirectoryPath)); |
||||
git.pull().setCredentialsProvider(new UsernamePasswordCredentialsProvider(userGit, accesToken)).setRemote("origin").setRemoteBranchName("main").call(); |
||||
} |
||||
} |
Loading…
Reference in new issue