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

files: fix upload of files with special characters in filename #324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion projects/sonar/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import { UserComponent } from './record/user/user.component';
import { ValidationComponent } from './record/validation/validation.component';
import { UserService } from './user.service';
import { AdminComponent } from './_layout/admin/admin.component';
import { UnicodeNormalizerPipe } from './pipe/unicode-normalizer';

export function appInitializerFactory(appInitializerService: AppInitializerService): () => Promise<any> {
return () => appInitializerService.initialize().toPromise();
Expand Down Expand Up @@ -114,7 +115,8 @@ export function minElementError(err: any, field: FormlyFieldConfig) {
SubdivisionDetailComponent,
ContributorsPipe,
ContributionsComponent,
ContributionComponent
ContributionComponent,
UnicodeNormalizerPipe
],
imports: [
BrowserModule,
Expand Down
2 changes: 1 addition & 1 deletion projects/sonar/src/app/deposit/upload/upload.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export class UploadComponent implements OnInit, AfterContentChecked, OnDestroy {
concatMap((file: File) => {
return this._depositService.uploadFile(
this.deposit.pid,
file.name,
encodeURIComponent(file.name),
type,
file
);
Expand Down
36 changes: 36 additions & 0 deletions projects/sonar/src/app/pipe/unicode-normalizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* SONAR User Interface
* Copyright (C) 2022 RERO
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { Pipe, PipeTransform } from '@angular/core';

/**
* Get string in its unicode normalized form.
*/
@Pipe({
name: 'unicode_normalizer'
})
export class UnicodeNormalizerPipe implements PipeTransform {
/**
* Transform string to a unicode normalized form.
*
* @param string String to transform.
* @param unicode Unicode form.
* @return Unicode normalized form of string.
*/
transform(str: string, unicodeForm: string) {
return str.normalize(unicodeForm);
}
}
20 changes: 10 additions & 10 deletions projects/sonar/src/app/record/document/file/file.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@
<ng-container *ngIf="file; else noFile">
<ng-container *ngIf="link; else previewOrExternal">
<a [routerLink]="link" *ngIf="inRouter; else normalLink">
<img src="{{ file.thumbnail }}" class="img-fluid" alt="{{ file.label }}">
<img src="{{ file.thumbnail | unicode_normalizer:'NFD' }}" class="img-fluid" alt="{{ file.label }}">
</a>
<ng-template #normalLink>
<a [href]="link">
<img src="{{ file.thumbnail }}" class="img-fluid" alt="{{ file.label }}">
<img src="{{ file.thumbnail | unicode_normalizer:'NFD' }}" class="img-fluid" alt="{{ file.label }}">
</a>
</ng-template>
</ng-container>
<ng-template #previewOrExternal>
<ng-container *ngIf="showExternalLink && file.links.external; else previewLink">
<a href="{{ file.links.external }}" target="_blank">
<img src="{{ file.thumbnail }}" class="img-fluid" alt="{{ file.label }}">
<img src="{{ file.thumbnail | unicode_normalizer:'NFD' }}" class="img-fluid" alt="{{ file.label }}">
</a>
</ng-container>
<ng-template #previewLink>
<ng-container *ngIf="showPreview && file.links.preview; else downloadLink">
<a href="{{ file.links.preview }}" (click)="preview(file); $event.preventDefault()">
<img src="{{ file.thumbnail }}" class="img-fluid" alt="{{ file.label }}">
<a href="{{ file.links.preview | unicode_normalizer:'NFD' }}" (click)="preview(file); $event.preventDefault()">
<img src="{{ file.thumbnail | unicode_normalizer:'NFD' }}" class="img-fluid" alt="{{ file.label }}">
</a>
</ng-container>
<ng-template #downloadLink>
<ng-container *ngIf="showDownload && file.links.download; else noLink">
<a href="{{ file.links.download }}?download" *ngIf="showDownload && file.links.download">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove ngIf the property showDownload still used in the component?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it is already in the line above:
<ng-container *ngIf="showDownload && file.links.download; else noLink">

<img src="{{ file.thumbnail }}" class="img-fluid" alt="{{ file.label }}">
<a href="{{ file.links.download | unicode_normalizer:'NFD' }}?download">
<img src="{{ file.thumbnail | unicode_normalizer:'NFD' }}" class="img-fluid" alt="{{ file.label }}">
</a>
</ng-container>
<ng-template #noLink>
<img src="{{ file.thumbnail }}" class="img-fluid" alt="{{ file.label }}">
<img src="{{ file.thumbnail | unicode_normalizer:'NFD' }}" class="img-fluid" alt="{{ file.label }}">
</ng-template>
</ng-template>
</ng-template>
Expand All @@ -59,11 +59,11 @@
<a href="{{ file.links.external }}" target="_blank" *ngIf="showExternalLink && file.links.external">
<i class="fa fa-external-link mx-1"></i>
</a>
<a href="{{ file.links.preview }}" *ngIf="showPreview && file.links.preview"
<a href="{{ file.links.preview | unicode_normalizer:'NFD' }}" *ngIf="showPreview && file.links.preview"
(click)="preview(file); $event.preventDefault()">
<i class="fa fa-eye mx-1"></i>
</a>
<a href="{{ file.links.download }}?download" *ngIf="showDownload && file.links.download">
<a href="{{ file.links.download | unicode_normalizer:'NFD' }}?download" *ngIf="showDownload && file.links.download">
<i class="fa fa-download mx-1"></i>
</a>
<a href="" *ngIf="statistics && showStatistics"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,5 @@ export class FileComponent {
preview(file: DocumentFile): void {
this.previewClicked.emit(file);
}

}