Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
steering
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AURA
steering
Commits
90e234f6
Verified
Commit
90e234f6
authored
1 year ago
by
Ernesto Rico Schmidt
Browse files
Options
Downloads
Patches
Plain Diff
Add management command to remove stale images
parent
74cb896f
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
program/management/commands/removestaleimages.py
+49
-0
49 additions, 0 deletions
program/management/commands/removestaleimages.py
with
49 additions
and
0 deletions
program/management/commands/removestaleimages.py
0 → 100644
+
49
−
0
View file @
90e234f6
import
os
from
versatileimagefield.fields
import
VersatileImageField
from
django.apps
import
apps
from
django.conf
import
settings
from
django.core.management.base
import
BaseCommand
from
django.db.models
import
ImageField
,
Q
class
Command
(
BaseCommand
):
help
=
"
removes images from the MEDIA_ROOT that are not referenced in the database.
"
def
handle
(
self
,
*
args
,
**
options
):
def
remove_file
(
path
:
str
)
->
None
:
print
(
f
"
REMOVED
{
path
}
"
)
os
.
remove
(
path
)
image_names
=
[]
# names (without extensions) of the images in the database
for
model
in
apps
.
get_models
():
for
field
in
model
.
_meta
.
get_fields
():
if
isinstance
(
field
,
VersatileImageField
)
or
isinstance
(
field
,
ImageField
):
is_empty
=
Q
(
**
{
f
"
{
field
.
name
}
"
:
""
})
images
=
model
.
objects
.
exclude
(
is_empty
).
values_list
(
field
.
name
,
flat
=
True
)
for
image
in
images
:
basename
=
os
.
path
.
basename
(
image
)
filename
,
ext
=
os
.
path
.
splitext
(
basename
)
image_names
.
append
(
filename
)
media_root
=
getattr
(
settings
,
"
MEDIA_ROOT
"
)
for
relative_root
,
dirs
,
files
in
os
.
walk
(
media_root
):
for
file_
in
files
:
relative_path
=
os
.
path
.
join
(
os
.
path
.
relpath
(
relative_root
,
media_root
),
file_
)
head
,
tail
=
os
.
path
.
split
(
relative_path
)
filename
,
ext
=
os
.
path
.
splitext
(
tail
)
fullpath
=
os
.
path
.
join
(
media_root
,
relative_path
)
if
not
head
.
startswith
(
"
__sized__
"
):
if
filename
not
in
image_names
:
remove_file
(
fullpath
)
else
:
# cropped images
name
,
_
=
filename
.
split
(
"
-crop-
"
)
if
name
not
in
image_names
:
remove_file
(
fullpath
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment