use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method writeContent.
@Override
public String writeContent(String site, String path, InputStream content) {
// Write content to git and commit it
String commitId = null;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repo != null) {
if (helper.writeFile(repo, site, path, content)) {
PersonIdent user = helper.getCurrentUserIdent();
String username = securityService.getCurrentUser();
String comment = helper.getCommitMessage(REPO_SANDBOX_WRITE_COMMIT_MESSAGE).replace(REPO_COMMIT_MESSAGE_USERNAME_VAR, username).replace(REPO_COMMIT_MESSAGE_PATH_VAR, path);
commitId = helper.commitFile(repo, site, path, comment, user);
} else {
logger.error("Failed to write content site: " + site + " path: " + path);
}
} else {
logger.error("Missing repository during write for site: " + site + " path: " + path);
}
}
} catch (ServiceLayerException | UserNotFoundException | CryptoException e) {
logger.error("Unknown service error during write for site: " + site + " path: " + path, e);
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method deleteContent.
@Override
public String deleteContent(String site, String path, String approver) {
String commitId = null;
boolean isPage = path.endsWith(FILE_SEPARATOR + INDEX_FILE);
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
try (Git git = new Git(repo)) {
String pathToDelete = helper.getGitPath(path);
Path parentToDelete = Paths.get(pathToDelete).getParent();
RmCommand rmCommand = git.rm().addFilepattern(pathToDelete).setCached(false);
retryingRepositoryOperationFacade.call(rmCommand);
String pathToCommit = pathToDelete;
if (isPage) {
pathToCommit = deleteParentFolder(git, parentToDelete, true);
}
// TODO: SJ: we need to define messages in a string table of sorts
commitId = helper.commitFile(repo, site, pathToCommit, helper.getCommitMessage(REPO_DELETE_CONTENT_COMMIT_MESSAGE).replaceAll(PATTERN_PATH, path), StringUtils.isEmpty(approver) ? helper.getCurrentUserIdent() : helper.getAuthorIdent(approver));
}
}
} catch (GitAPIException | UserNotFoundException | IOException e) {
logger.error("Error while deleting content for site: " + site + " path: " + path, e);
} catch (ServiceLayerException | CryptoException e) {
logger.error("Unknown service error during delete for site: " + site + " path: " + path, e);
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method createFolder.
@Override
public String createFolder(String site, String path, String name) {
// SJ: Git doesn't care about empty folders, so we will create the folders and put a 0 byte file in them
String commitId = null;
boolean result;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX)) {
Path emptyFilePath = Paths.get(path, name, EMPTY_FILE);
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
try {
// Create basic file
File file = new File(repo.getDirectory().getParent(), emptyFilePath.toString());
// Create parent folders
File folder = file.getParentFile();
if (folder != null) {
if (!folder.exists()) {
folder.mkdirs();
}
}
// Create the file
if (!file.createNewFile()) {
logger.error("error writing file: site: " + site + " path: " + emptyFilePath);
result = false;
} else {
// Add the file to git
try (Git git = new Git(repo)) {
AddCommand addCommand = git.add().addFilepattern(helper.getGitPath(emptyFilePath.toString()));
retryingRepositoryOperationFacade.call(addCommand);
git.close();
result = true;
} catch (GitAPIException e) {
logger.error("error adding file to git: site: " + site + " path: " + emptyFilePath, e);
result = false;
}
}
} catch (IOException e) {
logger.error("error writing file: site: " + site + " path: " + emptyFilePath, e);
result = false;
}
if (result) {
try {
commitId = helper.commitFile(repo, site, emptyFilePath.toString(), helper.getCommitMessage(REPO_CREATE_FOLDER_COMMIT_MESSAGE).replaceAll(PATTERN_SITE, site).replaceAll(PATTERN_PATH, path + FILE_SEPARATOR + name), helper.getCurrentUserIdent());
} catch (ServiceLayerException | UserNotFoundException e) {
logger.error("Unknown service error during commit for site: " + site + " path: " + emptyFilePath, e);
}
}
}
} catch (CryptoException e) {
logger.error("Unexpected service error creating folder " + name + " for site: " + site + " path: " + path, e);
} finally {
generalLockService.unlock(gitLockKey);
}
return commitId;
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method createVersion.
@Override
public String createVersion(String site, String path, String comment, boolean majorVersion) {
// SJ: Will ignore minor revisions since git handles that via write/commit
// SJ: Major revisions become git tags
// TODO: SJ: Redesign/refactor the whole approach in 3.1+
String toReturn = EMPTY;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
if (majorVersion) {
synchronized (helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : PUBLISHED)) {
Repository repo = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : PUBLISHED);
// Tag the repository with a date-time based version label
String gitPath = helper.getGitPath(path);
try (Git git = new Git(repo)) {
PersonIdent currentUserIdent = helper.getCurrentUserIdent();
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HHmmssX");
Calendar cal = Calendar.getInstance();
String versionLabel = dateFormat.format(cal.getTime());
TagCommand tagCommand = git.tag().setName(versionLabel).setMessage(comment).setTagger(currentUserIdent);
retryingRepositoryOperationFacade.call(tagCommand);
toReturn = versionLabel;
} catch (GitAPIException | ServiceLayerException | UserNotFoundException err) {
logger.error("error creating new version for site: " + site + " path: " + path, err);
}
}
} else {
logger.info("request to create minor revision ignored for site: " + site + " path: " + path);
}
} catch (CryptoException e) {
logger.error("Unexpected error creating new version for site: " + site + " path: " + path, e);
} finally {
generalLockService.unlock(gitLockKey);
}
return toReturn;
}
use of org.craftercms.studio.api.v1.constant.StudioConstants.PATTERN_SITE in project studio by craftercms.
the class GitContentRepository method deleteSite.
@Override
public boolean deleteSite(String site) {
boolean toReturn;
String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
generalLockService.lock(gitLockKey);
try {
GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
Repository repository = helper.getRepository(site, StringUtils.isEmpty(site) ? GLOBAL : SANDBOX);
if (repository != null) {
synchronized (repository) {
Repository publishedRepository = helper.getRepository(site, PUBLISHED);
if (publishedRepository != null) {
synchronized (publishedRepository) {
toReturn = helper.deleteSiteGitRepo(site);
}
} else {
toReturn = helper.deleteSiteGitRepo(site);
}
}
} else {
Path sitePath = Paths.get(studioConfiguration.getProperty(REPO_BASE_PATH), studioConfiguration.getProperty(SITES_REPOS_PATH), site);
try {
FileUtils.deleteDirectory(sitePath.toFile());
toReturn = true;
} catch (IOException e) {
logger.error("Error while deleting site " + site, e);
toReturn = false;
}
}
} catch (CryptoException e) {
logger.error("Error while deleting site " + site, e);
toReturn = false;
} finally {
generalLockService.unlock(gitLockKey);
}
return toReturn;
}
Aggregations