fileList.append(element);
void file.ready.then(() => {
- const thumbnail = file.thumbnails.find((thumbnail) => {
- return thumbnail.identifier === "tiny";
- });
+ if (file.isImage()) {
+ const thumbnail = file.thumbnails.find((thumbnail) => {
+ return thumbnail.identifier === "tiny";
+ });
- if (thumbnail !== undefined) {
- file.thumbnail = thumbnail;
+ if (thumbnail !== undefined) {
+ file.thumbnail = thumbnail;
+ }
}
});
}
endpointThumbnails: string;
fileID: number;
typeName: string;
+ mimeType: string;
data: Record<string, unknown>;
};
}
const hasThumbnails = response.endpointThumbnails !== "";
- fileElement.uploadCompleted(response.fileID, hasThumbnails);
+ fileElement.uploadCompleted(response.fileID, response.mimeType, hasThumbnails);
if (hasThumbnails) {
await generateThumbnails(fileElement, response.endpointThumbnails);
}
}
-async function generateThumbnails(
- fileElement: WoltlabCoreFileElement,
- endpoint: string,
-): Promise<void> {
+async function generateThumbnails(fileElement: WoltlabCoreFileElement, endpoint: string): Promise<void> {
let response: GenerateThumbnailsResponse;
try {
export class WoltlabCoreFileElement extends HTMLElement {
#filename: string = "";
#fileId: number | undefined = undefined;
+ #mimeType: string | undefined = undefined;
#state: State = State.Initial;
readonly #thumbnails: Thumbnail[] = [];
// Files that exist at page load have a valid file id, otherwise a new
// file element can only be the result of an upload attempt.
if (this.#fileId === undefined) {
- this.#filename = this.dataset.filename || "";
+ this.#filename = this.dataset.filename || "bogus.bin";
delete this.dataset.filename;
+ this.#mimeType = this.dataset.mimeType || "application/octet-stream";
+ delete this.dataset.mimeType;
+
const fileId = parseInt(this.getAttribute("file-id") || "0");
if (fileId) {
this.#fileId = fileId;
return this.#filename;
}
+ get mimeType(): string | undefined {
+ return this.#mimeType;
+ }
+
+ isImage(): boolean {
+ if (this.mimeType === undefined) {
+ return false;
+ }
+
+ switch (this.mimeType) {
+ case "image/gif":
+ case "image/jpeg":
+ case "image/png":
+ case "image/webp":
+ return true;
+
+ default:
+ return false;
+ }
+ }
+
uploadFailed(): void {
if (this.#state !== State.Uploading) {
return;
this.#readyReject();
}
- uploadCompleted(fileId: number, hasThumbnails: boolean): void {
+ uploadCompleted(fileId: number, mimeType: string, hasThumbnails: boolean): void {
if (this.#state === State.Uploading) {
this.#fileId = fileId;
+ this.#mimeType = mimeType;
this.setAttribute("file-id", fileId.toString());
if (hasThumbnails) {
element.append(file);
fileList.append(element);
void file.ready.then(() => {
- const thumbnail = file.thumbnails.find((thumbnail) => {
- return thumbnail.identifier === "tiny";
- });
- if (thumbnail !== undefined) {
- file.thumbnail = thumbnail;
+ if (file.isImage()) {
+ const thumbnail = file.thumbnails.find((thumbnail) => {
+ return thumbnail.identifier === "tiny";
+ });
+ if (thumbnail !== undefined) {
+ file.thumbnail = thumbnail;
+ }
}
});
}
return;
}
const hasThumbnails = response.endpointThumbnails !== "";
- fileElement.uploadCompleted(response.fileID, hasThumbnails);
+ fileElement.uploadCompleted(response.fileID, response.mimeType, hasThumbnails);
if (hasThumbnails) {
await generateThumbnails(fileElement, response.endpointThumbnails);
}
class WoltlabCoreFileElement extends HTMLElement {
#filename = "";
#fileId = undefined;
+ #mimeType = undefined;
#state = 0 /* State.Initial */;
#thumbnails = [];
#readyReject;
// Files that exist at page load have a valid file id, otherwise a new
// file element can only be the result of an upload attempt.
if (this.#fileId === undefined) {
- this.#filename = this.dataset.filename || "";
+ this.#filename = this.dataset.filename || "bogus.bin";
delete this.dataset.filename;
+ this.#mimeType = this.dataset.mimeType || "application/octet-stream";
+ delete this.dataset.mimeType;
const fileId = parseInt(this.getAttribute("file-id") || "0");
if (fileId) {
this.#fileId = fileId;
get filename() {
return this.#filename;
}
+ get mimeType() {
+ return this.#mimeType;
+ }
+ isImage() {
+ if (this.mimeType === undefined) {
+ return false;
+ }
+ switch (this.mimeType) {
+ case "image/gif":
+ case "image/jpeg":
+ case "image/png":
+ case "image/webp":
+ return true;
+ default:
+ return false;
+ }
+ }
uploadFailed() {
if (this.#state !== 1 /* State.Uploading */) {
return;
this.#rebuildElement();
this.#readyReject();
}
- uploadCompleted(fileId, hasThumbnails) {
+ uploadCompleted(fileId, mimeType, hasThumbnails) {
if (this.#state === 1 /* State.Uploading */) {
this.#fileId = fileId;
+ this.#mimeType = mimeType;
this.setAttribute("file-id", fileId.toString());
if (hasThumbnails) {
this.#state = 2 /* State.GeneratingThumbnails */;
'endpointThumbnails' => $endpointThumbnails,
'fileID' => $file->fileID,
'typeName' => $file->typeName,
+ 'mimeType' => $file->mimeType,
'data' => $processor->getUploadResponse($file),
]);
}
use wcf\system\file\processor\FileProcessor;
use wcf\system\file\processor\IFileProcessor;
use wcf\system\request\LinkHandler;
-use wcf\util\FileUtil;
/**
* @author Alexander Ebert
* @property-read int $fileSize
* @property-read string $fileHash
* @property-read string $typeName
+ * @property-read string $mimeType
*/
class File extends DatabaseObject
{
public function isImage(): bool
{
- $mimeType = FileUtil::getMimeType($this->getPath() . $this->getSourceFilename());
-
- return match ($mimeType) {
+ return match ($this->mimeType) {
'image/gif' => true,
- 'image/jpg', 'image/jpeg' => true,
+ 'image/jpeg' => true,
'image/png' => true,
'image/webp' => true,
default => false,
use wcf\data\DatabaseObjectEditor;
use wcf\data\file\temporary\FileTemporary;
+use wcf\util\FileUtil;
/**
* @author Alexander Ebert
public static function createFromTemporary(FileTemporary $fileTemporary): File
{
+ $mimeType = FileUtil::getMimeType($fileTemporary->getPath() . $fileTemporary->getFilename());
+
$fileAction = new FileAction([], 'create', ['data' => [
'filename' => $fileTemporary->filename,
'fileSize' => $fileTemporary->fileSize,
'fileHash' => $fileTemporary->fileHash,
'typeName' => $fileTemporary->typeName,
+ 'mimeType' => $mimeType,
]]);
$file = $fileAction->executeAction()['returnValues'];
\assert($file instanceof File);
filename VARCHAR(255) NOT NULL,
fileSize BIGINT NOT NULL,
fileHash CHAR(64) NOT NULL,
- typeName VARCHAR(255) NOT NULL
+ typeName VARCHAR(255) NOT NULL,
+ mimeType VARCHAR(255) NOT NULL,
);
DROP TABLE IF EXISTS wcf1_file_temporary;