Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: mimetype inconsistency between Detection.detectPath and Loader.updateFilecache #35958

Open
5 of 9 tasks
tacruc opened this issue Jan 3, 2023 · 3 comments
Open
5 of 9 tasks
Labels
0. Needs triage Pending check for reproducibility or if it fits our roadmap feature: files technical debt

Comments

@tacruc
Copy link
Contributor

tacruc commented Jan 3, 2023

⚠️ This issue respects the following points: ⚠️

  • This is a bug, not a question or a configuration/webserver/proxy issue.
  • This issue is not already reported on Github (I've searched it).
  • Nextcloud Server is up to date. See Maintenance and Release Schedule for supported versions.
  • Nextcloud Server is running on 64bit capable CPU, PHP and OS.
  • I agree to follow Nextcloud's Code of Conduct.

Bug description

For files named like .ext e.g. ".gpx", ".ext" or ".maps" get different mimetypes by

public function detectPath($path): string {

or
public function updateFilecache($ext, $mimeTypeId) {
.

To clearify its about files which start with a . and then just have the extension.
Therefore existing files get different mime-types then new file.

Steps to reproduce

  1. Create ".ext" file
  2. Run Loader->updateFilecache("ext", mimeTypeId)
  3. Create corresponding Mimetype mapping
  4. Uploade a file called ".ext" to a different folder
  5. See that it gets a different Mimetype

Expected behavior

  • Get the same Mimetype
  • It would be nice if it would be the Mimetype in the mapping, then mimetypes can be used to find .noindex and .nomedia files, or in maps a .maps file. Searching the file over the mimetype is faster as there is a db index.

Installation method

None

Operating system

None

PHP engine version

None

Web server

None

Database engine version

MySQL

Is this bug present after an update or on a fresh install?

None

Are you using the Nextcloud Server Encryption module?

None

What user-backends are you using?

  • Default user-backend (database)
  • LDAP/ Active Directory
  • SSO - SAML
  • Other

Configuration report

No response

List of activated Apps

[]

Nextcloud Signing status

No response

Nextcloud Logs

No response

Additional info

No response

@tacruc tacruc added bug 0. Needs triage Pending check for reproducibility or if it fits our roadmap labels Jan 3, 2023
@tacruc
Copy link
Contributor Author

tacruc commented Jan 3, 2023

Possible fixes:

	public function detectPath($path): string {
		$this->loadMappings();

		$fileName = basename($path);


		// remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part
		$fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName);

		//try to guess the type by the file extension
		$extension = strrchr($fileName, '.');
		if ($extension !== false) {
			$extension = strtolower($extension);
			$extension = substr($extension, 1); //remove leading .
			return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
		}

		return 'application/octet-stream';
	}

Pros:

  • Minimal change
  • Consistent with 'updateFilecache'

Cons:

	/**
	 * detect mimetype only based on filename, content of file is not used
	 *
	 * @param string $path
	 * @return string
	 */
	public function detectPath($path): string {
		$this->loadMappings();
		$extensions = array_keys($this->mimetypes);
		$fileName = basename($path);
		// remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part
		$fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', $fileName);
		$fileExtensions = [];
		foreach ($extensions as $ext) {
			if (str_ends_with('.'.$fileName, '.'.$ext)) $fileExtensions[]=$ext;
		}

		usort($fileExtensions, function($a, $b) {
			return strlen($b) - strlen($a);
		});

		$extension = array_shift($fileExtensions);
		return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
	}

Pros:

Cons:

  • Performance?
  • Again not consistent with 'updateFilecache()'
	/**
	 * detect mimetype only based on filename, content of file is not used
	 *
	 * @param string $path
	 * @return string
	 */
	public function detectPath($path): string {
		$this->loadMappings();

		$fileName = preg_replace('!((\.v\d+)|((\.ocTransferId\d+)?\.part))$!', '', basename($path));

		if (preg_match(
			'!(^\.(?<ext1>[^\.]+))|(^\.?[^\.]+\.(?<ext2>.+))$!',
			$fileName,
			$matches)){
			$extension = $matches['ext1'] ?? $matches['ext2'];
			return $this->mimetypes[$extension][0] ?? 'application/octet-stream';
		}

		return 'application/octet-stream';
	}

Pros:

Cons:

  • but create error on something like text.old.txt as it recoginzes old.txt as extension
  • Again not consistent with 'updateFilecache()'

@tacruc tacruc added this to the Nextcloud 26 milestone Jan 4, 2023
@szaimen
Copy link
Contributor

szaimen commented Jan 5, 2023

cc @nextcloud/server for input/feedback on this :)

tacruc added a commit to nextcloud/maps that referenced this issue Jan 7, 2023
tacruc added a commit to nextcloud/maps that referenced this issue Jan 7, 2023
@tacruc
Copy link
Contributor Author

tacruc commented Jan 25, 2023

Different fix from @nickvergessen would be:
$update->createNamedParameter('%_' . $this->dbConnection->escapeLikeParameter('.' . $ext))

@blizzz blizzz modified the milestones: Nextcloud 26, Nextcloud 27 Mar 9, 2023
@skjnldsv skjnldsv removed this from the Nextcloud 27.0.2 milestone Aug 8, 2023
@joshtrichards joshtrichards changed the title [Bug]: mimetype inconsitency between Dectection.detectPath and Loader.updateFilecache [Bug]: mimetype inconsistency between Detection.detectPath and Loader.updateFilecache Sep 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
0. Needs triage Pending check for reproducibility or if it fits our roadmap feature: files technical debt
Projects
None yet
Development

No branches or pull requests

5 participants