@@ -55,7 +55,7 @@ func (cli *Client) ContentBySpaceAndTitle(space, title string) (Content, error)
5555 q := url.Values {
5656 "title" : {title },
5757 "spaceKey" : {space },
58- "expand" : {"version,body.storage" },
58+ "expand" : {"version,body.storage,ancestors " },
5959 }
6060
6161 resp , err := cli .ApiGET ("/content" , q )
@@ -158,7 +158,7 @@ func (cli *Client) ContentUpdate(content Content) (Content, error) {
158158}
159159
160160//从指定空间查找或创建指定标题的内容
161- func (cli * Client ) PageFindOrCreateBySpaceAndTitle (space , parentId , title , data string ) (Content , error ) {
161+ func (cli * Client ) PageFindOrCreateBySpaceAndTitle (space , parentId , title , wikiDirPrefix , data string ) (Content , error ) {
162162 //内容中的空行会被Confluence保存时自动去掉
163163 //因此前先去掉,以避免对比内容变化时受到影响
164164 data = strings .TrimSuffix (strings .TrimPrefix (data , "\n " ), "\n " )
@@ -173,31 +173,44 @@ func (cli *Client) PageFindOrCreateBySpaceAndTitle(space, parentId, title, data
173173 return cli .PageCreateInSpace (space , parentId , title , data )
174174 }
175175
176- //存在:对比内容是否有变化
177- newValue , err := cli .ContentBodyConvertTo (data , "storage" , "view" )
178- if err != nil {
179- return Content {}, fmt .Errorf ("转换新内容失败: %s" , err )
176+ // 获取文件的路径
177+ pagePath := ""
178+ for i , ancestor := range content .Ancestors {
179+ if i != 0 {
180+ pagePath += "/" + ancestor .Title
181+ }
180182 }
181183
182- oldValue , err := cli .ContentBodyConvertTo (content .Body .Storage .Value , "storage" , "view" )
183- if err != nil {
184- return Content {}, fmt .Errorf ("转换旧内容失败: %s" , err )
185- }
184+ //存在,但不在指定的路径下,报错结束
185+ if ! strings .HasPrefix (pagePath , wikiDirPrefix ) {
186+ return Content {}, fmt .Errorf ("一个标题为 '%v' 的页面已经存在于该空间中。为您的页面输入一个不同的标题。" , title )
187+ } else {
188+ //存在:对比内容是否有变化
189+ newValue , err := cli .ContentBodyConvertTo (data , "storage" , "view" )
190+ if err != nil {
191+ return Content {}, fmt .Errorf ("转换新内容失败: %s" , err )
192+ }
186193
187- if newValue == oldValue {
188- return content , nil
189- }
194+ oldValue , err := cli .ContentBodyConvertTo (content .Body .Storage .Value , "storage" , "view" )
195+ if err != nil {
196+ return Content {}, fmt .Errorf ("转换旧内容失败: %s" , err )
197+ }
190198
191- // 存在则否则更新
192- content .Space .Key = space
193- content .Version .Number += 1
194- content .Version .Message = time .Now ().Local ().Format ("机器人更新于2006-01-02 15:04:05" )
195- content .SetStorageBody (data )
199+ if newValue == oldValue {
200+ return content , nil
201+ }
196202
197- //设置父页面
198- if parentId != "" {
199- content .Ancestors = []Content {Content {Id : parentId }}
200- }
203+ // 存在则否则更新
204+ content .Space .Key = space
205+ content .Version .Number += 1
206+ content .Version .Message = time .Now ().Local ().Format ("机器人更新于2006-01-02 15:04:05" )
207+ content .SetStorageBody (data )
208+
209+ //设置父页面
210+ if parentId != "" {
211+ content .Ancestors = []Content {Content {Id : parentId }}
212+ }
201213
202- return cli .ContentUpdate (content )
214+ return cli .ContentUpdate (content )
215+ }
203216}
0 commit comments