2019-01-20 10:13:41 +00:00
|
|
|
<?php
|
2019-12-03 18:57:53 +00:00
|
|
|
|
2019-01-20 10:13:41 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
/**
|
2024-05-23 07:26:56 +00:00
|
|
|
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
|
|
|
namespace OCP\Files\SimpleFS;
|
|
|
|
|
|
|
|
use OCP\Files\NotPermittedException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class represents a file that is only hold in memory.
|
|
|
|
*
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
|
|
|
class InMemoryFile implements ISimpleFile {
|
|
|
|
/**
|
|
|
|
* Holds the file name.
|
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
private string $name;
|
2019-01-20 10:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Holds the file contents.
|
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
private string $contents;
|
2019-01-20 10:13:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* InMemoryFile constructor.
|
|
|
|
*
|
|
|
|
* @param string $name The file name
|
|
|
|
* @param string $contents The file contents
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
|
|
|
public function __construct(string $name, string $contents) {
|
|
|
|
$this->name = $name;
|
|
|
|
$this->contents = $contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function getName(): string {
|
2019-01-20 10:13:41 +00:00
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2023-01-23 14:03:56 +00:00
|
|
|
public function getSize(): int|float {
|
2019-01-20 10:13:41 +00:00
|
|
|
return strlen($this->contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function getETag(): string {
|
2019-01-20 10:13:41 +00:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function getMTime(): int {
|
2019-01-20 10:13:41 +00:00
|
|
|
return time();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function getContent(): string {
|
2019-01-20 10:13:41 +00:00
|
|
|
return $this->contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function putContent($data): void {
|
2019-01-20 10:13:41 +00:00
|
|
|
$this->contents = $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* In memory files can't be deleted.
|
2019-02-01 14:02:53 +00:00
|
|
|
*
|
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function delete(): void {
|
2019-01-20 10:13:41 +00:00
|
|
|
// unimplemented for in memory files
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
2022-06-22 10:05:26 +00:00
|
|
|
public function getMimeType(): string {
|
2019-01-20 10:13:41 +00:00
|
|
|
$fileInfo = new \finfo(FILEINFO_MIME_TYPE);
|
|
|
|
return $fileInfo->buffer($this->contents);
|
|
|
|
}
|
|
|
|
|
2022-03-03 05:10:57 +00:00
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
2022-03-07 09:46:05 +00:00
|
|
|
* @since 24.0.0
|
2022-03-03 05:10:57 +00:00
|
|
|
*/
|
|
|
|
public function getExtension(): string {
|
|
|
|
return \pathinfo($this->name, PATHINFO_EXTENSION);
|
|
|
|
}
|
|
|
|
|
2019-01-20 10:13:41 +00:00
|
|
|
/**
|
|
|
|
* Stream reading is unsupported for in memory files.
|
|
|
|
*
|
|
|
|
* @throws NotPermittedException
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
|
|
|
public function read() {
|
|
|
|
throw new NotPermittedException(
|
|
|
|
'Stream reading is unsupported for in memory files'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stream writing isn't available for in memory files.
|
|
|
|
*
|
|
|
|
* @throws NotPermittedException
|
2019-02-01 14:02:53 +00:00
|
|
|
* @since 16.0.0
|
2019-01-20 10:13:41 +00:00
|
|
|
*/
|
|
|
|
public function write() {
|
|
|
|
throw new NotPermittedException(
|
|
|
|
'Stream writing is unsupported for in memory files'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|