MCPcopy Create free account
hub / github.com/getgridea/gridea / savePostToFile

Method savePostToFile

src/server/posts.ts:160–204  ·  view source on GitHub ↗

* Save Post to file * @param post

(post: IPost)

Source from the content-addressed store, hash-verified

158 * @param post
159 */
160 async savePostToFile(post: IPost): Promise<IPost | null> {
161 const helper = new ContentHelper()
162 const content = helper.changeImageUrlLocalToDomain(post.content, this.db.setting.domain)
163 const extendName = (post.featureImage.name || 'jpg').split('.').pop()
164
165 post.title = formatYamlString(post.title)
166
167 const mdStr = `---
168title: '${post.title}'
169date: ${post.date}
170tags: [${post.tags.join(',')}]
171published: ${post.published}
172hideInList: ${post.hideInList}
173feature: ${post.featureImage.name ? `/post-images/${post.fileName}.${extendName}` : post.featureImagePath}
174isTop: ${post.isTop}
175---
176${content}`
177
178 try {
179 // If exist feature image
180 if (post.featureImage.path) {
181 const filePath = `${this.postImageDir}/${post.fileName}.${extendName}`
182
183 if (post.featureImage.path !== filePath) {
184 fse.copySync(post.featureImage.path, filePath)
185
186 // Clean the old file
187 if (post.featureImage.path.includes(this.postImageDir)) {
188 fse.removeSync(post.featureImage.path)
189 }
190 }
191 }
192
193 // Write file must use fse, beause fs.writeFile need callback
194 await fse.writeFile(`${this.postDir}/${post.fileName}.md`, mdStr)
195
196 // Clean the old file
197 if (post.deleteFileName) {
198 fse.removeSync(`${this.postDir}/${post.deleteFileName}.md`)
199 }
200 } catch (e) {
201 console.error('ERROR: ', e)
202 }
203 return post
204 }
205
206 async deletePost(post: IPostDb) {
207 try {

Callers 1

constructorMethod · 0.95

Calls 2

formatYamlStringFunction · 0.90

Tested by

no test coverage detected