| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | package plugins | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"net/url" | 
					
						
							|  |  |  | 	"path" | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 	"strings" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-15 00:16:10 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util" | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type FrontendPluginBase struct { | 
					
						
							|  |  |  | 	PluginBase | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (fp *FrontendPluginBase) initFrontendPlugin() { | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 	if isExternalPlugin(fp.PluginDir) { | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 		StaticRoutes = append(StaticRoutes, &PluginStaticRoute{ | 
					
						
							| 
									
										
										
										
											2016-03-15 00:16:10 +08:00
										 |  |  | 			Directory: fp.PluginDir, | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 			PluginId:  fp.Id, | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 	fp.handleModuleDefaults() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 17:15:16 +08:00
										 |  |  | 	fp.Info.Logos.Small = getPluginLogoUrl(fp.Type, fp.Info.Logos.Small, fp.BaseUrl) | 
					
						
							|  |  |  | 	fp.Info.Logos.Large = getPluginLogoUrl(fp.Type, fp.Info.Logos.Large, fp.BaseUrl) | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for i := 0; i < len(fp.Info.Screenshots); i++ { | 
					
						
							| 
									
										
										
										
											2016-03-08 04:45:49 +08:00
										 |  |  | 		fp.Info.Screenshots[i].Path = evalRelativePluginUrlPath(fp.Info.Screenshots[i].Path, fp.BaseUrl) | 
					
						
							| 
									
										
										
										
											2016-02-09 22:36:42 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 17:15:16 +08:00
										 |  |  | func getPluginLogoUrl(pluginType, path, baseUrl string) string { | 
					
						
							| 
									
										
										
										
											2016-03-15 16:15:24 +08:00
										 |  |  | 	if path == "" { | 
					
						
							| 
									
										
										
										
											2016-03-17 17:15:16 +08:00
										 |  |  | 		return "public/img/icn-" + pluginType + ".svg" | 
					
						
							| 
									
										
										
										
											2016-03-15 16:15:24 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return evalRelativePluginUrlPath(path, baseUrl) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | func (fp *FrontendPluginBase) setPathsBasedOnApp(app *AppPlugin) { | 
					
						
							| 
									
										
										
										
											2018-01-04 05:07:38 +08:00
										 |  |  | 	appSubPath := strings.Replace(strings.Replace(fp.PluginDir, app.PluginDir, "", 1), "\\", "/", -1) | 
					
						
							| 
									
										
										
										
											2016-02-10 05:10:36 +08:00
										 |  |  | 	fp.IncludedInAppId = app.Id | 
					
						
							|  |  |  | 	fp.BaseUrl = app.BaseUrl | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if isExternalPlugin(app.PluginDir) { | 
					
						
							| 
									
										
										
										
											2019-01-29 05:18:48 +08:00
										 |  |  | 		fp.Module = util.JoinURLFragments("plugins/"+app.Id, appSubPath) + "/module" | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2019-01-29 05:18:48 +08:00
										 |  |  | 		fp.Module = util.JoinURLFragments("app/plugins/app/"+app.Id, appSubPath) + "/module" | 
					
						
							| 
									
										
										
										
											2016-09-27 20:39:51 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (fp *FrontendPluginBase) handleModuleDefaults() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 	if isExternalPlugin(fp.PluginDir) { | 
					
						
							| 
									
										
										
										
											2016-01-10 06:56:39 +08:00
										 |  |  | 		fp.Module = path.Join("plugins", fp.Id, "module") | 
					
						
							| 
									
										
										
										
											2016-02-10 01:17:32 +08:00
										 |  |  | 		fp.BaseUrl = path.Join("public/plugins", fp.Id) | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | 	fp.IsCorePlugin = true | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 	fp.Module = path.Join("app/plugins", fp.Type, fp.Id, "module") | 
					
						
							| 
									
										
										
										
											2016-02-10 01:17:32 +08:00
										 |  |  | 	fp.BaseUrl = path.Join("public/app/plugins", fp.Type, fp.Id) | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-09 04:42:33 +08:00
										 |  |  | func isExternalPlugin(pluginDir string) bool { | 
					
						
							| 
									
										
										
										
											2016-03-15 00:16:10 +08:00
										 |  |  | 	return !strings.Contains(pluginDir, setting.StaticRootPath) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 21:31:02 +08:00
										 |  |  | func evalRelativePluginUrlPath(pathStr string, baseUrl string) string { | 
					
						
							| 
									
										
										
										
											2016-02-27 01:25:39 +08:00
										 |  |  | 	if pathStr == "" { | 
					
						
							|  |  |  | 		return "" | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | 	u, _ := url.Parse(pathStr) | 
					
						
							|  |  |  | 	if u.IsAbs() { | 
					
						
							|  |  |  | 		return pathStr | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-07 21:31:02 +08:00
										 |  |  | 	return path.Join(baseUrl, pathStr) | 
					
						
							| 
									
										
										
										
											2016-01-10 06:34:20 +08:00
										 |  |  | } |