2
1
0
1 回答
- 543
私も同じ問題を抱えました。。
ページが保持する全ての添付ファイルとマニュアルで利用されている添付ファイルを比較し、ファイル名が一致しないファイルを削除すればできると思ったのでJavaScriptで試してみました。
少しでもご参考になれば幸いです。
※補足
・OS、windows10
・ファイル名、image****.png
・Confluence、ver 7.1.1 動作確認
・Chrome デベロッパーツール の Consoleで実行var token = AJS.$('#atlassian-token').attr('content'); var contextPath = AJS.contextPath(); var contentId = Confluence.getContentId(); var spaceKey = AJS.Data.get('space-key'); (function(){ if(token && contentId && spaceKey) unnecessary_image_file_deletion(); else console.error("The function execution failed."); })(); function unnecessary_image_file_deletion(){ $.ajax({ type: "GET", url:contextPath + "/rest/api/content/" + contentId + "/child/attachment?start=0&limit=1000", dataType: "json", success: function (data){ for(var i = 0, len = data.results.length; i < len; i++){ var attachment_name = data.results[i].title; var is_matched = attachment_name_comparison( attachment_name ); if(is_matched){ delete_target_Attachment( attachment_name, contentId ) }else if(is_matched == "null"){ console.error("There is no image in the page."); break; } } }, error : function(xhr, errorText){ console.log('Error '+ xhr.responseText); } }); }; function attachment_name_comparison(file_name){ var images = get_necessary_files(); if(images.length == 0){ return "null"; } var targetImages = images.split(","); var bool = true; for(var j = 0, len = targetImages.length; j < len; j++){ var filename = file_name.replace(/[\,]/g,"").trim() var target_file_name = targetImages[j].replace(/[\"]/g,"").trim() if(filename === target_file_name){ bool = false; } } return bool; } function delete_target_Attachment(title,contentId){ $.ajax({ type: "POST", timeout: 10000, url:contextPath + "/json/removeattachment.action?pageId=" + contentId + "&fileName=" + title, contentType:"application/x-www-form-urlencoded; charset=UTF-8", dataType: "json", sync: false, data: 'atl_token=' + AJS.$('#atlassian-token').attr('content'), success: function (){ console.log("Delete target file : " + title); }, error : function(xhr, errorText){ console.log('Error '+ xhr.responseText); } }); } function get_necessary_files(){ var myregex = new RegExp("image.*png", "g"); var confluence_embedded_images = document.querySelectorAll(".confluence-embedded-image"); if(confluence_embedded_images.length == 0){ console.error("Target delete file is not exsit.") return "null"; } var array = []; for(var i = 0, len = confluence_embedded_images.length; i < len; i++){ var link = confluence_embedded_images[i].getAttribute("src").trim(); var str = link.match(myregex); if(link.match(myregex)){ array.push(str); } } var joinStr; for(var i = 0, len = array.length -1; i < array.length; i++){ if(len != 0){ len--; joinStr += '"' + array[i] + '"' + ", "; }else{ joinStr += '"' + array[i] + '"'; } } var a = "undefined"; return joinStr.replace( a , " " ) ; }
イメージ
実行前
実行後コメントを追加...
Confluence でマニュアルを作る時に、スクショが簡単に貼れるので重宝しています。
ですが、スクショを修正して貼り直した時に不要な画像ファイルが残ってしまいます。これを整理する方法をご存じの方が居ましたら、教えて下さい。