Skip to content

Commit

Permalink
Fix theme updation error (#1217)
Browse files Browse the repository at this point in the history
* Make rest controller loggable

* Refactor pull from git process

* Replace Callback interface with Consumer

* Tag theme fetch apis and services deprecated

* Add getAllBranchesTest

* Refactor theme fetcher partially

* Refactor theme property scanner

* Add ThemeFetcherComposite

* Add InputStreamThemeFetcher

* Accomplish multipart zip file theme fetcher

* Reformat ThemeServiceImpl

* Reformat codes

* Provide ThemeRepository

* Complete MultipartFileThemeUpdater

* Make CommonsMultipartResolver support put request method

* Replace some methods with ThemeRepository

* Add GitThemeUpdater

* Add merge two local repo test

* Refine merge process with two repos

* Add more test entry point in GitTest

* Add shutdown hook after creating temporary directory

* Add test: find commit by tag

* Refactor git clone process in GitThemeFetcher

* Refine merge process of two repo

* Make sure that RevWalk closed

* Fix FileUtils#findRootPath bug

* Add clean task before gradle check

* Add fallback theme fetcher

* Disable logback-test.xml

* Set testLogging.showStandardStreams with true

* Fix test error while missing halo-test folder

* Enhance git theme fetcher

* Add copy hidden folder test

* Refine GitThemeFetcherTest

* Accomplish GitThemeUpdater

* Accomplish theme update

* Fix checkstyle error

* Add more deprecated details
  • Loading branch information
JohnNiang authored Jan 26, 2021
1 parent eaa3a80 commit 30c9baf
Show file tree
Hide file tree
Showing 32 changed files with 1,721 additions and 674 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,5 @@ dependencies {

test {
useJUnitPlatform()
testLogging.showStandardStreams = true
}
15 changes: 14 additions & 1 deletion src/main/java/run/halo/app/config/HaloMvcConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.servlet.MultipartConfigElement;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.servlet.ServletRequestContext;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
Expand All @@ -32,6 +35,7 @@
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.lang.NonNull;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
Expand Down Expand Up @@ -118,7 +122,16 @@ FreeMarkerConfigurer freemarkerConfig(HaloProperties haloProperties)
@Bean(name = "multipartResolver")
MultipartResolver multipartResolver(MultipartProperties multipartProperties) {
MultipartConfigElement multipartConfigElement = multipartProperties.createMultipartConfig();
CommonsMultipartResolver resolver = new CommonsMultipartResolver();
CommonsMultipartResolver resolver = new CommonsMultipartResolver() {
@Override
public boolean isMultipart(@NonNull HttpServletRequest request) {
final var method = request.getMethod();
if (!"POST".equalsIgnoreCase(method) && !"PUT".equalsIgnoreCase(method)) {
return false;
}
return FileUploadBase.isMultipartContent(new ServletRequestContext(request));
}
};
resolver.setDefaultEncoding("UTF-8");
resolver.setMaxUploadSize(multipartConfigElement.getMaxRequestSize());
resolver.setMaxUploadSizePerFile(multipartConfigElement.getMaxFileSize());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ public ThemeProperty uploadTheme(@RequestPart("file") MultipartFile file) {
return themeService.upload(file);
}

@PutMapping("upload/{themeId}")
@PostMapping("upload/{themeId}")
@ApiOperation("Upgrades theme by file")
public ThemeProperty updateThemeByUpload(@PathVariable("themeId") String themeId,
Expand All @@ -190,40 +191,45 @@ public ThemeProperty fetchTheme(@RequestParam("uri") String uri) {
return themeService.fetch(uri);
}

@PostMapping("fetchingBranches")
@PostMapping(value = {"fetchingBranches", "/fetching/git/branches"})
@ApiOperation("Fetches all branches")
@Deprecated(since = "1.4.2", forRemoval = true)
public List<ThemeProperty> fetchBranches(@RequestParam("uri") String uri) {
return themeService.fetchBranches(uri);
}

@PostMapping("fetchingReleases")
@ApiOperation("Fetches all releases")
@Deprecated(since = "1.4.2", forRemoval = true)
public List<ThemeProperty> fetchReleases(@RequestParam("uri") String uri) {
return themeService.fetchReleases(uri);
}

@GetMapping("fetchingRelease")
@ApiOperation("Fetches a specific release")
@Deprecated(since = "1.4.2", forRemoval = true)
public ThemeProperty fetchRelease(@RequestParam("uri") String uri,
@RequestParam("tag") String tagName) {
return themeService.fetchRelease(uri, tagName);
}

@GetMapping("fetchBranch")
@ApiOperation("Fetch specific branch")
@Deprecated(since = "1.4.2", forRemoval = true)
public ThemeProperty fetchBranch(@RequestParam("uri") String uri,
@RequestParam("branch") String branchName) {
return themeService.fetchBranch(uri, branchName);
}

@GetMapping("fetchLatestRelease")
@ApiOperation("Fetch latest release")
@Deprecated(since = "1.4.2", forRemoval = true)
public ThemeProperty fetchLatestRelease(@RequestParam("uri") String uri) {
return themeService.fetchLatestRelease(uri);
}

@PutMapping("fetching/{themeId}")
@ApiOperation("Upgrades theme by remote")
@ApiOperation("Upgrades theme from remote")
public ThemeProperty updateThemeByFetching(@PathVariable("themeId") String themeId) {
return themeService.update(themeId);
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/run/halo/app/core/ControllerLogAop.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@
@Slf4j
public class ControllerLogAop {

@Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
public void restController() {
}

@Pointcut("@within(org.springframework.stereotype.Controller)")
public void controller() {
}

@Around("controller()")
@Around("controller() || restController()")
public Object controller(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
final Method method = signature.getMethod();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/run/halo/app/exception/ThemeUpToDateException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package run.halo.app.exception;

/**
* Theme up to date exception.
*
* @author johnniang
*/
public class ThemeUpToDateException extends BadRequestException {

public ThemeUpToDateException(String message) {
super(message);
}
}
15 changes: 5 additions & 10 deletions src/main/java/run/halo/app/handler/file/LocalFileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.nio.file.Paths;
import java.util.Calendar;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -58,8 +57,6 @@ public class LocalFileHandler implements FileHandler {

private final String workDir;

private final ReentrantLock lock = new ReentrantLock();

public LocalFileHandler(OptionService optionService,
HaloProperties haloProperties) {
this.optionService = optionService;
Expand All @@ -79,13 +76,11 @@ private void checkWorkDir() {
Path workPath = Paths.get(workDir);

// Check file type
Assert.isTrue(Files.isDirectory(workPath), workDir + " isn't a directory");

// Check readable
Assert.isTrue(Files.isReadable(workPath), workDir + " isn't readable");

// Check writable
Assert.isTrue(Files.isWritable(workPath), workDir + " isn't writable");
if (!Files.isDirectory(workPath)
|| !Files.isReadable(workPath)
|| !Files.isWritable(workPath)) {
log.warn("Please make sure that {} is a directory, readable and writable!", workDir);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Theme property.
*
* @author ryanwang
* @author johnniang
* @date 2019-03-22
*/
@Data
Expand All @@ -31,13 +32,18 @@ public class ThemeProperty {
/**
* Theme remote branch.(default is master)
*/
private String branch;
private String branch = "master";

/**
* Theme repo url.
* Theme git repo url.
*/
private String repo;

/**
* Theme update strategy. Default is branch.
*/
private UpdateStrategy updateStrategy = UpdateStrategy.RELEASE;

/**
* Theme description.
*/
Expand Down Expand Up @@ -115,8 +121,13 @@ public int hashCode() {
return Objects.hash(id);
}

/**
* Theme author info.
*
* @author johnniang
*/
@Data
private static class Author {
public static class Author {

/**
* Author name.
Expand All @@ -133,4 +144,22 @@ private static class Author {
*/
private String avatar;
}

/**
* Theme update strategy.
*
* @author johnniang
*/
public enum UpdateStrategy {

/**
* Update from specific branch
*/
BRANCH,

/**
* Update from latest release, only available if the repo is a github repo
*/
RELEASE;
}
}
18 changes: 15 additions & 3 deletions src/main/java/run/halo/app/listener/StartedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.sql.SQLException;
import java.util.Collections;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.eclipse.jgit.storage.file.WindowCacheConfig;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.internal.jdbc.JdbcUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -21,10 +23,10 @@
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import org.springframework.util.ResourceUtils;
import run.halo.app.config.properties.HaloProperties;
Expand All @@ -42,7 +44,7 @@
* @date 2018-12-05
*/
@Slf4j
@Configuration
@Component
@Order(Ordered.HIGHEST_PRECEDENCE)
public class StartedListener implements ApplicationListener<ApplicationStartedEvent> {

Expand Down Expand Up @@ -71,9 +73,19 @@ public void onApplicationEvent(ApplicationStartedEvent event) {
} catch (SQLException e) {
log.error("Failed to migrate database!", e);
}
this.initThemes();
this.initDirectory();
this.initThemes();
this.printStartInfo();
this.configGit();
}

private void configGit() {
// Config packed git MMAP
if (SystemUtils.IS_OS_WINDOWS) {
WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitMMAP(false);
config.install();
}
}

private void printStartInfo() {
Expand Down
29 changes: 12 additions & 17 deletions src/main/java/run/halo/app/mail/AbstractMailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
Expand All @@ -28,10 +29,15 @@
public abstract class AbstractMailService implements MailService {

private static final int DEFAULT_POOL_SIZE = 5;

protected final OptionService optionService;

private JavaMailSender cachedMailSender;

private MailProperties cachedMailProperties;

private String cachedFromName;

@Nullable
private ExecutorService executorService;

Expand All @@ -47,7 +53,7 @@ public ExecutorService getExecutorService() {
return executorService;
}

public void setExecutorService(ExecutorService executorService) {
public void setExecutorService(@Nullable ExecutorService executorService) {
this.executorService = executorService;
}

Expand All @@ -72,7 +78,7 @@ public void testConnection() {
*
* @param callback mime message callback.
*/
protected void sendMailTemplate(@Nullable Callback callback) {
protected void sendMailTemplate(@Nullable Consumer<MimeMessageHelper> callback) {
if (callback == null) {
log.info("Callback is null, skip to send email");
return;
Expand Down Expand Up @@ -101,7 +107,7 @@ protected void sendMailTemplate(@Nullable Callback callback) {
// set from-name
messageHelper.setFrom(getFromAddress(mailSender));
// handle message set separately
callback.handle(messageHelper);
callback.accept(messageHelper);

// get mime message
MimeMessage mimeMessage = messageHelper.getMimeMessage();
Expand All @@ -123,9 +129,10 @@ protected void sendMailTemplate(@Nullable Callback callback) {
* @param callback callback message handler
* @param tryToAsync if the send procedure should try to asynchronous
*/
protected void sendMailTemplate(boolean tryToAsync, @Nullable Callback callback) {
protected void sendMailTemplate(boolean tryToAsync,
@Nullable Consumer<MimeMessageHelper> callback) {
ExecutorService executorService = getExecutorService();
if (tryToAsync && executorService != null) {
if (tryToAsync) {
// send mail asynchronously
executorService.execute(() -> sendMailTemplate(callback));
} else {
Expand Down Expand Up @@ -233,16 +240,4 @@ protected void clearCache() {
log.debug("Cleared all mail caches");
}

/**
* Message callback.
*/
protected interface Callback {
/**
* Handle message set.
*
* @param messageHelper mime message helper
* @throws Exception if something goes wrong
*/
void handle(@NonNull MimeMessageHelper messageHelper) throws Exception;
}
}
Loading

0 comments on commit 30c9baf

Please sign in to comment.