Skip to content

Commit 035ed0e

Browse files
committed
Add support for many file types
1 parent 065eec7 commit 035ed0e

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

backend/internal/entity/content.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ type Content struct {
77
Title string `gorm:"size:255;not null;" json:"title"`
88
Path string `gorm:"size:255;not null;" json:"path"`
99
Body string `gorm:"size:2000;" json:"body"`
10+
Type string `gorm:"size:255;not null;" json:"type"`
1011
ModuleID uint
1112
Module Module `gorm:"foreignKey:ModuleID;references:ID" json:"-"`
1213
}

backend/internal/handlers/courses.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,12 @@ func EditContent(c *fiber.Ctx) error {
352352
if err := database.DB.Model(&content).Update("path", path).Error; err != nil{
353353
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": err.Error()})
354354
}
355+
356+
// Add the type
357+
filetype := file.Header.Get("Content-Type")
358+
if err := database.DB.Model(&content).Update("type", filetype).Error; err != nil{
359+
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"message": err.Error()})
360+
}
355361
}
356362

357363
return c.JSON(fiber.Map{

frontend/src/components/pages/Content.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,18 @@ export function Content() {
130130
);
131131
}
132132

133+
let attachment = null;
134+
if (contentInfo.path !== "") {
135+
let mediaType = contentInfo.type.split("/")[0];
136+
if (mediaType === "image") {
137+
attachment = <img src={process.env.PUBLIC_URL + "/content" + contentInfo.path} alt="Uploaded File"/>
138+
} else if (mediaType === "video") {
139+
attachment = <iframe src={process.env.PUBLIC_URL + "/content" + contentInfo.path} className="w-screen lg:max-w-5xl max-w-[90%] aspect-video" title="Uploaded File"/>
140+
} else {
141+
attachment = <iframe src={process.env.PUBLIC_URL + "/content" + contentInfo.path} className="w-[82vw] h-[82vh]" title="Uploaded File"/>
142+
}
143+
}
144+
133145
return (
134146
<div className="p-6">
135147
<div className="flex justify-between">
@@ -144,10 +156,7 @@ export function Content() {
144156
<div>
145157
{contentInfo.body}
146158
</div>
147-
{contentInfo.path !== "" ?
148-
<img src={process.env.PUBLIC_URL + "/content" + contentInfo.path} alt=""/> : null
149-
}
159+
{attachment}
150160
</div>
151-
152161
);
153162
}

0 commit comments

Comments
 (0)